1. Break Statement without Tags:
Public classTESTBREAK1 { Public Static voidMain (string[] args) {intScore,sum=0, I; for(i=0;i<=10;i++) {score=(int) (Math.random () *100); System.out.print (Score+","); if(score<60) Break;//No label formatsum+=score; } System.out.println (); if(i==10 && sum/10>+80) System.out.println ("Team Test Passed"); ElseSystem.out.println ("Team Test does not pass"); }}
2. Break Statement with Tags:
Public classTESTBREAK2 { Public Static voidMain (string[] args) {BooleanIstrue=true; Label: for(inti=0;i<=5;i++){ while(isTrue) {System.out.println ("Hello"); BreakLabel//have a label, exit to the specified location} System.out.println ("While end");//Break jumps out of the for loop, so this sentence does not execute} System.out.println ("For End"); } }
3. Continue statement without tags:
Public classTestContinue1 { Public Static voidMain (string[] args) {intN; N=(int) (Math.random () *100+100);//cast type, random number to takeAll factors of System.out.println (n+ ") include:"); for(inti=1;i<=n;i++){ if(n%i!=0)Continue;//Stop this iteration for the next round of loopsSystem.out.print (i+ ","); } }}
4, continue statement with Tags:
Public classTestContinue2 { Public Static voidMain (string[] args) {label: for(inti=0;i<5;i++){ for(intj=0;j<5;j++) {System.out.println ("Hello"); //if (j>4) return; //return without statement, indicating the execution of the direct End method//continue label; //end this loop and return to the label} System.out.println ("Inner Loop End"); } System.out.println ("Outer Loop End"); } Public StaticString My Method () {return"ABC";//Return value returned with the value returned to the call location }}
Usage of break,continue statements in Java