Why is the GOTO statement re-used in C #?

Source: Internet
Author: User
Tags case statement
For goto statements, it is no longer recommended in C/C ++, and there is no goto in Java. Why does the GOTO statement be re-used in C?

First, let's take a look at the usage of the GOTO statement in C:
1) The basic function is to execute the statement in the place where the user jumps to the label as before.

// Exp1 valid Code
{

{

Goto labela;

}
Labela:

}

2) but in C #, Goto can only jump out of one statement block, or jump to another place in one statement block;
3) It is not allowed for a goto to jump out of a block into a block. // Exp2.1 valid Code
// Goto jump in the same statement Block
{

{

Goto labela;


Labela:

}


}


// Exp2.2 Invalid code
// Goto jump from the block to the block [Error]
{

{

Goto labela; // error. cannot jump into a statement Block

{

Labela:

}
}


}

4) A valid statement must be followed after the label corresponding to goto, that is, the label cannot be placed at the end of a statement block. // Exp3 Invalid code
{

{

Goto labela;

}

Labela: // error: labela should follow a valid statement.
}

The above is the basic usage of the GOTO statement, and the only useful part of goto in C # is to use it in conjunction with the switch statement. In other places, remember that the GOTO statement is not recommended.
Let's take a look at the syntax of the switch statement: www. elivn. comswitch (expression)
{
Case 1:
.. // (1) Error Syntax: No break statement is added to jump out
Case 2:
..
Break;
Case 3: // (2) Correct syntax: No break statement exists.
Case 4:
.
Break;
Default:
.
Break;
}
// Compare the two cases above without the break statement at the end,
// However, It is correct and incorrect. We can see that (1) the case is followed by a statement, and (2) it is not followed by any statement.
// In this way, we can know that in C #, the use of (1) is not allowed, and the use of (2) is allowed.
// That is to say, if the previous case statement is not followed by any valid statement, it will automatically jump into the next case,
// However, if the previous case statement is followed by any valid statement, you cannot directly jump into the following case.
// ▲Here the GOTO statement is needed to jump into the next case.

Switch (expression)
{
Case 1:
..
Goto case 2; // (1) Correct syntax
Case 2:
..
Break;
Case 3: // (2) Correct syntax: No break statement exists.
Case 4:
.
Break;
Default:
.
Break;
}

// Actually, The Goto here also acts as a break,
// Here, the switch statement of C # does not allow traversing between case and case. You can only use the GOTO statement to implement this function.
// As to the definition, it should be the programming habits of some masters of Ms.
// Ps. C # allows the flow control of the switch part that contains 0 statements to continue (traverse) The next switch part.

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.