Well-off will accompany you to learn JAVA -------- comparison between continu3 and break, javabreak

Source: Internet
Author: User

Well-off will accompany you to learn JAVA -------- comparison between continu3 and break, javabreak

This article compares continu3 with break.

In the Java language, there are some jumping statements, such as break and continue, which are not encouraged from the perspective of structured program design, these skip statements increase debugging and reading difficulties. Therefore, we recommend that you do not use them unless you cannot. This article describes the break and continue statements.

 

BreakStatement

The break statement can force the program to skip the loop. When the program runs the break statement, it will exit the loop and continue executing the next statement outside the loop. If the break statement appears in the inner loop of the nested loop, then, the break statement only skips the cycle of the current layer. Take the for loop as an example. When the program runs break when there is a break statement in the loop body, it will leave the loop body and continue to execute the statement at the outer layer of the loop.

The following program is used as an example to output the I value of the loop variable using a for loop. When the remainder of I divided by 3 is 0, the Skip loop of the break statement is used, and output the final value of the loop variable I before the program ends.

01 // The following program describes how to use break.

02 public class TestJava3_32

03 {

04 public static void main (String [] args)

05 {

06 int I;

07

08 for (I = 1; I <= 10; I ++)

09 {

10 if (I % 3 = 0)

11 break; // jump out of the entire loop body

12 System. out. println ("I =" + I );

13}

14 System. out. println ("cyclic interruption: I =" + I );

15}

16}

Output result:

I = 1

I = 2

Cyclic interruption: I = 3

 

Program description:

1. 9th ~ 13. The subject of the behavior loop. I is the control variable of the loop.

2. When I % 3 is 0, the if condition is met, that is, the 11th line break statement is executed, and the entire fo loop is exceeded. In this example, when the I value is 3, the remainder of 3% 3 is 0. if the condition is met, the for loop is left and the 14th rows are executed: when the output loop ends, the value of loop control variable I is 3. Generally, the designer sets a condition. When the condition is set, the cyclic subject is no longer executed. Therefore, when a break statement appears in a loop, the if statement usually appears at the same time.

 

ContinueStatement

The continue statement can force the program to jump to the starting point of the loop. When the program runs to the continue statement, it stops running the remaining loop entity, but returns to the starting point of the loop to continue running. Take the for loop as an example. The loop body contains a continue statement. When the program runs on the continue, it returns to the starting point of the loop and continues to execute some statements of the loop body.

Change the break statement in TestJava3_32 to the continue statement to form the TestJava3_33.java program. You can observe the differences between the two bounce statements. The break statement is a cycle that skips from the current layer, and the continue statement is the starting point for returning to the loop. The program is as follows:

01 // The following program describes how to use continue

02 public class TestJava3_33

03 {

04 public static void main (String [] args)

05 {

06 int I;

07

08 for (I = 1; I <= 10; I ++)

09 {

10 if (I % 3 = 0)

11 continue; // jump out of a loop

12 System. out. println ("I =" + I );

13}

14 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 interruption: I = 11

 

Program description:

1. 9th ~ 13. The subject of the behavior loop. I is the variable of the loop control.

2. When I % 3 is 0, if-compliant conditions are determined, that is, the 11th-row continue statement is executed, jumping out of the current for loop (other statements in the loop body are no longer executed ), instead, return to the beginning of the loop and continue to judge whether the loop is executed. In this example, when the I value is 3, 6, and 9, the remainder is 0, which meets the if judgment condition and leaves the for loop of the current layer, return to the beginning of the loop and continue to judge whether the loop is executed.

3. When the I value is 11, it does not meet the cyclic execution condition. At this time, the execution program runs row 14th: when the output loop ends, the cyclic control variable I value is 11. When the condition is determined, the break statement and the continue statement have different execution methods. The Break statement leaves the loop first regardless of the situation. The continue statement does not execute the remaining statements of the loop and directly returns to the starting position of the 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.