Control Flow: Conditional (while
) Loops
while
Loops
while (condition):
Code Statements
The while
loop is similar to the if
control statement, except that the inner code block is executed repeatedly as long as the condition statement evaluates to True
.
Similar to the for
loop, while
loops can also be nested inside other while
and for
loops. The for
loops can be nested inside the while
loops too.
The break
statement can be used to stop repeating the loop immediately. Code execution will continue with the first statement following the loop body.
The continue
statement will cause the loop to repeat the loop body with the next iteration, immediately abandoning the remaining code statements in the current loop iteration.
break
and continue
can be used with both for
and while
loops.