1, If-else
Grammar:
if (condition)
{Statement 1;}
Else
{Statement 2;}
Execution process:
If the condition is true, the statement block 1 of the if band is executed and the statement block 2 of the else Band is skipped, and if the condition is false, the statement block 1 of the If band is skipped, and the statement block 2 of the else band is executed.
Example:
if (score>=90)
{
Console.WriteLine ("Dad rewards 100 Yuan");
}
Else
{
Console.WriteLine ("Write Learning Summary 100 times");
}
2, If-else-if
Grammar:
if (condition 1)
{Statement 1;}
else if (condition 2)
{Statement 2;}
Else
{Statement 3;}
Execution process:
When the condition 1 is not established, will enter the next if statement and the condition after the IF statement, once there is a condition after a if is true, then executes the statement block with this if, after the statement block execution completes, the program jumps out of the IF-ELSE-IF structure, if all the if conditions are not established, Executes the statement block with else, otherwise nothing is executed
Example:
if (score>=90)
{
Console.WriteLine ("A");
}else if (score>=80)
{
Console.WriteLine ("B");
}
Else
{
Console.WriteLine ("C");
}
3, Switch-case
Grammar:
switch (expression) {
Case value 1: statement block 1;
Break
Case Value 2: statement block 2;
Break
Default: statement block 3;
Break
}
Execution process:
The expression is evaluated first, and then according to the value followed by the match case, if there is a match, the statement following the match is executed until the break statement jumps out of switch-case, and if all case values do not match, then there is default, Executes the statement after default until the break ends and, if there is no default, jumps out of swtich-case and does nothing.
Example:
Switch (SCORE/10)
{
Case 9:
Console.WriteLine ("A");
Break
Case 8:
Console.WriteLine ("B");
Break
Case 7:
Console.WriteLine ("C");
Break
Case 6:
Console.WriteLine ("D");
Default
Console.WriteLine ("E");
Break
}
Summary: Comparison of if-else-if and switch:
The same point: can achieve multi-branch offices;
Different points: switch generally can only be used for equivalent comparison;
If-else-if can handle the range;
If else if else