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.
break语句不知道它在一个开关或循环语句中。编译器验证break语句是否在一个开关或循环语句中。如果遇到不在循环语句中的中断语句,它将发出编译时错误。
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.
如果编译器能够验证中断语句是否在一个开关或循环语句中,那么它将发出JVM指令,以便在最接近的循环循环之后立即突然跳到第一个语句。
for(int i = 0; i < 10; i++) {
if(i % 2 == 0) {
break;
将由编译器翻译成:
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
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.break语句不知道它在一个开关或循环语句中。编译器验证break语句是否在一个开关或循环语句中。如果遇到不在循环语句中的中断语句,它将发出编译时错误。If no s...
public int findRepeatNumber(int[] nums) {
int n=-1;
lableA:for(int i=0;i<nums.length-1;i++){
for(int j=i+1;j<=nums.length-1;j++){
if(nums[i
java
中已知的三种跳出多重循环的方式:
System.out.println("---------
java
中跳出多重循环的三种方式:---------");
System.out.println("---------第一种,使用带有标号的的break语句---------");
String a1 = "";
String b1 = "";