Asp.net switch statement usage (C, C #)

Source: Internet
Author: User
Tags case statement constant goto regular expression

Asp tutorial. net switch statement usage (C, C #)
A switch statement is a control statement that processes multiple choices and enumerations by passing a case statement to the body.
Int caseSwitch = 1;
Switch (caseSwitch)
{
Case 1:
Console. WriteLine ("Case 1 ");
Break;
Case 2:
Console. WriteLine ("Case 2 ");
Break;
Default:
Console. WriteLine ("Default case ");
Break;
}


One:
Int I = 0;

Switch (I)
{
Case 0:
Console. WriteLine ("0 ");
Case 1:
Console. WriteLine ("1 ");
Break;
}
// Here, you want to execute
// Console. WriteLine ("0"), and then "fall through" to the lower layer to execute Case 1.
// However, "fall through" is not allowed in C. Unless none of the "case 0" statements exist.
However, you can use the goto statement to implement "fall through"
 
Int I = 0;
Switch (I)
{
Case 0:
Console. WriteLine ("0 ");
Goto case 1;
Case 1:
Console. WriteLine ("1 ");
Break;
}
In C #, every case xxx is a tag, so you can use the goto statement to redirect.
 
Two:
 

Here, the default statement does not do anything, but does not add break;
The prompt is as follows:

C # strictly control that the branches of each Switch are not allowed to run through ("fall through"). For example, sometimes you may look like the following:

This statement is completely legal.
 
Three:
Sometimes you define variables in case, but the variables may have the same name, for example:

Two identical variables, y, are defined in case 0 and case 1. The compiler prompts the following error:

To solve this problem, add "{}" to convert the Case statement into a block statement.

Of course, there is also an unknown solution, as shown below:

 
 
Four:
Suppose you have a method M and the code is as follows:
Int M (bool B)
{
Switch (B)
    {
Case true: return 1;
Case false: return 0;
    }
}
It is obvious that B has only two values, one being true and the other being false, but the compiler fails. The prompt is:

The compiler considers that every switch structure can be executed and does not execute it. However, method M must have an int return value, so the compiler prompts an error.
The solution to this problem is also simple:
Int M (bool B)
{
Switch (B)
    {
Case true: return 1;
Default: return 0;
    }
}
Or:
Int M (bool B)
{
Return B? 1: 0;
}

Note the following when using the switch statement:

No traversal allowed

C and C ++ allow the case label in the switch statement without the break statement, but C # does not allow this. It requires that each label item use the break statement or the jump statement goto, that is, it is not allowed to automatically traverse from one case to other cases. Otherwise, an error will be reported during compilation.

A program is used to calculate the number of days spent in a year. month indicates the month, day indicates the date, and the calculation result is saved in total. For the sake of simplicity, we excluded the leap year. C and C ++ programmers will use some tips to implement this program:

Total = 365;
Switch (month ){
Case 1: total-= 31;
Case 2: total-= 28;
Case 3: total-= 31;
Case 4: total-= 30;
Case 5: total-= 31;
Case 6: total-= 30;
Case 7: total-= 31;
Case 8: total-= 31;
Case 9: total-= 30;
Case 10: total-= 31;
Case 11: total-= 30;
Case 12: total-= 31;
Default: total + = day;
}
However, this method is not allowed in C. Experienced programmers will use this feature, but it is difficult to ensure that anyone will not forget to add a break statement after case during programming. This will often cause some imperceptible errors. Therefore, in C #, if the label item does not contain a break statement or a jump statement goto, the compiler will require the programmer to add it.

If you think of c and c ++, it is not difficult to traverse other statements after execution. You just need to explicitly add these two jump statements:

● Goto case label: Skip to label statement execution

● Goto default: Jump to the default tag for execution

In this way, we can rewrite the above program:

All 365;
Switch (month ){
Case 1: total-= 31; goto case 2;
Case 2: total-= 28; goto case 3;
Case 3: total-= 30; goto case 4;
Case 4: total-= 31; goto case 5;
Case 5: total-= 30; goto case 6;
Case 6: total-= 31; goto case 7;
Case 7: total-= 30; goto case 8;
Case 8: total-= 31; goto case 9;
Case 9: total-= 30; goto case 10;
Case 10: total-= 31; goto case 11;
Case 11: total-= 30; goto case 12;
Case 12: total-= 31; goto default;
Default: total + = day;
}
While avoiding errors caused by missing break in c and c ++, the principle of "no traversal" also allows us to arrange case items in the switch statement without affecting the function of the switch statement. This article is published in http://bianceng.cn

In addition, in general, each switch item ends with break, goto case, or goto default, but in fact any structure that does not lead to "Traversal" is allowed, for example, throw and return statements can also jump out of control, so the following example is correct:

Switch (I ){
Case 0:
While (true) F ();
Case 1:
Throw new ArgumentException ();
Case 2:
Return;
}
Use a string as a constant expression

VB programmers may be used to using strings as constant expressions, but C and C ++ do not support this. However, another difference between the switch statement of C # and c, c ++ is that C # can use a string as a constant expression. Therefore, the switch statement can be of the string type.

The following example shows how to implement a floating window prompt. In Windows, when we move the mouse over a control for a few seconds, a floating prompt will appear, indicating the role of the control. In this example, the GetButtonCaption method is used to obtain the text on the button. ShowMessage indicates that information is displayed in the floating prompt window.

String text = GetButtonCaption ();
Switch (text)
{
Case "OK": ShowMessage ("save the change and exit"); break;
Case "Retry": ShowMessage ("return and retry"); break;
Case "Abort": ShowMessage ("Abort the change and exit"); break;
Case "Help": ShowMessage ("Get help from system"); break;
}
In fact, in the C language of the old version, the control type of the switch statement can only be an integer expression or a regular expression, while the ANSI standard relaxed this requirement. C # is another extension of the control type of the switch statement. In C #, the reference value of the case label can be nul.

Note:

Controls the case statement that matches the value passed to the switch. The switch statement can contain any number of case instances, but neither case statement can have the same value. The statement body starts execution from the selected statement until break passes the control beyond the case body. Each case block (including the previous one, whether it is a case statement or a default statement) must be followed by a jump statement (such as break ). But there is one exception (different from the C ++ switch statement). C # does not support explicit transfer from one case tag to another. This exception occurs when no code exists in the case statement.

 

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.