1.4c # condition judgment,

Source: Internet
Author: User

1.4c # condition judgment,

C # three basic condition judgment statements: 1.if 2. if... else... 3. switch

1.5.1if Structure

In c #, the if structure syntax is exactly the same as that in java, that is

1.

If (expression)

{

Code block

}

2.

The structure of if... else... is as follows:

If ()

{

Code Block 1

}

Else

{

Code Block 2

}

3.

The multi-if structure is as follows:

If (expression 1)

{

Code Block 1

}

Else if (expression 2)

{

Code Block 2

}

Else if (expression 3)

{

Code block 3

}

......

Else

{

Code block

}

4. The nested if structure is to embed the if structure in the if structure, that is

If (expression 1)

{

If (expression 2)

{

Code Block 1

}

Else

{

Code Block 2

}

}

Else

{

Code block 3

}

Example

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace HellowWorld 8 {9 /// <summary> 10 // The program outputs a message to the console 11 /// </summary> 12 /// <param name = "args"> </param> 13 class Program14 {15 // program entry 16 static void Main (string [] args) 17 {18 int price = 4000; // original ticket price 19 int month; 20 int type; 21 string info1 = "please input the mo Nth when you go out: 1 ~ 12 "; 22 string info2 =" please choose first-class or economy-class? First-class 1, economy-class 2 "; 23 string info3 =" your plane ticket price is {0} $ "; 24 Console. writeLine (info1); 25 month = int. parse (Console. readLine (); 26 Console. writeLine (info2); 27 type = int. parse (Console. readLine (); 28 if (month> = 5 & month <= 10) // peak season 29 {30 if (type = 1) // first class 31 {32 Console. writeLine (info3, price * 0.9); 33} 34 else if (type = 2) // economy class 35 {36 Console. writeLine (info3, price * 0.75); 37} 38} 39 else // off-season 40 {41 if (type = 1) // First Class 42 {43 Console. writeLine (info3, price * 0.6); 44} 45 else if (type = 2) // economy class 46 {47 Console. writeLine (info3, price * 0.3); 48} 49} 50 Console. readLine (); 51} 52} 53}
View Code

Running result

Note:

1. The condition of the inner if is determined only when the outer if condition is met.

2. The else matches the one closest to it that lacks the else if.

Specifications:

1. To make the if structure clear, enclose the statements contained in each if or else in braces.

2. a matching pair of if and else should be left aligned

3. The if structure of the inner layer must be indented to the if structure of the outer layer.

1.5.2switch Structure

Java C #

Switch (int/char expression)

{

Case constant expression 1:

Statement 1;

Break; // No

Case constant expression 2:

Statement 2;

Break; // No

......

Default:

Statement n;

}

Switch (int/char/string expression)

{

Case constant expression 1:

Statement 1;

Break; // required

Case constant expression 2:

Statement 2;

Break; // required

......

Default:

Statement n;

Break; // required

}

Switch (int/char/string expression)

{

Case constant expression 1:

Case constant expression 2:

Statement 2;

Break; // required

......

Default:

Statement n;

Break; // required

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Example

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace HellowWorld 8 {9 /// <summary> 10 // The program outputs a message to the console 11 /// </summary> 12 /// <param name = "args"> </param> 13 class Program14 {15 // program entry 16 static void Main (string [] args) 17 {18 string name1 = "Jack"; 19 string name2 = "Tom"; 20 string subject1 = "c #"; 21 string subj Ect2 = "java"; 22 int score1 = 91; 23 int score2 = 89; 24 Console. WriteLine ("please choose to output which person's information, Jack or Tom? "); 25 string choice = Console. readLine (); 26 Console. writeLine ("{0} \ t {1} \ t {2}", "name", "subject", "score"); 27 switch (choice) 28 {29 case "Jack": 30 Console. writeLine ("{0} \ t {1} \ t {2}", name1, subject1, score1); 31 break; 32 case "Tom": 33 Console. writeLine ("{0} \ t {1} \ t {2}", name2, subject2, score2); 34 break; 35 default: 36 Console. writeLine ("sorry, there is no the person you search! "); 37 break; 38} 39 Console. ReadLine (); 40} 41} 42}
View Code

Running result

Errors are often caused by forgetting to Write break. In vs, you can click menu tools> Options> text editor> c # and select the row number displayed on the right.

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.