+ 2

Difference between for and while loop

4th Aug 2016, 5:11 PM
mounika
mounika - avatar
5 odpowiedzi
+ 2
the big difference is that a while loop is used when you don't know how long you want a condition to run (usually )the for loop is used when you have an idea of how long it should be executed (usually )
4th Aug 2016, 8:10 PM
destro
+ 2
tnq all
5th Aug 2016, 1:41 AM
mounika
mounika - avatar
+ 2
Both are entry controlled loop but the difference is in syntax while does not allow us to initialize variable but in case of for loop we can initialize the variable and also allow us to increment or decrement. second is , in while we dont know how many times our while loop will be executed but in case of for loop we know that initially how many times it will be executed.
5th Aug 2016, 7:35 AM
Vishal Koshti
Vishal Koshti - avatar
0
for (int a = 0; a < 10; a++) { cout << a << endl; } the for loop allows you to initialise your counter, set your condition and set you increment all in the head of the statement.
4th Aug 2016, 5:40 PM
Alexander Drinkwater
Alexander Drinkwater - avatar
0
in for -loop it is mandatory that the output will print for the first time and then the second time it matches/checks the condition.On the other hand in While-Loop, the condition is first checked and after that it print the output on the screen.
6th Aug 2016, 8:40 AM
Akshay Khandka
Akshay Khandka - avatar