while (condition) {
if (condition) {
statement1;
statement2;
break;
} else {
statement3;
statement4;
}
}
By using break
in the if clause, we ensure the loop is halted and exited.
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, or how it "knows" where to jump to. How does this happen?
The break statement is used to terminate the loop immediately. The continue statement is used to skip the current iteration of the loop. break keyword is used to indicate break statements in java programming. continue keyword is used to indicate continue statement in java programming.
Tips. The break statement exits a for or while loop completely. To skip the rest of the instructions in the loop and begin the next iteration, use a continue statement. break is not defined outside a for or while loop.
The Java break statement is used to break loop or switch statement. It breaks the current flow of the program at specified condition. In case of inner loop, it breaks only inner loop. We can use Java break statement in all types of loops such as for loop, while loop and do-while loop.
Generally break statement breaks out of loops ( for , while , and do... while ) and switch statements.
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
, orfor
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With