Difference Between Continue and Next Sentence

break

  1. Usage scenario: used in various loop statements to jump out of the loop

  2. effect:

    • Jump out of the loop where the break is (if there are multiple loops, you can only jump out of the inner layer)

                        public class A{     public static void main(String args[]){         int a = 60;         switch(a){             case 60:                 System.out.println("Pass");                 break;             case 80:                                  System.out.println("Excellent");             case 100:                                  System.out.println("Full score");             case default:                 System.out.println("default");         }     } }  //1. Output result: Pass  /*2. If there is no break in each case, after the case is matched correctly, the next sentence will still be output.  Output result (after removing break):    Pass    Excellent    Full marks  default */                                  
                        public class A {       public static void main(String args[]){            for(int i = 1; i < 6; i++){                for(int j = 1; j < 6; j++){                    System.out.print("haha" + " ");                    break;                }                                System.out.println(" " + i + " ");            }        }    }        /*1. Output result:                haha first time                haha 2nd time                haha 3rd time                haha 4th                haha 5th time    */        /*2. If there is no break in the inner loop, the inner loop ends and starts from the outer loop                haha haha ​​haha ​​haha ​​haha ​​1st time                haha haha ​​haha ​​haha ​​haha ​​2nd time                haha haha ​​haha ​​haha ​​haha ​​3rd time                haha haha ​​haha ​​haha ​​haha ​​4th time                haha haha ​​haha ​​haha ​​haha ​​5th time     */                                  
    • If you want to break out of the multi-level loop, you need to use it with the label statement

      • Label sentence format (label name must conform to Java naming rules, reserved words cannot be used) ----- label name: ---- such as Outer:

                              public class A {       public static void main(String args[]){           Outer:            for(int i = 1; i < 6; i++){                Inner:                for(int j = 1; j < 6; j++){                    System.out.print("haha" + " ");                    break Outer;                }                                System.out.println(" " + i + " ");            }        } }  /*Output result: 	haha   Successfully break out of the two-layer cycle */                                          

continue

  1. Usage scenario: in a loop statement

  2. Function: Interrupt the current cycle and proceed to the next cycle.

                  public class B {       public static void main(String args[]){            for(int i = 1; i < 6; i++){                if(i == 4) continue;                                System.out.println(" " + i + " ");            }        } }  /*Output result          1st           2nd           the 3rd time           5th            Skip i=4 directly and enter the next loop */                          

    It is worth noting that continue cannot be used in a single switch statement (compilation errors will occur and it is meaningless), but it can be used in a switch of a loop statement. Proceed directly to the next cycle.

                  public class B {       public static void main(String args[]){           int a = 80           boolean flag = false;            while(flag == false){                flag = true;                 switch(a){                     case 60:                                                  System.out.println("Pass");                     case 80:                                                  System.out.println("Excellent");                         continue;                     case 100:                                                  System.out.println("Full score");                     case default:                         System.out.println("default");                 }            }       } }  /*Output result 	 Excellent 	 	 When the statement reaches case: 80, directly exit the current loop */                          

return

  1. Usage scenario: within the method

  2. Role: Exit the current method

                  public class A {       public static void main(String args[]){            for(int i = 1; i < 6; i++){                for(int j = 1; j < 6; j++){                    System.out.print("haha" + " ");                    if(j == 2) return;                }                                System.out.println(" " + i + " ");            }           System.out.println("end");        } }  /*Output result 	haha 	          Do not execute the statement after the for loop, and jump out of the current method directly. */                          

Break Continue Return Difference

Differences between Break Continue Return Keyword position effect break Switch and cycles End cycle continue cycle End this cycle, carry out the next cycle return Use anywhere in the method End method...

The difference between break and continue

break terminates the current loop: break ---- must be used in a loop break ---- Terminate the current loop and the code below the break will not be executed continue Jump out of this loop and continue...

The difference between break and continue

Break can leave the current block of switch, for, while, do while, and advance to the next statement after the block. In switch, it is mainly used to interrupt the comparison of the next case. In for,...

The difference between continue and break

  Break is to jump out of the current loop (not the entire loop) E.g: The printed effect is like this:     Continue is to jump out of this loop and execute the next loop       can write such a co...

The difference between break and continue

(1) break jumps out of the innermost loop and terminates (2) continue just terminates this cycle, and then executes the following cycle   sum=6; Execute break End of cycle  ...

murdockpowle1955.blogspot.com

Source: https://www.programmerall.com/article/6587528470/

0 Response to "Difference Between Continue and Next Sentence"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel