Back to questions
JuniorTheoryCommon
What does the program print?
public class Main {
public static void main(String[] args) {
outer: for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (i * j == 4) {
break outer;
}
System.out.print(i * j);
}
}
}
}1232
- Expecting
1232369: abreakwith a label ends the loop that the label marks, here the outer one, so the laps fori = 3never run. - Expecting an endless loop: a label is not a place to jump back to;
break outerleaves the marked loop and goes on after it.
- What would the program print with
continue outerin place ofbreak outer? - How else can you leave two nested loops at once without a label?