Well-off to accompany you to learn Java--------Continu3 and break comparison

Source: Internet
Author: User

This article will be a comparison of CONTINU3 and break with you.

In the Java language, there are a number of skipped statements, such as break, continue, and so on, which stand in the perspective of structured programming and are not encouraged, because these jump-off statements add difficulty in debugging and reading. Therefore, it is recommended that you do not use them unless you are in some circumstances. In this article, the break and continue statements are described.

Break Statement

The break statement forces the program to jump out of the loop, and when the program executes to the break statement, it leaves the loop, resumes execution of the next statement outside the loop, and if the break statement appears in the inner loop of the nested loop, the break statement skips only the loop from the current layer. As an example of a for loop, when there is a break statement in the body of a loop, when the program executes to break, it leaves the loop body and continues to execute the statement of the outer layer of the loop.

Take the following program as an example, using the For loop to output the value of the loop variable i, when I divided by 3 the remainder is 0 o'clock, that is, the break statement using the Skip loop, and the end of the program to output the final value of the loop variable i.

01//The following procedure describes how to use break

public class Testjava3_32

03 {

public static void Main (string[] args)

05 {

int i;

07

for (i=1;i<=10;i++)

09 {

if (i%3 = = 0)

Break one by one; Jump out of the whole loop body

System.out.println ("i =" +i);

13}

SYSTEM.OUT.PRINTLN ("cyclic interruption: i =" +i);

15}

16}

Output Result:

i = 1

i = 2

Cyclic interrupt: i = 3

Program Description:

1, the 9th to 13th behavior Cycle body, I is the control variable of the loop.

2, when the i%3 is 0 o'clock, conforms to the if condition judgment, namely executes the 11th line the break statement, jumps off the entire fo cycle. In this example, when the value of I is 3 o'clock, the remainder of 3%3 is 0, the condition of the if is judged, and the For loop is executed, line 14th: The value of the loop control variable I at the end of the output loop is 3. Usually the designer sets a condition that no longer executes the loop body when the condition is set. So when a break statement occurs in a loop, the If statement usually appears at the same time.

Continue Statement

The Continue statement forces the program to jump to the beginning of the loop, and when the program runs to the continue statement, it stops running the rest of the loop body, but goes back to the beginning of the loop to continue running. As an example of a for loop, there is a continue statement in the body of the loop, and when the program executes to continue, it goes back to the start of the loop and proceeds to the partial statement of the loop body.

The program Testjava3_33.java is formed by changing the break statement in the program TESTJAVA3_32 to the continue statement. You can observe the differences between these two types of jump statements. The break statement is a jump away from the current layer loop, and the continue statement is the starting point for returning to the loop. The program looks like this:

01//The following procedure describes how to use the Continue

public class Testjava3_33

03 {

public static void Main (string[] args)

05 {

int i;

07

for (i=1;i<=10;i++)

09 {

if (i%3==0)

One by one continue; Jump out of one cycle

System.out.println ("i =" +i);

13}

SYSTEM.OUT.PRINTLN ("cyclic interruption: i =" +i);

15}

16}

Output Result:

i = 1

i = 2

i = 4

i = 5

i = 7

i = 8

i = 10

Cyclic interrupt: i = 11

Program Description:

1, the 9th to 13th behavior Cycle body, I is the cyclic control variable.

2, when i%3 is 0 o'clock, conforms to the if condition judgment, namely executes the continue statement of line 11th, jumps away from the current for loop (no longer executes the other statements in the loop body), but goes back to the beginning of the loop to judge whether the loop is executed. In this example, when the value of I is 3, 6, 9 o'clock, the remainder is 0, conforms to the if judgment condition, leaves the current layer of the For loop, and then goes back to the beginning of the loop to determine whether the loop is executed.

3, when the value of I is 11 o'clock, does not conform to the conditions of the loop execution, this time the execution of the program line 14th: The output loop at the end of the loop control variable i value 11. When the judging condition is established, the break statement and the continue statement will have different execution methods. The break statement leaves the loop no matter what the situation is, and the continue statement no longer executes the remainder of the loop and goes directly to the beginning of the loop.

Related Article

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.