I don't understand how the break statement "knows" that it is within a loop for it to exit out of in the first place.
The break
statement does not know that it's within a switch
or loop statement. The compiler verifies that the break
statement is within a switch
or loop statement. If it encounters a break
statement that is not within a loop statement, it will emit a compile-time error.
If no switch
, while
, do
, or for
statement in the immediately enclosing method, constructor, or initializer contains the break statement, a compile-time error occurs.
If the compiler is able to verify that the break
statement is within a switch
or loop statement, it will then emit JVM instructions to jump abruptly to the first statement immediately after the nearest enclosing loop.
Thus:
for(int i = 0; i < 10; i++) {
if(i % 2 == 0) {
break;
}
}
would be translated by the compiler into:
0: iconst_0 # push integer 0 onto stack
1: istore_1 # store top of stack in local 1 as integer
# i = 0
2: iload_1 # push integer in local 1 onto stack
3: bipush 10 # push integer 10 onto stack
5: if_icmpge 23 # pop and compare top two (as integers), jump if first >= second
# if i >= 10, end for
8: iload_1 # push integer in local 1 onto stack
9: iconst_2 # push integer 2 onto stack
10: irem # pop top two and computes first % second and pushes result
# i % 2
11: ifne 17 # pop top (as integer) and jump if not zero to 17
# if(i % 2 == 0)
14: goto 23 # this is the break statement
17: iinc 1, 1 # increment local 1 by 1
# i++
20: goto 2 # go to top of loop
# loop
23: return # end of loop body