If…Else if Decision Structures
Conditional expressions used in a special block of statements called a decision structure control whether other statements in your program are executed. You can use an decision structure to evaluate a condition in the program and it will take respective action depends on the condition.
If (condition)
Statement;
where condition is a conditional expression, and statement is a valid Visual C# program statement. For example,
If (Marks >= 40)
Label1.Text = "You Pass!";
is an decision structure that uses the conditional expression
Marks >= 40
to determine whether the program should set the Text property of the Label1 object to “You Pass!” If the Marks variable contains a value that’s greater than or equal to 40, Visual C# sets the Text property; otherwise, it skips the assignment statement and executes the next line in the event procedure. This sort of comparison always results in a True or False value.
Visual C# also supports an decision structure that allows you to include several conditional expressions. This block of statements can be several lines long and contains the important keywords Else If, Else.
If (condition1)
statements executed if condition1 is True;
Else If (condition2)
statements executed if condition2 is True;
[Additional Else If clauses and statements can be placed here]
Else
statements executed if none of the conditions is True;
In this structure, condition1 is evaluated first. If this conditional expression is True, the block of statements below it is executed, one statement at a time, and then jumps to the line after the final statement and picks up execution there. If the first condition isn’t True, the second conditional expression (condition2) is evaluated. If the second condition is True, the second block of statements is executed. (You can add additional Else If conditions and statements if you have more conditions to evaluate.) If none of the conditional expressions is True, the statements below