Use continue to end this cycle

Source: Internet
Author: User

4.4.2 Use continue to end this cycle

The Continue function is a bit like a break, except that continue just stops the loop and then starts the next one. And the break is a complete termination of the loop. It can be understood that the role of continue is to skip over the remaining statements in the secondary loop and start a new loop again. For example, the following program demonstrates the use of continue:

List of programs: Codes/04/4-4/testcontinue.java public
class testcontinue
{public
static void Main (string[] args) 
{
//A simple for loop for
(int i = 0; i < 3; i++)
{
System.out.println ("I's value is" + i);
if (i = = 1)
{
//Skip the remaining statements of this cycle
continue;
}
SYSTEM.OUT.PRINTLN ("output statement after Continue");}
}

Run the above program and see the following results:

I's value is 0
continue after the output statement
I value is 1
i the value is 2
continue after the output statement

From the results above, when I equals 1 o'clock, the program does not output the "Continue output statement" string, because when the program executes to continue, the code behind the continue statement in the secondary loop is ignored. In this sense, if you put a continue statement on the last line of a single loop, the continue statement doesn't make any sense-because it simply ignores a blank and ignores any program statements.

Similar to the break, continue can also be followed by a label that directly ends the cycle of the loop identified by the label, and starts the next loop again. For example, the following code:

List of programs: Codes/04/4-4/testcontinue2.java public
class TestContinue2
{public
static void Main (string[] args) 
{
//outer Loop
outer: for
(int i = 0; i < 5; i++)
{
//inner loop for
(int j = 0; J < 3; J +) The
value of c11/>{System.out.println ("i) is:" + i + "J" is: "+ j";
if (j = = 1)
{
//jump out of the loop specified by the outer label.
continue outer
}}
}
}

Running the above program, you can see that the value of the loop variable will not exceed 1, because whenever J equals 1, the continue outer statement ends the cycle of the outer loop, starting the next loop directly, and the inner loop does not have the opportunity to perform the completion.

Like a break, a continue tag must also be a valid label, that is, the label should normally be defined before the outer loop of the continue's loop.

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.