0% found this document useful (0 votes)
46 views23 pages

Multi Way Selection and Summaries

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views23 pages

Multi Way Selection and Summaries

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Control Structure

- Selection & Decision


Making Decision:
Multiway Selection
If Elif
Nested If

Prepared By Dr Goh Wan Inn


Multi-Way Selection
The if…elif Statement

Copyright © 2007 Pearson Education, Inc.


Publishing as Pearson Addison-Wesley
The if..elif Statement
• Chain of if statements that test in order
until one is found to be true
• Also models thought processes:
– “If it is raining, take an umbrella,
else, if it is windy, take a hat,
else, take sunglasses”
if..elif format
if condition:
statement1 // or block
elif: (expression)
statement2 // or block
.
. // other else ifs
.
else: (expression)
statementn // or block
Using a Trailing else
• Used with if/elif statement when none of
the expressions are true
• Provides default statement/action
• Used to catch invalid values, other exceptional
situations
• Nested Condition
Nested if Statements
• An if statement that is part of the if or else
part of another if statement
• Can be used to evaluate more than one
condition:
if mark<100:
if mark>90:
grade = 'A+'
print ("Your grade is ",grade)
else:
grade = 'A'
print ("Your grade is ",grade)
Slide 4- 10
Notes on coding nested ifs
• An else matches the nearest if that does not
have an else:

if mark<100:
if mark>90:
grade = 'A+'
print ("Your grade is ",grade)
else:
grade = 'A'
print ("Your grade is ",grade)
else:
print ("Your grade more than 100")
Slide 4- 11
Checking Numeric Ranges
with Logical Operators

Copyright © 2007 Pearson Education, Inc.


Publishing as Pearson Addison-Wesley
Checking Numeric Ranges with
Logical Operators
• Used to test to see if a value falls inside a range:
if grade >= 0 and grade <= 100:
print (“Valid Score”)

• Can also test to see if value falls outside of range:


if grade <= 0 or grade >= 100
print ("Invalid grade“)

• Cannot use mathematical notation:


if 0 <= grade <= 100 //doesn’t work!

Slide 4- 14
• Validating User Input

Copyright © 2007 Pearson Education, Inc.


Publishing as Pearson Addison-Wesley
Validating User Input
• Input validation: inspecting input data to
determine whether it is acceptable
• Bad output will be produced from bad input
• Can perform various tests:
– Range
– Reasonableness
– Valid menu choice
– Divide by zero

Slide 4- 16
• Summaries Decision and Selection
– Example using Python language

Copyright © 2007 Pearson Education, Inc.


Publishing as Pearson Addison-Wesley
Decision Structure - IF
Pattern 1

condition
This must be a True if condition:

True statement

statement False
Example 1: Printing a number only if it is a negative

n<0
if n<0:

True
print (“n is “,n)

print False
n
Decision Structure – IF / ELSE
Pattern 2

if condition:

statement_1
condition True statment_1

else:
False
statement_2

statment_2
Decision Structure – If/Elif/Else
Pattern 3 if condition_1:

statement_1

condition_1 True statment_1


elif condition_2:

False
statement_2

condition_2 True statment_2

elif condition_n:

condition_n True statment_n statement_n

False else:

statment_m
statement_m
Tutorial 1
Draw a flow chart and write a
program to determine the grade of
user marks.

Slide 4- 23
Tutorial 2
Draw a flow chart and write a program to calculate A, Ix and Iy
based on selection from user.

Computer Programming 24

You might also like