+ 1

for loop doubt

#include <iostream> using namespace std; int main() { for (int a = 0; a < 6;a++) // if i put a++ here then i get output as 012345 { cout << a << endl; } return 0; } #include <iostream> using namespace std; int main() { for (int a = 0; a < 6;) { cout << a << endl; a++; // if i put a++ here then i get no output } return 0; } #include <iostream> using namespace std; int main() { for (int a = 0; a < 6;) { a++; // if here then i get 12345 cout << a << endl; } return 0; } please explain me how this loop is working and how i m getting different answers thanks.

17th Jul 2016, 2:40 AM
Karan Luther
Karan Luther - avatar
8 Réponses
+ 2
In the program given below, when a value becomes equal to 6, the condition fails so you will get 012345. #include <iostream> using namespace std; int main() { for (int a = 0; a < 6;a++) { cout << a << endl; } return 0; } The code given below is same as that of above except, the increment operation is done inside the loop. This also results into the same answer as above. Here also you will get 012345. Just check once properly. #include <iostream> using namespace std; int main() { for (int a = 0; a < 6;) { cout << a << endl; a++; } return 0; } In the last code, given below, the increment operation is done before the print operation, as a result 0 will not be printed and you will get the result 12345. #include <iostream> using namespace std; int main() { for (int a = 0; a < 6;) { a++; cout << a << endl; } return 0; }
17th Jul 2016, 6:34 AM
B. K.
+ 2
after that if condition true it enters into loop after everything written inside loop block is compiled then compiler goes to increment and then checks condition after that it goes inside loop if condition true and this goes on remember a compiler generally comes from top to bottom
17th Jul 2016, 6:42 AM
manish rawat
manish rawat - avatar
+ 1
u will understand all if u look into what compiler does compiler first looks at initialisation like int I=0 then it checks condition like I<10
17th Jul 2016, 6:39 AM
manish rawat
manish rawat - avatar
+ 1
for loop always works in such a fashion that the initialization takes place then the condition is checked then it will perform the function in it.then only it consider the increment or decrement.but in the 3 rd one u have the increment as a function inside the loop.hence it prints12345
17th Jul 2016, 11:44 AM
gowtham
0
#include <iostream> using namespace std; int main() { for (int a = 0; a < 6;a++) { cout << a << endl; } return 0; } why 012345 is coming ? initally a = 0 then increment of a that is a++ is taking place before printing so it should only print 12345 why 0 is coming and when does compiler increment ?
17th Jul 2016, 6:41 AM
Karan Luther
Karan Luther - avatar
0
you mean after checking for the condition compiler first prints the value then increment takes place then again it checks for the condition and prints then increments and so on until condition fails... thanks bro..
17th Jul 2016, 6:47 AM
Karan Luther
Karan Luther - avatar
0
The last code should output 123456
23rd Jul 2016, 11:07 AM
david
0
for loop .work whenever expression is true.but 0 equality false. so there nothing to do in blacket.
24th Jul 2016, 12:33 PM
Natthaphat Jitrattanasophit
Natthaphat Jitrattanasophit - avatar