Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

EE106: Engineering Design For

p
Software Development 1

Decisions
Overview
ƒ Introduction
ƒ Basic
B i decision
d i i making ki – if statement
t t t
ƒ Evaluating expressions
ƒ Mutually exclusive execution
ƒ Multiple parallel branches
ƒ Multiple values versus multiple conditions
ƒ Summary
Introduction
ƒ Computer programs perform millions of
calculations
l l ti per second.
d

ƒ The
Th also
l take
t k millions
illi off d
decisions
i i per second.
d

ƒ Th
The if,
if else,
l and
d elif
lif statements
t t t provide
id this
thi
capability
Basic decision making
ƒ The if statement
provides
id ththe means
to conditionally
executet a sequence
of statement
ƒ Condition
C diti iis a
boolean expression
ƒ Generally in the form
of a comparison
if syntax

The condition must evaluate to a if <condition> :


boolean True or False # conditional
di i l branch
b h
statement1
A colon
l ffollows
ll the
h condition.
di i statement2
statement3
Indentation defines a code block statement4

A code block forms the conditional # common branch


branch, execute when True statement55
statement6
Subsequently,
S b l rejoin
j i common
branch
Boolean expressions
ƒ Comparisons
ƒ x == y: x equal to y?
ƒ x != y : x not equal
q to yy?
ƒ x > y : x greater than y?
ƒ x < y : x less than y?
ƒ x >= y : x greater than or equal to y?
ƒ x <= y : x less than or equal to y?
ƒ The outcome of these comparisons is a
boolean type
Mutually exclusive execution
ƒ The if – else combination
allows two mutually
exclusive paths of
e ecu o
execution
ƒ Either branch 1 or
branch 2 then rejoin
common branch
ƒ Boolean nature of
expression means only
two branches
if – else syntax

If the condition resolves to True,, if <condition> :


conditional branch 1 is executed. # conditional
di i l bbranchh1
statement1
Iff the
h condition
di i resolves
l to False,
l statement2
conditional branch 2 is executed. else:
# conditional branch 2
Subsequently, rejoin common statement3
branch statement4

# common branch
statement5
statement6
Multiple branches
ƒ Nested if statements
used
d
ƒ if within an if
ƒ if within else
ƒ Functionally equivalent
ƒ Multiple values
ƒ Multiple variables
Multiple values
ƒ Both conditions operate on a common variable
off interest
i t t
if x > 0:
ƒx>0 # execute branch 1
else
l :
ƒx<0 if x < 0:
# execute branch 2
ƒ x == 0 else:
# execute branch 3

Multiple values (or ranges of values) of a single variable.


Three ranges – three outcomes.
Multiple variables
ƒ More than one variable if x <= 0:
value
l off interest
i t t # execute bbranch
h1
else :
ƒ x <= 0 if y >= 0:
# execute branch 2
ƒ x > 0, y >= 0 else:
ƒ x > 0,
0 y<0 # execute branch 3

Two variables of interest: x and y.


Three outcomes.
elif syntax
ƒ Common variable
ƒx>0 if x > 0 :
ƒx<0 # execute branch 1
elif
lif x < 0 :
ƒ x == 0 # execute branch 2
else :
ƒ More readable # execute branch 3

Rule of thunb: elif syntax is preferred when testing multiple


values of a single variable.
elif syntax
ƒ Multiple variables if x <= 0:
# execute bbranch
h1
ƒ x <= 0 elif y >= 0:
ƒ x > 0,, y >= 0 # execute branch 2
else:
ƒ x > 0, y < 0 # execute branch 3

ƒ Many variables being considered

Program logic more difficult to read.


Perhaps nested approach more appropriate.
appropriate
Summary
ƒ Conditional execution of code
ƒ Condition
C diti iis a boolean
b l expression
i
ƒ Generally a comparison of some sort
ƒ Mutually exclusive execution of two branches
ƒ For more than two branches
ƒ Use nesting
ƒ Use elif statement
ƒ Rule of thumb:
ƒ Multiple values, use elif
ƒ Multiple variables
variables, use explicit nesting

You might also like