Usage of break,continue statements in Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.