0% found this document useful (0 votes)
92 views

COMPROG 3 Decision Making

The document discusses different selection statements in C# programming, including if-else statements, else-if statements, switch statements, and ternary operators. If-else statements allow for conditional execution of code based on a Boolean test. Else-if statements allow combining multiple conditions into a single selection structure. Switch statements execute code based on comparing a variable against predefined values. Ternary operators provide a shorthand for if-else statements with a single statement body by evaluating a condition and returning one of two possible values.

Uploaded by

erikalast.acad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

COMPROG 3 Decision Making

The document discusses different selection statements in C# programming, including if-else statements, else-if statements, switch statements, and ternary operators. If-else statements allow for conditional execution of code based on a Boolean test. Else-if statements allow combining multiple conditions into a single selection structure. Switch statements execute code based on comparing a variable against predefined values. Ternary operators provide a shorthand for if-else statements with a single statement body by evaluating a condition and returning one of two possible values.

Uploaded by

erikalast.acad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

IT1907

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:

int exam_score = 75;


if (exam_score >= 90)
{
Console.WriteLine("The grade of the student is A.");
}
else if (exam_score >= 80)
{
Console.WriteLine("The grade of the student is B.");
}
else if (exam_score >= 70)
{
Console.WriteLine("The grade of the student is C.");
}
else if (exam_score >= 60)
{
Console.WriteLine("The grade of the student is D.");
}
else
{
Console.WriteLine("The grade of the student is F.");
}

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.

03 Handout 1 *Property of STI


[email protected] Page 1 of 4
IT1907
Otherwise, it will continue to evaluate the conditions in else if statements. If none of the expressions is true, the statements
inside the else statement is executed.
An if-else-if statement can have one (1) or more else if statements and can have or no else statement at its end. For
example:

int exam_score = 75;


if (exam_score > 95)
{
Console.WriteLine("The student's exam score is excellent.");
}
else if (exam_score < 95 && exam_score > 50)
{
Console.WriteLine("The student's exam score is average.");
}
else if (exam_score < 50)
{
Console.WriteLine("The student's exam is poor.");
}

The switch Statement


The switch statement is used to execute a block of statements based on the variable to be tested for equality against
predefined list of values. The general syntax of a switch statement in C# is as follows:

switch (variable or expression) {


case value1:
//statement(s)
break;
case value1:
//statement(s)
break;
.
.
.
case valuen:
//statement(s)
break;
default:
//statement(s)
break;
}

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.

03 Handout 1 *Property of STI


[email protected] Page 2 of 4
IT1907

Example switch statement with a 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:

char letter = 'B';


switch (letter) {
case 'A':
Console.WriteLine("Apple");
break;
case 'B':
Console.WriteLine("Banana");
break;
case 'C':
Console.WriteLine("Cherry");
break;
case 'D':
Console.WriteLine("Dewberry");
break;
}

03 Handout 1 *Property of STI


[email protected] Page 3 of 4
IT1907
The Conditional Operator
The conditional operator, also called as ternary operator (?:), is a special type of decision-making operator used to perform
an operation that evaluates a logical expression then selects between two (2) single statements depending on whether the
defined expression evaluates to true or false and evaluates it. The general syntax of ternary operator is

condition ? consequence : alternative

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.

03 Handout 1 *Property of STI


[email protected] Page 4 of 4

You might also like