Continue 和 Break

1. What is continue and break

continue: continue, continue

break: break, break

Function: Used to terminate the loop body, mainly used for switch conditional statements and loop body conditional statements (for, while, do-while)

 

2. continue: The execution of the statement will terminate this loop and enter the next loop in advance

(1) Case 1: When the conditions are met, enter the next cycle in advance

public class test { 
    public static void main (String [] args) { 
        int i; 
       for (i = 0; i <= 10; i ++) 
       { 
           if (i == 5) // when i = 5, it will be skipped This cycle, enter the next cycle in advance i = 6, that is, when i = 5, the following statement will not continue to execute, and will not output 5 
           { 
               continue; 
           } 
           System.out.print (i + "\ t"); 
       } 

    } 

} 

Output result: 0 1 2 3 4 6 7 8 9 10	

 

 

(2) Case 2: When two or more layers of for loops are nested, use continue, and when the conditions are met, the statement after continue is no longer executed, and the next loop is entered in advance

public class test { 
    public static void main (String [] args) { 
        int i, j; 
       for (i = 0; i <= 3; i ++) 
       { 
           for (j = 0; j <= 3; j ++) 
           { 
               if ( j == 2) {// When j = 2, the statement after continue will not continue to execute, enter the next loop body in advance, that is j = 3, the value when j = 2 will not be taken to 
                   continue; 
               } 
               System. out.println ( "first" + i + "layer cyclic:" + i + "+" + J + "=" + (I + J)); 
           } 
       } 

    } 

} 

The result: 

layer 0 cycle: 0 + 0 = 0 
The first layer 0 cycle: 0 + 1 = 1 
0 layer cycle: 0 + 3 = 3 
level 1 cycle: 1 + 0 = 1 
Tier 1 cycle: 1 + 1 = 2 
the first layer 1 cycle: 1 + 3 = 4 
of Layer 2 cycle: 2 + 0 = 2 
Layer 2 cycle: 2 + 1 = 3 
Layer 2 cycle: 2 + 3 = 5 
Layer 3 cycle: 3 + 0 = 3
Layer 3 loop: 3 + 1 = 4 
Layer 3 loop: 3 + 3 = 6

 

    

3. break: The execution of the statement does not end the entire method, mainly to terminate the loop body that manages it most recently

(1) Case 1: When there is only one for loop, terminate its loop body

public class test { 
    public static void main (String [] args) { 
        int i; 
       for (i = 0; i <= 10; i ++) 
       { 
           if (i == 5) // if i = 5, stop leaving him The most recent loop body, that is, the current for loop 
           { 
               break; 
           } 
           System.out.print (i + "\ t"); 
       } 

    } 

} 

Output result: 0 1 2 3 4

 

 

(2) Case 2: When two or more layers of for loops are nested, use break to terminate the loop body closest to it

public class test { 
    public static void main (String [] args) { 
        int i, j; 
       for (i = 0; i <= 3; i ++) 
       { 
           for (j = 0; j <= 3; j ++) 
           { 
               if ( j == 2) {// When j = 2, terminate the inner for loop and enter the outer for loop, that is, the values ​​obtained by the inner loop are only 0 and 1 
                   break; 
               } 
               System.out.println ("第" + i + "layer loop:" + i + "+" + j + "=" + (i + j)); 
           } 
       } 

    } 

} 


Output result: 

layer 0 loop: 0 + 0 = 
0 layer 0 loop: 0 + 1 = 1 
layer 1 cycle: 1 + 0 = 1 
layer 1 cycle: 1 + 1 = 2 
layer 2 cycle: 2 + 0 = 
2 layer 2 cycle: 2 + 1 = 
3 layer 3 cycle: 3 + 0 = 3 
Layer 3 loop: 3 + 1 = 4

  

(3) Case 3: When terminating other loop bodies, you can alias other loop bodies, and add an alias after the break to play the role of terminating other loop bodies

public class test {
    public static void main(String[] args) {
        int i,j;
    a : for (i=1;i<=9;i++)
        {
            for (j=1;j<=i;j++)
            {
                System.out.print(j+"*"+i+"="+i*j+"\t");
                break a; //外层循环取别名为a,即会终止外层循环导致整个嵌套循环体结束
            }
            System.out.println();
        }

    }
}

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/ibear/p/12732799.html