While loop, for loop and do loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

While loop, for loop and do loop

What are the significant differences between those loops?

5th Jun 2017, 9:10 AM
Elias Fzada
Elias Fzada - avatar
3 Answers
+ 3
We use FOR to iterate over a known range of values and WHILE to evaluate an expression. for (initialization; termination; increment) { statement(s) } while (expression) { statement(s) } The difference between WHILE and DO WHILE is that on WHILE it checks the condition first, than execute the code, on DO WHILE it execute at least once and then check the condition. do { statement(s) } while (expression);
5th Jun 2017, 9:56 AM
Felipe Cruz
Felipe Cruz - avatar
+ 2
In while loops you can check some condition before starting each iteration and it runs while the condition is true. In do...while loops you check the condition after each iteration. Thus do...while loops always run at least once. for loops are more suitable when you have some iterator or variable(s) which value(s) need to be changed or incremented, decremented after each iteration. For example when iterating through arrays or collections.
5th Jun 2017, 10:10 AM
Aibek T.
Aibek T. - avatar
+ 1
Thanks a lot for the answer guys!!
5th Jun 2017, 10:29 AM
Elias Fzada
Elias Fzada - avatar