Joke Collection Website - Joke collection - C# select structure if statement

C# select structure if statement

If statement and switch statement are relatively more important, especially if statement. These two statements are very commonly used in practical development.

If the statement

If statement is a very important choice structure statement, which exists in almost all advanced programming languages. It is one of two operations used by programming languages (including C language, c#, VB, assembly language, etc.). ) judge whether the given conditions are met, and decide to execute the given operation according to the judgment result (true or false).

If statement * * * has three forms.

1.if (conditional statement) {

Statement1;

...;

}

For example, there is a little story about a programmer buying watermelons: his wife called her husband as a programmer and said, "Buy a catty of steamed bread and bring it back from work. When you see a watermelon seller, buy one. " That night, the programmer's husband came into the house with a steamed stuffed bun in his hand ... The wife said angrily, "Why did you buy a steamed stuffed bun?" ! The husband replied, "Because I saw the watermelon seller."

This story, if expressed in IF language, can be expressed in the following code.

int count = 10;

int flag = 0;

Printf ("Did you see the watermelons on sale? 1. Yes; 2. No):);

Scanf("%d ",& flag);

if (flag == 1) {

count = 1;

}

if (flag == 2){

count = 10;

}

Printf ("bought %d steamed buns \n", counting);

2.if (conditional expression) {

Statement 1

} Otherwise {

Statement 2

}

For example: if you lose from the console? Become? A role, if you lose? If you enter m, output men, otherwise output? Woman, how can that be achieved?

Char gender = 0;

Printf ("Please enter a character:");

Scanf("%c ",& gender);

if (gender == 'm') {

printf(" male \ n ");

} Otherwise {

Printf ("female \ n");

}

Nesting of if statements

int height = 0;

Printf ("input height:");

Scanf("%d ",& height);

If (height & gt =160) (

Printf ("Let's talk ~ ~ \ n");

} Otherwise {

Printf ("Sorry, I still want to be a quiet and free handsome guy! \ n ");

}

3.if (conditional expression 1) {

Statement1;

} else if (conditional expression 2) {

Statement 2;

} Otherwise {

Statement 3;

}

For example, enter a grade, if it is greater than or equal to 90, the output is excellent; If it is less than 90 but greater than or equal to 80, the output is good; Less than 80 but greater than or equal to 70, the output is medium; If it is less than 70 but greater than or equal to 60, the output is poor; If it is less than 60, the output fails.

Floating score = 0.0;

Printf ("Please enter your grade:");

Scanf("%f ",& score);

if(score & gt; = 90.00) {

Printf ("excellent \ n");

} else if(score & gt; = 80) {

printf(" good \ n ");

} else if(score & gt; = 70) {

printf(" medium \ n ");

} else if(score & gt; = 60) {

printf(" poor \ n ");

} Otherwise {

Printf ("Failed \ n");

}

Exchange statement

Switch is also a judgment selection code, which is often used with case in C language.

Its expression is as follows:

Switch (integer expression)

Case value 1:

Statement 1

Break;

Case Value 2:

Statement 2

Break;

situation ...

Default value:

Break;

For example, enter the number of months and display the days.

int month = 0;

Printf ("Enter month:");

Scanf("%d ",& month);

int day = 0;

Switch (month) (

Case 1:

Day = 31;

Break;

Case 2:

Day = 28;

Break;

Case 3:

Day = 31;

Break;

Case 4:

Days = 30;

Break;

Case 5:

Day = 31;

Break;

Case 6:

Day = 31;

Break;

Case 7:

Day = 31;

Break;

Case 8:

Days = 30;

Break;

Case 9:

Days = 30;

Break;

Case 10:

Day = 31;

Break;

Case 1 1:

Days = 30;

Break;

Case 12:

Day = 31;

Break;

Default value:

Printf ("Input error, please re-enter");

Break;