CSC 126 CH3
CSC 126 CH3
SELECTION CONTROL
STRUCTURE
Lesson Outcomes
Operator Description
! not
&& and
|| or
Boolean Expression
▪ Example:
Expression Value Explanation
Because ‘A’ > ‘B’ is false, ! (‘A’
! (‘A’ > ‘B’) true
> ‘B’) is true
Because 6 <= 7 is true, ! (6 <=
! (6 <= 7) false 7) is false
Boolean Expression – the && (and) Operator
▪ Expression:
Expression 1 Expression 2 Expression 1 && Expression 2
true (1) true (1) true (1)
true (1) false (0) false (0)
false (0) true (1) false (0)
false(0) false(0) false (0)
▪ Example:
Expression 1 Value Explanation
Because (14 > = 5) is true, (‘A’ < ‘B’)
(14 > = 5) && (‘A’ < ‘B’) true is true, and true && true is true, the
expression evaluates to true.
Because (24 >= 35) is false, (‘A’ < ‘B’)
false
(24 >= 35) && (‘A’ < ‘B’) is true, and false && true is false, the
expression evaluates to false. 8
Boolean Expression – the && (and) Operator
▪ Example:
Expression 1 Value Explanation
true Because (14 > = 5) is true, (‘A’ > ‘B’) is false,
(14 > = 5) || (‘A’ > ‘B’) and true || false is true, the expression
evaluates to true.
OPERATORS PRECEDENCE
++,-- First
!, +,- (unary operators) Second
*, /, % Third
+, ‐ (binary operator) Fourth
<, <=, >=, > Fifth
==, != Sixth
&& Seventh
|| Eighth
= (assignment operator) Last
TYPE OF
SELECTION
CONTROL
STRUCTURE
Introduction
1. ONE‐WAY SELECTION
a. Simple selection with null false branch (null ELSE statement)
2. TWO‐WAYS SELECTION
a. Simple selection (if‐else statement)
b. Combined selection
i. && (AND)
ii. || OR
▪ If the condition is false, then no processing will take place and the
IF statement will be passed.
▪ Syntax: if (expression)
Statement
One-way Selection (if)
• Pseudocode:
If (expression) then
statements
endif
• Flowchart:
True
Expression Statements
False
One-way Selection (if)
How it works??
One-way Selection (if)
How it works??
One-way Selection (if)
Example
If the speed of the car is greater than 80km/h, then output a message “you are too fast!”
Start
Begin
input speed
Input
if speed > 80 then speed
output “you are too fast!”
endif T
speed>80 You are
End too fast
F
End
One-way Selection (if)
C++
#include <iostream.h>
void main()
{
int speed;
}
One-way Selection (if) – Compound
Statement
} }
Single statement
}
compound statements
One-way Selection (if)
To be continued…