CONTROL STRUCTURES - SELECTIONS
Selection structures
The compiler usually execute program instructions line by line without jumping a line. Even though the compiler does not jump a line, a program is usually not limited to a linear sequence of instructions. During its process it may, repeat code or take decisions.
C# provides control structures that controls the flow of program execution.
C# statement
A C# statement is a computer instruction which end with a semicolon. A compound statement is a sequence of single statements enclosed between braces ({}). A compound statement is also called a block statement
Conditional Structure : if-else
The if-else construct is a two way selection that tells the computer to execute statements basing on evaluation of a condition. The syntax is:
if (condition) statement1 else
statement2
In the if-else construct the condition is evaluated first.If the condition evaluates to True, statement1 is executed. If the condition evaluates to false, statement2 is executed. The if part of the construct is compulsory while the else part is optional. The word statement stands for either simple statement or compound statement.
The flow diagram below gives a visual implementation of the if-else construct
The code fragment below prints x is 5 only if x contains 5, but prints x is not 5 if x contains a value other than 5
if (x == 5)
Console.WriteLine(" x is 5");
else
Console.WriteLine(" x is not 5");
Since the else part is optional, then it can be omitted. When the else part is omitted, the if-else construct becomes a one way selection structure and the syntax becomes
if (condition) statement
The statement is executed only if the condition evaluates to true. The modified flow diagram is below
the code fragment below prints x is 5 only if the value stored in the x variable is equal to 5:
if (x = =5) Console.WriteLine(" x is 5");
Nesting if statements
an if-else statement can contain any valid C# statement, including another if-else statement. This means one or more if-else statements can be included in either part of an if-else statement. An existing if-else statement is called a nested if statement when it contains other if-else statement.
if(x>y) {
if(y>z)
Console.WriteLine("y is greater than z ");
} else
Console.WriteLine("x is less than or equal to y "); ";
if the else part of if-else construct is omitted in a nested if-else, it is difficult to know which if is associated with a certain else. To avoid this ambiguity, the else is associated with the closest previous if (without else).
if(x>y) if(y>z)
Console.WriteLine("y is greater than z ");
else
Console.WriteLine("x is less than or equal to y ");
To avoid the confusion, it is better to include braces{}
if(x>y) {
if(y>z)
Console.WriteLine("y is greater than z ");
else
Console.WriteLine("y is less than or equal to z ");
}
if(x>y) {
if(y>z)
Console.WriteLine("y is greater than z ");
} else {
Console.WriteLine("x is less than or equal to y ");
}
if-else chain
The if-else chain is a way of presenting a multi-way selection. If the condition evaluates to true, the statement associated with that condition is executed. If none of the condition evaluates to true, the statement associated with the last else is executed. Here is the syntax
if (condition) statement
else if (condition) statement
else if (condition) statement
. .
else if (condition) statement
else statement
the code fragement below compares two values stored in variables x and y
if (x > y) {
Console.WriteLine("x is bigger than y ");
}
elseif (x == y) {
Console.WriteLine("x is equal to y ");
} else {
Console.WriteLine("x is smaller than y ");
}
Remember that in case that we want more than a single statement to be executed, we must group them in a block by enclosing them in braces { }.
//variable initialisation int grade;
Console.WriteLine("Enter grade : ");
grade = int.Parse(Console.ReadLine());
if (grade >=70)
Console.WriteLine("you have passed with flying colours");
else if (grade>=60 && grade<70)
Console.WriteLine("you have passed with a high grade");
else if (grade>=50 && grade<60)
Console.WriteLine("you have passed");
else
Console.WriteLine("you must improve your grade");
THE SELECTIVE STRUCTURE: switch
an if-else chain is used when one set of instructions needs to be selected from many possible alternatives. An alternative approach is to use the switch statement. The switch statement is a multi-way selection statement that tests whether an expression matches one of a number of given integer or character constants (other types of constants are not allowed.)
switch(expression) { case constant1:
statement 1 break;
case constant2:
statement 2 break;
case constant3:
statement 3 break;
. . .
default:
statement d }
switch evaluates expression and checks if it is equivalent to cases, starting with the first case. If the expression matches with a particular case, it executes group of statements associated with that case until it finds a break statement. If no matching case is found, the switch construct executes the statement sequence under the default case. A default is optional; if it isn't there and if none of the cases match, no action at all takes place. Cases and the default clause can occur in any order.
Here is the flow diagram of the switch statement
Because cases are labels, after the code for one case is done, execution falls through to the next unless you specify to leave the switch. break and return are the most common ways to leave a switch. The break statement causes program execution to jump to the end of the switch selective structure
int n;
Console.WriteLine("Enter 1 or 2 or 3 : "); n = int.Parse(Console.ReadLine());
switch(n) {
case 1:
Console.WriteLine("you typed 1");
break;
case 2:
Console.WriteLine("you typed 2");
break;
case 3:
Console.WriteLine("you typed 3");
break;
default:
Console.WriteLine("you did not follow the instruction");
break; //this break is optional }
Since the switch statement uses labels instead of blocks we can use the switch statement to execute the same block of instructions for different possible values for the expression being evaluated. For example:
Console.WriteLine("Enter 1 or 2 or 3 : ");
n = int.Parse(Console.ReadLine());
switch(n) {
case 1:
case 2:
Console.WriteLine("you either typed 1 or 2");
break;
case 3:
Console.WriteLine("you typed 3");
break;
default:
Console.WriteLine("you did not follow the instruction");
break; //this break is optional } Console.ReadKey();
Notice that switch can only be used to compare an expression against types that can be statically evaluated. This limits the expression on the switch to the following types: built-in integral types, bool, and enum types (and nullable versions of these), and string type. In addition you should not use variables as case arguments.