+ 2
Difference between for and while loop
5 Answers
+ 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 )
+ 2
tnq all
+ 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.
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.
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.