C#_ judgment [switch statement]:[c# get started classic]

Source: Internet
Author: User

A switch is a statement that is very similar to an if statement. However, a switch allows a condition to have multiple values.

The basic structure of the program is as follows

Switch (textval)

{

Case Val:

Program code

Break

Case VAL2:

Program code

Break

Case VAL3:

Program code

Break

......

Case VALN:

Program code

Break

}

A switch is a statement that consists of curly braces, which contain case and break in parentheses.

Where C # requires that a case must have a break. A case cannot jump directly to the next case.

Break means to jump out, when the value of the case conforms to the switch condition value, enters into the value of the suit and executes the code, executes the break, jumps out of the switch statement, and executes other program code. There can be countless case, the machine is not tired anyway.

Flowchart arrows to the right are eligible and do not conform downward.

The code is a bit of a brain remnant, but the meaning is right. Simple, clear.

        intA = -, B = One; Switch(A +b) { Case Ten: Console.WriteLine ("correct");  Break;  Case  -: Console.WriteLine ("correct");  Break;  Case Panax Notoginseng: Console.WriteLine ("correct");  Break; }

There is a keyword in switch, default. The code within the default is executed without a matching case value in switch. The code is as follows:

 intA = -, B = One; Switch(A +b) { Case Ten: Console.WriteLine ("correct");  Break;  Case  -: Console.WriteLine ("correct");  Break;  Case  -: Console.WriteLine ("correct");  Break; default: Console.WriteLine ("None of them are right.");  Break; }

Okay, so that's the switch base.

C#_ judgment [switch statement]:[c# get started classic]

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.