3 Controls
3 Controls
By Dr. Qi Li
BUS 601
Learning Objectives
• Decide whether to execute actions with the statements
if, if…else and if…elif…else.
• Learn problem-solving skills: understanding problem
requirements, dividing problems into smaller pieces,
developing algorithms to solve problems and
implementing those algorithms in code.
• Develop algorithms through the process of top-down,
stepwise refinement.
• Create compound conditions with the Boolean
operators and, or and not.
BUS 601
Control Statements
• Usually, statements in a program execute in the
order in which they’re written, called sequential
execution.
• Some Python statements enable you to specify
that the next statement to execute may be other
than the next one in sequence, called transfer of
control, or control statements.
– The selection statement
– The iteration statement
BUS 601
Flowcharts
• Graphical representation of an algorithm or a part of one.
• Using rectangles, diamonds, rounded rectangles and small circles
connected by arrows called flowlines.
• Clearly show how forms of control operate.
• Rectangle (or action) symbol indicates any action
• Flowlines show the order in which the actions execute.
• In a flowchart for a complete algorithm, the first symbol is a
rounded rectangle containing the word “Begin.” The last symbol
is a rounded rectangle containing the word “End.”
• In a flowchart for only a part of an algorithm, use small circles
called connector symbols.
BUS 601
Flowchart example
• Flowchart shows a sequential execution:
BUS 601
If Statement Flowchart
• The decision (diamond) symbol contains a condition that can be
either True or False.
• Two flowlines emerging from it:
– One indicates the direction to follow when the condition in the symbol is
True.
– The other indicates the direction to follow when the condition is False.
BUS 601
The if statement: a summary
if true-or-false-condition :
Indented Statement Block, if the
condition is true
Example: if miles > 20000:
print( "Upgrade!" )
Wrong: Beware!
This is not a true-or-false condition!
if miles:
print( "Upgrade!" )
BUS 601
if Statement
• “A passing grade on an examination is 60”.
• The pseudocode
If student’s grade is greater than or equal to 60
Display 'Passed'
BUS 601
Python: if
• Keyword of if,
• Semicolon
• Suite indentation
BUS 601
Indentation tells you important
information!
n= input( "Pick a positive number" )
if n < 0:
print( "I said a positive number!" )
print( "Were you listening?" )
print( "Anyway." )
BUS 601
Summary of comparison operators
Watch the order: =< and => are not correct here.
Also, no spaces are allowed in the middle of ==, !=, <=, or >=.
BUS 601
Checking equality
• To check if two things are equal, use a double equal sign: ==.
BUS 601
Combining conditions
• if statement with simple conditions:
If your temperature is greater than 98.6, then go to see the
doctor.
If your temperature is greater than 98.6 OR you have a rash on your arm, go
to see the doctor.
If your temperature is NOT greater than 98.6, don’t call in sick to work.
If you are stung by a bee AND you are allergic to bees, go to see the doctor.
If you are stung by a bee AND you are NOT allergic to bees, do not worry!
BUS 601
Logical connective (logical operator)
• In order to perform multiple conditions, logical
connective (symbol or word ) is applied to one or
multiple conditions is a used to connect one or
more simple conditions
• binary connectives
– and: conjunction, e.g., rivers AND salinity
– or: disjunction
• unary connective
– not: negation
BUS 601
Combining conditions with and
if width > 100 and height > 100 :
print ( "Area is greater than 10000 square inches." )
BUS 601
Combining conditions with or
if age < 0 or age > 120:
print ( "Not human." )
• With or, at least one of the conditions must be true for the
expression to be true!
BUS 601
Grouping conditions
if a < b and b < c or c < b and b < a :
print ( "in ascending or descending order" )
if ( a < b and b < c ) or ( c < b and b < a ) :
print ( "in ascending or descending order" )
BUS 601
Watch the phrasing of conditions
BUS 601
Selection Statement
• Three selection statements that execute code based on a
condition—an expression that evaluates to either True
or False:
– if performs an action if a condition is True or skips the action
if the condition is False.
– if…else statement performs an action if a condition is True
or performs a different action if the condition is False.
– if…elif…else statement performs one of many different
actions, depending on the truth or falsity of several
conditions.
• Anywhere a single action can be placed, a group of
actions can be placed.
BUS 601
if-else
• Pseudocode if condition :
– If student’s grade is greater than or equal to what to do if condition is true
60
Display 'Passed'
else :
Else
what to do if condition is false
Display 'Failed'
BUS 601
Concise conditional expression
BUS 601
Multi-way decisions: if-elif-else
if condition A :
...
what to do if condition A is true
...
elif condition B :
...
what to do if condition A is false but B is true
...
elif condition C :
...
what to do if conditions A and B are false but C is true
...
else:
...
what to do if none of the above conditions are true
...
BUS 601
Listen to the logic of English
BUS 601
Different ways of coding the same thing
BUS 601
Multiway decision vs. multiple decisions
if isSleepy() : if isSleepy() :
print "Sleepy" print "Sleepy"
else : else :
print "Whatever" print "Whatever"
BUS 601
Nesting
• You can put anything in an if, else, or elif
block, including other if statements, if-
else blocks, etc.
if age < 17:
if rating == "R":
print( "Parent or guardian please" )
elif rating == “X":
print ( "Go away" )
else:
print( “You may enter alone" )
BUS 601
Practice with multi-way decisions
BUS 601
Color choice program -- flow diagram
reply == no
“black”?
yes reply == no
“white”?
yes reply == no
“blue”?
color = _ color = _
yes
color = _ offer color picker
BUS 601
Algorithm
• Any computing problem can be solved by
executing a series of actions in a specific order.
• An algorithm is a procedure for solving a
problem in terms of
– the actions to execute and
– the order in which these actions execute
• Specifying the order in which statements
(actions) execute in a program is called program
control.
BUS 601
Pseudocode
• An informal language that helps you develop algorithms without
having to worry about the strict details of Python syntax.
• Similar to everyday English.
• Helps you “think out” a program before attempting to write it in
a programming language.
• Carefully prepared pseudocode can easily be converted to a
corresponding program.
• Pseudocode normally describes only statements representing the
actions that occur (e.g., input, output or calculations) after you
convert a program from pseudocode to Python and run the
program.
BUS 601
Example
BUS 601