Back to questions
JuniorTheoryCommon
What does the program print?
public class Main {
public static void main(String[] args) {
int i = 10;
do {
System.out.print(i + " ");
i++;
} while (i < 5);
System.out.println(i);
}
}10 11
- Expecting
10: that is thewhilerule. Ado-whilechecks its condition after the body, so the body runs once even though10 < 5is false. - Expecting an endless loop:
do-whilerepeats while the condition is true, not until it becomes true, so it stops after the first check.
- How would the output change if the
do-whilewere replaced by awhilewith the same condition? - When does a
do-whileloop fit a task better than awhileloop?