While Loops
While Statement Loops
It is possible for a while
loop to stand without the do
. Including the do
guarantees that the statement inside the while
will process once. It is wise to take care with stand-alone while
loops, as they have a natural tendency to go infinite.
The while
loop executes its statements if the designated condition (i.e. n < 3
) reads as TRUE. If the condition reads FALSE, the statement within the loop stops executing and control passes on.
The condition is checked before the statement (i.e. n++; x += n;
) is executed, at which point, the condition is checked again.
If the condition returns FALSE, execution stops and control passes on before the statement is executed.
To execute multiple statements, use a block statement ({...}
) to group.
Difference Between For and While Loops
Last updated