Module 3 - Program Flow Control
Module 3 - Program Flow Control
Programming
Module 2 – Program
Flow Control
Jasmin Tumulak Estudillo, MSIT
Lecturer
Conditional
s
2
Boolean Value
◉ A Boolean value is either true or false.
◉ In Python, the two Boolean values are True and False (the
capitalization must be exactly as shown), and the Python
type is bool.
3
Boolean expression
◉ A Boolean
expression is an
expression that
evaluates to
produce a result
which is a Boolean
value.
4
Logical operators
◉ There are three logical operators, and, or, and not
◉ Example:
◉ X=5
◉ x > 0 and x < 10 = True
5
Truth Table
6
Conditional Execution
◉ If-else syntax
7
Conditional Execution
◉ If statement syntax:
if Boolean
expression:
statement
if true
8
Chained Conditional
◉ If-elif statement syntax
9
Chained Conditional
◉ Example Grade Range Letter Grade
Write a Python program that 100 – 95 A
accepts the student’s grade points
90-94 B
and displays the corresponding
letter grade. 80-89 C
70-79 D
60-69 E
Below 60 F
10
Nested Conditionals
◉ Nested If statement syntax
11
Logical opposites
◉ Each of the six relational
operators has a logical
opposite.
12
Logical opposites
13
Short Hand
14
Iterations
15
Short Hand
16
Python Loops
◉ Python has two primitive loop commands:
a. while loops
b. for loops
17
The while loop
18
break statement
◉ With the break statement
we can stop the loop even
if the while condition is
true.
19
continue statement
◉ With the continue
statement we can stop the
current iteration, and
continue with the next:
20
for loop
◉ A for loop is used for
iterating over a sequence
(that is either a list, a
tuple, a dictionary, a set,
or a string).
21
The range() Function
◉ To loop through a set of code a
specified number of times, we
can use the range() function,
◉ The range() function returns a
sequence of numbers, starting
from 0 by default, and increments
by 1 (by default), and ends at a
specified number.
22
The range() Function
◉ To loop through a set of code a
specified number of times, we
can use the range() function,
◉ The range() function returns a
sequence of numbers, starting
from 0 by default, and increments
by 1 (by default), and ends at a
specified number.
23
Using the start parameter
◉ The range() function
defaults to 0 as a starting
value, however it is
possible to specify the
starting value by adding a
parameter: range(2, 6),
which means values from
2 to 6 (but not including
6):
24
Using the increment
parameter
◉ The range() function
defaults to increment the
sequence by 1, however it
is possible to specify the
increment value by adding
a third parameter:
range(2, 30, 3):
25
Else in For Loop
◉ The else keyword in a for
loop specifies a block of
code to be executed when
the loop is finished:
26
References
27