What is difference in working of while, do while and For loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is difference in working of while, do while and For loop

10th Sep 2017, 2:44 PM
Yashvardhan Singh
Yashvardhan Singh - avatar
2 Answers
+ 4
do while is always done at least one time, because a condition is gonna be checked at the end if loop code. while is a loop with a condition, where we don't actually know how much iterations it is going to be. in for loop we add conditions with steps and bounds, so we can predict amount of iterations.
10th Sep 2017, 3:07 PM
Топчий Анастасия Александровна
Топчий Анастасия Александровна - avatar
0
A While loop will execute its statement as long as its condition remains true. int x = 0; while(x <=10){ statement; } This While loop will run as long as x <= 10 meaning it will run forever as x will always be less than 10, so you need to end it by making i false. One way is by adding x += 1; so every time it runs the loop x will plus 1 until x is not smaller than 10, which then ends the loop. Do while loop is simmilar to while except it will run its statement regardless if the condition is false. For loop is similar again to while loop except you can add an initilizaion and an increment (x += 1) For loop is usually use to loop a specific amount of time. example for (int x = 0; x < 10; x++){ run this code } it will run the code 10 times x++ is same as x += 1
10th Sep 2017, 3:25 PM
jayrn02
jayrn02 - avatar