+ 1

What is the difference between while used before loop and after loop?

20th Sep 2016, 12:32 PM
Geek
Geek - avatar
1 Answer
+ 2
In while loops (i.e while used before the body of the loop) the loop will continue to execute as long as the given condition is true. But if the condition is false initially then the while loop won't execute. However in a do while loop in which the condition is given after the body of the loop, even if the condition is false initially the loop still will be executed for one time. For example: int a = 5; while (a < 5) // condition is tested before loop body { cout << "Loop body"; } cout << "Out of the loop"; return 0; output : Out of the loop int a =5; do { cout << "Loop body"; } while(a<5); // Condition is tested after the loop body so it runs at least once cout << endl << "Out of the loop"; return 0; Output : Loop body Out of the loop
20th Sep 2016, 4:48 PM
Mohammed Maaz
Mohammed Maaz - avatar