Back to questions
JuniorTheoryCommon
What does the program print?
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue;
} else if (i > 7) {
break;
}
System.out.print(i);
}
}
}1357
- Expecting
13579:breakends the loop at once, before the rest of the body runs, so 9 is never printed. - Expecting
1:continuedoes not leave the loop; it skips the rest of this lap, and the loop goes on with the nexti.
- What would the program print if
continuewere replaced withbreak? - Why can
continueturn awhileloop into an endless one?