You are on page 1of 25

Chapter 4: Decisions

Selection Control Structure


Learning Outcomes
Upon completion of this lecture, learners will be
able to:
– grasp the basic concept of selection structures
and
– apply selection structures (if..else) and
(nested if) in Python program
Control Structures
• control the flow of execution in a program
• enable you to combine individual instructions
into a single logical unit with one entry point
and one exit point
• there are THREE (3) control structures:
 sequence
 selection
 repetition
Use of Logical Operator in Control
Structure
• Python supports the usual logical conditions from
mathematics. Some of the operators are written
differently.
✓ Equals: a == b
✓ Not Equals: a != b
✓ Less than: a < b
✓ Less than or equal to: a <= b
✓ Greater than: a > b
✓ Greater than or equal to: a >= b
• These conditions can be used in several ways, most
commonly in “if statements” and loop.
Logical Operator - and
• The and keyword is a logical operator and is used to
combined conditional statements.

Output:

condition1 condition2 condition1 How do we evaluate the statements?


and
condition2

False False False (a > b) and (c>a) is True,


because (200>33) and (500>300) are both
False True False True.

True False False

True True True


Logical Operator - and
• Trace this..
Age=24
Output:
gender='F’
if (Age > 18) and (gender=='F'):
print ('She is an adult. You may marry her')

condition1 condition2 condition1 Example


and
condition2

False False False (Age > 18) and (gender==‘F’) is True,


because (24>18) and (‘F’==‘F’) are both True.
False True False

True False False *What if we change gender=‘M’?


True True True
Logical Operator - or
• The or keyword is a logical operator, and is used to combine
conditional statements:
Output:

condition1 condition2 condition1 Example


or
condition2

False False False (a > b) or (c>a) is True, because both


conditions are True
False True True
True False True What if a=34 ?
True True True
Logical Operator - not
• The not keyword is a logical operator. The
return value will be True if the statements(s)
are not True, otherwise it will return False
Output:

p Not p Example(assume age = 24, gender = ‘F’)


True False not (age>18) is False, because (age>18) is
True.
False True not (gender ==‘M’) is True, because
(gender==‘M’) is False
Selection Control Structure
• An “if statement” is written by using the if
keyword
• The elif keyword is Python’s way of saying “if
the previous conditions were not true, then try
this condition.
• The else keyword catches anything which isn’t
caught by the preceding conditions.
• Python relies on indentation to define scope in
the code. Other programming languages often
use curly-brackets for this purpose.
Selection Control Structure (cont.)
• three different situations
– One alternative (if)
– Two alternatives (if..else)
– Multiple alternatives (if..elif..else /
nested if)
if
statement with one alternative

false
Score true

< 40

Display ‘See your lecturer’


if
statement with one alternative (cont.)
SYNTAX: if condition :
statementT;
if condition : statementT;
EXAMPLE: if score < 40:
print(‘see your lecturer’)
if score < 40: print(‘see your lecturer’)

• if condition evaluates to true, then statementT is


executed; otherwise, statementT is skipped.
• Condition can be single or multiple condition (AND/OR)
• statementT can be a single statement or
compound/multiple statements.
Compound Statements (cont.)
if score < 40:
print(‘URGENT’)
print(‘See your lecturer’)
if..else
statement -Two alternatives

start

get score

false true
score >= 40

Display Display
‘fail’ ‘pass’

stop
if..else
statement -Two alternatives (cont.)
SYNTAX: if condition:
statementT
else:
statementF
statementT if condition else statementF
EX: if score >= 40:
print(‘pass’)
else:
print(‘fail’)
print(‘pass’) if score>=40 else print(‘fail’)
if..elif..else
Multiple-Alternative Decisions
start

get score

false score >= true


80

false score >= true


40
Display Display Display
‘fail’ ‘pass’ ‘distinction’

stop
if..elif..else
Multiple-Alternative Decisions (cont.)
SYNTAX: if condition:
statementT
elif condition:
statementT
else:
statementF

EX: if score >= 80:


print(‘distinction’)
elif score >= 40:
print(‘pass’)
else:
print(‘fail’)
Nested if..
Multiple-Alternative Decisions
start

get x

true Display
x > 10
‘Above ten’

false true
x > 20 Display ‘and
also above 20!’
false
Display ‘but
not above 20’

stop
Nested if..
Multiple-Alternative Decisions
• You can have if statements inside if statements, this is
called nested if statements.
SYNTAX: if condition:
statementT
if condition:
statementT
else:
statementF
EX: if x >= 10:
print(‘Above ten,’)
if x >= 40:
print(‘and also above 20!’)
else:
print(‘but not above 20.’)
Exercise
• Create an analysis, pseudocode, flowchart and
python program for checking whether a
number is divisible by 2 and 3, by 2 or 3, and
by 2 or 3 but not both. Display appropriate
message for each checking.
Solution
• Analysis
Input Process Output

number Prompt and get number Related


If (number%2==0) and (number%3==0) message
display (number, “is divisible by 2 and 3”)

If (number %2==0)or(number %3==0)


display (number, “is divisible by 2 or 3”)

If((number%2==0 )or (number%3==0)) and not((number%2==0)and(number%3==0))


display (number, “is divisible by 2 or 3,but not both”)
Solution(cont.)
• Pseudocode

1) Start
2) Prompt and get number
3) If (number % 2 ==0) and (number%3==0)
display (number, “is divisible by 2 and 3”)
4) If (number %2==0)or(number %3==0)
display (number, “is divisible by 2 or 3”)
5) If((number%2==0 )or (number%3==0)) and not((number%2==0)and(number%3==0))
display (number, “is divisible by 2 or 3,but not both”)
6) End
Solution(cont.)
• Flowchart

start

Prompt and get


number

If ((number%2==0)
If
or(number%3==0)) f
(number%2==0) f and not
((number%2==0)
and
and(number%3==0))
(number%3==0)
If f
(number%2==0)
t or t
display (number, (number%3==0)
display (number,
“is divisible by 2 “is divisible by 2
and 3”) or 3,but not
t
both”)
display (number,
“is divisible by 2
or 3”)
end
Solution(cont.)

Output (Run 1): Output (Run 2):


Summary
• The relational operators (<, <=, ==, !=, >, >=) which
work with numbers and characters, yield a Boolean value.

• The Boolean operators and, or and not operate with Boolean


values and variables.

• Selection statements are used for programming with alternative


courses. There are several type of selection statements: if
statements, if..else statements, nested
if..elif..else statements, and conditional expressions.

• The various if statements all make control decisions based on a


Boolean expression. Based on the True or False evaluation of
the expression, these statements take one of possible courses.

You might also like