You are on page 1of 34

CHAPTER 3:

Selection Control Structure

Prepared for:
CSC126 - Fundamentals of Algorithms & Computer Problem Solving
OBJECTIVES OF THIS CHAPTER

In this chapter, you will:


Learn about control structures
Examine relational and logical operators
Explore how to form and evaluate logical (Boolean) expressions
Discover how to use the selection control structures if, if...else
in a program
INTRODUCTION

What does it mean when a


program is executed sequentially?
CONTROL STRUCTURES

A computer program can be executed:


In sequence
By Selection (Branch): Making a choice
By Repetition (Iterative): Looping
SELECTION STRUCTURE
Selection Structure:
Allows a computer program to make decisions

Able to control the flow of the program based on the decision made
Decision is made by evaluating the conditions given, which can
either be:
True
• Condition is met
Boolean • Represents 1
expression False
Condition is not met
Represents 0
RELATIONAL OPERATORS
Relational Operators:
Are used to compare values between 2 operands
The result of comparison will either be:
True
False
Relation Operator Description Example
== Equal to X == Y
!= Not equal to X != Y
< Less than X<Y
<= Less than or equal to X <= Y
> Greater than X>Y
>= Greater than or equal to X >= Y
QUICK EXERCISE

Evaluate these conditional statements:


Let a = 4 and b = 10, then:
a) a < b
b) b == a
c) a >= b

Let a = 7 and b = 5
a) a != b
b) a > b
c) b <= 7
LOGICAL OPERATORS
Logical Operators:
Are used when there are more than one relational expression to evaluate
at a time
Allows you to combine the relational expressions
The result of comparison will either be:
True
False
Logical Description Unary or Binary Rule Example
Operator Operator
! NOT Unary Produce opposite ! (a < b)
relation
&& AND Binary Both operand must (a < b) && (a < c)
be true
|| OR Binary At least one operand (a == b) || (a == c)
must be true
QUICK EXERCISE

Evaluate these conditional statements:


NOT:
a) ! (4 > 8)
b) ! (6 <= 7)
AND:
a) (3 < 9) && (10 > 2)
b) (3 > 6) && (4 < 6)
OR:
a) (5 < 7) || (9 > 4)
b) (5 > 8) || (6 < 2)
ORDER OF PRECEDENCE

Order of Precedence:
Determine the order of evaluation in an expression
QUICK EXERCISE

Evaluate these conditional statements:

Let x = 3, y = 4 and z = 5, then:


1) (x > y) && (y < z)
2) (x == y) || (x == z)
3) ! (z < y)
SELECTION CONTROL STRUCTURE TYPES

Types of Selection Control Structure:


1. One-Way Selection
2. Two-ways selection
3. Multiple selection
ONE-WAY SELECTION

One-Way Selection:
Only involve an if statement (without else statement)
If the condition is TRUE, the statement inside if.. will be executed
If condition is FALSE, the control goes to the next statement placed
outside the if statement (the if statement is ignored)

Condition is formed as a
Boolean expression

When condition is TRUE

When condition is FALSE


QUICK EXERCISE
Suppose the passing grade of an examination is 50 and the value
of grade is 50:

a. Draw a flowchart based on the program above


b. If the grade is keyed-in as 50, what would be the output displayed on the
screen?
c. If the grade is keyed-in as 40, what would be the output displayed on the
screen?
QUICK EXERCISE

a. Draw a flowchart based on the program above.


b. If the two integers entered are 15 and 9 respectively, what would be the
output displayed on the screen?
TWO-WAY SELECTION
Two-Way Selection:
Is used when a decision is required between two alternatives
Uses if... else statement
If the condition is TRUE, the statement inside if.. will be executed
and the statement inside else.. will be ignored
If condition is FALSE, the statement inside if.. will be ignored and
the statement inside else.. will be executed

When condition is TRUE

When condition is FALSE

Executed afterwards
QUICK EXERCISE

a. Draw a flowchart based on the program above


b. If the hoursWorked is keyed-in as 50, what would be the output displayed on
the screen?
c. If the hoursWorked is keyed-in as 100, what would be the output displayed on
the screen?
MULTIPLE SELECTION

Multiple Selection:
Is used when there are multiple conditions involved
Can be implemented using any of the 3 ways:
1. if …. else if statement
2. Nested if Statement
3. switch Statement
MULTIPLE SELECTION - if…else if
if…else if Statement:
Is used when there are multiple conditions based on the
same subject
The program will continuously check each condition
starting from the top to the bottom until it has found
the condition that is TRUE
Once the condition is evaluated as TRUE,
the statement inside if.. will be executed and the other
if statements will be ignored
If there are no condition that can be evaluated as TRUE,
the last else.. statement will be executed (if exist)

Executed afterwards
QUICK EXERCISE

a. Draw a flowchart based on the program


given here
b. If the grade is keyed-in as B, what would
be the output displayed on the screen?
c. If the grade is keyed-in as E, what
would be the output displayed on the
screen?
COMPOUND/BLOCK OF STATEMENTS

C++ provide a structure called a compound statement or a block of


statements if you intend to execute more than one statement with an if…
else if statement.
QUICK EXERCISE
QUICK EXERCISE
if…else STATEMENTS VS A SERIES OF if STATEMENTS
if…else STATEMENTS VS A SERIES OF if STATEMENTS
MULTIPLE SELECTION - NESTED IF
Nested if Statement:
Is used when a particular condition contains multiple conditions of
the same subject
Uses nested if.. else (one control statement inside another)

Each of these conditions


can only be evaluated if
the outer condition is
true

Executed afterwards
QUICK EXERCISE

a. Draw a flowchart based on the program


given here
b. If the gender and age is keyed-in as M
and 12 respectively, what would be
the output displayed on the screen?
c. If the gender and age is keyed-in as W
and 36 respectively, what
would be the output displayed on the
screen?
MULTIPLE SELECTION - SWITCH STATEMENT
switch Statement:
Is an alternative method that can be used for multiple selection instead of a
nested if.. else
Can only be used for comparing:
Integer expression
Character constant

When break statement is


executed, it will exit the switch

Default case is optional, it act


like an else statement
MULTIPLE SELECTION - SWITCH STATEMENT

switch Statement for Integer Expression:


MULTIPLE SELECTION - SWITCH STATEMENT
switch Statement for Character Constant:
COMPARING STRINGS
Comparing strings:
To compare strings in C++:
A predefined function strcmp( ) is used

Will compare string in variable, s1


and string in variable, s2

Will check if the two strings are


the same
(have no differences)
QUICK EXERCISE

a. Draw a flowchart based on the program above


b. If the gender is keyed-in as female , ha o ld be he o p displa ed on
the screen?
c. If the gender is keyed-in as Male , ha o ld be he o p displa ed on he
screen?
QUICK EXERCISE
END OF CHAPTER 3

You might also like