(original) C # learning note 04--flow control 03--Branch 03--switch statement

Source: Internet
Author: User
Tags case statement

4.3.3 Switch statement

The switch statement is very similar to an if statement because it is also conditionally executing code based on the value of the test. However, a switch statement can compare a test variable to more than one value at a time, rather than testing only one condition. This test is limited to discrete values , not clauses like "greater than X", so it's a bit different, but it's still a powerful technique.

The basic structure of the switch statement is as follows:

Switch (<testVar>)

{

Case <comparisonval1>:

<code to execute if <testVar> = = <comparisonVal1> >

Break

Case <comparisonval2>:

<code to execute if <testVar> = = <comparisonVal2> >

Break

...

Case <comparisonvaln>:

<code to execute if <testVar> = = <comparisonValN> >

Break

Default

<code to execute if <testVar>! = comparisonvals>

Break

}

The values in <testVar> are compared with each <comparisonValX> value (as specified in the case statement), and if there is a match, the statement provided for that match is executed . If there is no match, execute the code in the default section.

After you have executed the code in each section, you need another statement break. After executing a case block, it is illegal to execute the second case statement.

The break statement here interrupts the execution of the switch statement and executes the statement following the structure.

Note: Here, C # differs from C + + in that, in C + +, you can run another case statement after you run a case statement.

In C # code, there is also a way to prevent a program flow from transferring from one case statement to the next case statement. Even with the return statement , the operation of the current function is interrupted rather than just the execution of the switch structure (see Chapter 6th). You can also use the goto statement , as described earlier, because the case statement is actually a label defined in C # code.

Once a case statement has been processed, it is not free to enter the next cases statement, but there is one exception to this rule. If you put multiple case statements together (stacking them) and then add a block of code, you are actually checking multiple conditions at once. if any of these conditions are met, the code is executed , for example:

Switch (<testVar>)

{

Case <comparisonval1>:

Case <comparisonval2>:

<code to execute if <testVar> = = <comparisonVal1> or <testVar> = = <comparisonVal2> >

Break

...
Note that these conditions are also applied to the default statement.

Each <comparisonValX> must be a constant value. One way is to provide the literal value

Switch(myinteger) { Case 1: <code to executeifMyinteger = =1> Break;  Case-1: <code to executeifMyinteger = =-1> Break; default: <code to executeifMyinteger! = comparisons> Break; } 

Another way is to use constants. constants are the same as other variables, but there is one important difference: They contain a fixed value .

Declaring constants requires specifying the variable type and the keyword const, and they must be assigned a value.

Const int 2

If you write the following code:

Const int  2

A compilation error is generated. If you attempt to change the value of a constant in any way after the initial assignment, a compilation error will also occur.

The following example shows the following switch statement using the following code:

Static voidMain (string[] args) { Const stringMyName ="Karli"; Const stringSexyname ="Angelina"; Const stringSillyname ="Ploppy"; stringname; Console.WriteLine ("What is your name?"); Name=console.readline ();Switch(name. ToLower ()) { CaseMyName:Console.WriteLine ("You have the same name as me!");  Break;  CaseSexyName:Console.WriteLine ("My, what a sexy name have!");  Break;  CaseSillyName:Console.WriteLine ("That ' s a very silly name.");  Break; } Console.WriteLine ("Hello {0}!", name); Console.readkey (); } 

The results of the operation are as follows:

(original) C # learning note 04--flow control 03--Branch 03--switch 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.