Roles of break, continue, and goto

Source: Internet
Author: User

Skip statement

Since break, continue, and goto statements help to jump to a specific statement in the code, they are skip statements. The following describes the three statements.

  ① Break statement

This statement is often used together with the switch statement. However, C # allows you to use it to jump out of the current statement block. We often use the break statement to jump out of a loop statement block.

The following is an application example of the break statement:

Int I = 0;

While (I <6)

{

Response. Write ("the current I value is" I. tostring (). Trim () "<br>");

If (I = 5)

{

Break; // jump out of the loop when I = 5;

}

I;

}

  ② Continue statement

This statement controls the Boolean expression returned to the control loop statement.

The following is an application example of the continue statement:

For (INT I = 1; I <= 6; I ++)

{

If (I <5)

{

Continue; // continue execution if I is less than 5

}

Response. Write ("the current I value is" I. tostring (). Trim () "<br>");

}

  ③ GOTO statement

This statement gives unconditional control to a markup statement. In C #, any statement can be marked. The statement is followed by a colon, a tag identifier, and then a goto keyword. The GOTO statement gives control to the statement named by the tag identifier.

If the GOTO statement is used excessively, the written code may be difficult to read and understand. Generally, it is better to rewrite the code to avoid using the GOTO statement.

The following is an application example of the GOTO statement:

Int I = 0;

While (I <6)

{

Response. Write ("the current I value is" I. tostring (). Trim () "<br>");

If (I = 5)

Goto complete; // If I is equal to 5, the while statement is directly ended at the complete mark;

I;

}

Complete:

Response. Write ("the program jumps directly here to end! ");

Reproduced, Original Source: http://www.amhl.net/wenzhang/DianNao-BianChengKaiFa/20101201/117767.html

Roles of break, continue, and goto

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.