Section 5.2 Indefinite Loops
Both Python and Java support the
while loop, which continues to execute as long as a condition is true. Here is a simple example in Python that counts down from 5:
In Java, we add parentheses and curly braces. Here is the same countdown loop in Java:
Java adds an additional, if seldom used variation of the
while loop called the do-while loop. The do-while loop is very similar to while except that the condition is evaluated at the end of the loop rather than the beginning. This ensures that a loop will be executed at least one time. Some programmers prefer this loop in some situations because it avoids an additional assignment prior to the loop. For example, the following loop will execute once even though the condition is initially false.
You have attempted of activities on this page.
