"Java" Java_16 control loop structure break, Continue, Return

Source: Internet
Author: User

1.break

Break is used to completely end a loop and jump out of the loop body. Regardless of the loop, once a break is encountered in the loop body, the system will completely end the loop

In Java is the label definition, the tag is a colon immediately following the English (:) identifier

CODE schematic:

 Packagecom.oliver.test; Public classTestbreak { Public Static voidMain (string[] args) {//outer Loop, outside as identifierOutside: for(inti=0;i<=5;i++){            //Memory Loops             for(intj=0;j<=5;j++) {System.out.println (The value of "I" is "+i+" and the value of J is "+j); //if the value of J is 4, then the outer loop is ended                if(j==2){                     Breakoutside; }            }        }    }}

Printing results:

The value of I is 0,j with a value of 0
The value of I is 0,j with a value of 1
The value of I is 0,j with a value of 2

2.continue

Continue function and break a bit similar, the difference is that continue just ignore the remainder of the loop, and then start the next loop, does not terminate the loop, and break is to completely terminate the loop itself.

CODE schematic:

 Packagecom.oliver.test; Public classTestcontinue { Public Static voidMain (string[] args) {//outer Loop, outside as identifierOutside: for(inti=0;i<=5;i++){            //Memory Loops             for(intj=0;j<=5;j++) {System.out.println (The value of "I" is "+i+" and the value of J is "+j); //if the value of J is 2, then the outer loop is ended                if(j==2){                    Continueoutside; }            }        }    }}

Printing results:

The value of I is 0,j with a value of 0
The value of I is 0,j with a value of 1
The value of I is 0,j with a value of 2
The value of I is 1,j with a value of 0
The value of I is 1,j with a value of 1
The value of I is 1,j with a value of 2
The value of I is 2,j with a value of 0
The value of I is 2,j with a value of 1
The value of I is 2,j with a value of 2
The value of I is 3,j with a value of 0
The value of I is 3,j with a value of 1
The value of I is 3,j with a value of 2
The value of I is 4,j with a value of 0
The value of I is 4,j with a value of 1
The value of I is 4,j with a value of 2
The value of I is 5,j with a value of 0
The value of I is 5,j with a value of 1
The value of I is 5,j with a value of 2

3.return

The return keyword is used to end a method and is not intended to end the loop.

CODE schematic:

 Package com.oliver.test;  Public class Testreturn {    publicstaticvoid  main (string[] args) {        //  a simple for loop for        (int i=0;i<=3;i++) {            System.out.println ( the value of "I" is: "+i            ); if (i==2) {                return;     }}}

Printing results:

The value of I is: 0
The value of I is: 1
The value of I is: 2

"Java" Java_16 control loop structure break, Continue, Return

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.