COMPROG 3 Decision Making
COMPROG 3 Decision Making
Decision Making
Selection Statements
Selection statements or selection structures are used to execute particular statements whether the value of the given
condition evaluates to true or false. These are used to alter the flow of program that will select a block of statementstoexecute.
The C# provides three (3) selection structures:
• The if and if…else statements are conditional statements that include a logical expression and provide a one- ortwo-way
selections.
• The switch statement executes a block of statements based on the variable to be tested for equality against a list ofvalues.
The if-else-if Statement
The if statement instructs the program to execute a certain block of statements only if a particular condition evaluates totrue;
otherwise, the program will execute the bock of statements in the else statement if false. An if-else statement in C# takes
the following syntax and example:
Syntax: For example:
if (expression) int exam_score = 75;
{ if ( exam_score >= 60 )
//statement(s) {
} Console.WriteLine("The student passed the exam.");
else }
{ else
//statement(s) {
} Console.WriteLine("The student failed the exam.");
}
The else if statement is used to combine multiple conditions into a single selection structure. This is also called multiple
selection. An else if statement is placed after the if statement or another else if statement. For example:
The if-else-if statement and its conditions are executed from top to bottom. When the condition in if statement is
evaluated to true, the if-else-if statement will skip the other conditions and jump to the statements afterthisif-else-ifstatement.
The variable or expression in the switch statement in C# can only be an integer, a floating-point number, a character, or a
string type. This variable is then tested for equality to each case label from top to bottom. The value of these case labelsmust
be the same data type as the variable in the switch. Each case label must contain a break statement to stop the flow control
of the switch statement if the specified value of the variable matches the value in the case label. The break statementcanonly
terminate the process of a switch statement or any repetition structure or looping statements in which it appears.
The switch statement may or may not contain a default statement, which must appear at the end of switch statement. The
default statement is used to execute statements when none of the cases match the specified variable. The defaultstatement
in C# must contain a break statement. This is to make sure that the flow control will not fall through to the default statement.
int weekNumber = 4;
switch (weekNumber) {
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
case 4:
Console.WriteLine("Thursday");
break;
case 5:
Console.WriteLine("Friday");
break;
case 6:
Console.WriteLine("Saturday");
break;
case 7:
Console.WriteLine("Sunday");
break;
default:
Console.WriteLine("Invalid number of day.");
break;
}
Example switch statement without default statement:
The condition must be a logical expression, and the statements in consequence and alternative must be a value oranexpression
that returns a value. If the condition evaluates to true, then the statement in consequence is evaluated, and its resultbecomes
the return value of the operation. If condition evaluates to false, then the statement in alternative is evaluated, and its result
becomes the return value of the operation. The ternary operator does not execute a statement but returns a value, therefore
can be assigned into a variable. The statements in consequence and alternative must be the same type as the variable where it
will be stored. The following are examples of using ternary operator:
Example 1:
int sum = 4 > 2 ? 4 + 2 : 4 – 2; //the return value is 6 and assigned to the variable sum
Example 2:
int exam_score = 75;
string result = exam_score >= 60 ? "Student passed the exam." : "Student failed the exam.";
Console.WriteLine(result);
The ternary operator (?:) is used as an alternative to if…else statements that contains a single statement body.
REFERENCES:
Deitel, P. and Deitel, H. (2015). Visual C# 2012 how to program, 5th edition. USA: Pearson Education, Inc.
Gaddis, T. (2016). Starting out with visual C#, 4th edition. USA: Pearson Education, Inc.
Harwani, B. (2015). Learning object-oriented programming in C# 5.0. USA: Cengage Learning PTR.