0
What is difference in working of while, do while and For loop
2 Respostas
+ 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.
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