B12 03 Python Control Flow
B12 03 Python Control Flow
Lê Ngọc Hiếu
lnhieu@hcmute.edu.vn
1. Decision Control
4. Exercises
2
Decision Control
Python if Statement
Conditio False
n
True
3
if Statement
• Python use if statement to execute a block of code based
on a specified condition. Syntax of if statement:
Conditio False
if condition: n
True
statement_1
Statement_1
statement_2 Statement_2
... ...
Statement_N
statement_N
if Statement – Important Notes
• After the condition, there must be a colon.
Correct Incorrect
if Statement – Important Notes
• Python relies on indentation (whitespace at the beginning of a
line) to define scope in the code.
if Statement – Important Notes
• Python relies on indentation (whitespace at the beginning of a
line) to define scope in the code.
VS
if…else Statement
• Python use if ... else statement to perform an action when a
condition is True and another action when the condition is
False.
if condition:
statements in if-block Conditio False
e lse: n
True
statements in else-block
Statement Statement
s in if- s in else-
block block
8
if…elif…else Statement
if condition1:
• Python use
statements in if-
if...elif...else
block statement to check multiple
elif condition2: conditions and perform an
statements in elif- action accordingly.
block
elif condition3: • The elif stands for else if.
statements in elif-
block
... 9
if…elif…else Statement - Example
10
if…elif…else Statement – Another Example
11
Nested if, elif, else Conditions
• Python supports nested if, elif, and else condition.
12
Nested if, elif, else Conditions
• Python supports nested if, elif, and else condition.
13
Python Ternary Operator
• Syntax of ternary operators:
value_if_true if condition else value_if_false
Input: h,
w
Yes a = h*w
h>0&w
p = (h+w)
>0
*2
No
Output: Output: S,
Invalid C
inputs
End
15
[if] Ex01: Write a program to
compute perimeter and area of
a rectangle given the length
and width of the rectangle.
Begi
n
Input: h,
w
Yes a = h*w
h>0&w
p = (h+w)
>0
*2
No
Output: Output: S,
Invalid C
inputs
End
16
[if] Ex02: Write a program to
solve linear equations.
17
[if] Ex02: Write a program to
solve linear equations.
18
[if] Ex03: Write a program to
solve Quadratic equations.
19
[if] Ex03: Write a program to
solve Quadratic equations.
20
Python Loops
while loops
for loops
21
while loop
• Python uses the while statement to execute a code block
repeatedly as long as a condition is True.
Conditio False
while condition:
n
statement1 True
statement2 statement1
... statement2
...
statementN statementN
while loop - Example
• Print all integers between 1 and 10.
Correct
implementation
Wrong
23
[while] Ex01: Sum of all
integers between 1 and n,
where n is a positive integer
inputted by users.
24
[while] Ex01: Sum of all
integers between 1 and n,
where n is a positive integer
inputted by users.
25
[while] Ex01: Sum of all
integers between 1 and n,
where n is a positive integer
inputted by users.
Begi
n
Input:
n
i=1
s=0
i <= No Output:
s End
n
s = sYes
+
i
i=i+
1
26
[while] Ex02: Sum of all even
integers between 1 and n,
where n is a positive integer
inputted by users.
27
[while] Ex02: Sum of all even
integers between 1 and n,
where n is a positive integer
inputted by users.
28
[while] Ex03: Find greatest
common divisor (GCD) of two
positive integers.
29
[while] Ex03: Find greatest
common divisor (GCD) of two
positive integers.
30
for loop
• A for loop is used for iterating over a sequence, such as
list, tuple, set, range, etc.
• The body of the for loop is executed for each member
element in the sequence.
• Hence, for loop doesn't require explicit verification of a
boolean expression controlling the loop (as in the while
loop).
31
for Loop with range() Function
• To loop through a set of code a specified number of times, we
can use the range() function.
• Syntax: for var_name in range(start, stop, step)
Parameter Description
start Optional (default is 0)
An integer number specifying at which position to
start.
stop Required.
An integer number specifying at which position to
stop (not included).
step Optional (default is 1)
An integer number specifying the incrementation.
32
for Loop with range() Function -
Examples
33
for Loop with range() Function -
Examples
Corresponding output
34
for Loop with Sequence Type
• The object of any Python sequence data type can be
iterated using the for statement.
35
for Loop with Sequence Type
• The object of any Python sequence data type can be
iterated using the for statement.
36
[for] Ex01: Sum of all
integers between 1 and n,
where n is a positive integer
inputted by users.
39
break statement
• Python use the break statement to terminate a loop
prematurely.
• Typically, the break statement is used with the if
statement to terminate a loop when a condition (of the if
statement) is True.
break statement
• Python use the break statement to terminate a loop
prematurely.
• Typically, the break statement is used with the if
statement to terminate a loop when a condition (of the if
statement) is True.
42
break statement
• If the break statement is used in a nested loop, it will
terminate the innermost loop.
• Example:
Output
43
[break] Ex01: Write a program to enter a name (max of 50).
Print the name to the screen. The program stops if quit is
entered.
44
[break] Ex02: Input a number. Check if this number is a
prime number.
Idea: Count the number of factors. If the number of factors = 2 it is a
prime number.
45
[break] Ex02: Input a number. Check if this number is a
prime number.
Idea: Count the number of factors. If the number of factors = 2 it is a
Begi
prime number.
n
Input:
a
count =
0
i=1
Output:
No count No non-prime
i <= a
=2 number
Yes
a%i No Yes
== 0 Output: prime
Yes number End
count
+= 1
i += 1
46
[break] Ex02: Input a number. Check if this number is a
prime number.
Idea: Count the number of factors. If the number of factors = 2 it is a
prime number.
47
[break] Ex02: Input a number. Check if this number is a
prime number.
Begi
Improved version of the previous function
n
Input:
a
count =
0
i=1
No
i <= a
Output:
Yes count No non-prime
No a%i =2
== 0 number
Yes Yes
count Output: prime
+= 1 End
number
count Yes
>2
No
i += 1 48
[break] Ex02: Input a number. Check if this number is a
prime number.
Improved version of the previous function
49
[break] Ex02: Input a number. Check if this number is a
prime number.
Another idea: if is a factor of the input number (2 ) then a is not a
prime number.
50
Begi
n [break] Ex02: Input a number. Check if
Input: this number is a prime number.
a
isprime =
Another idea: if is a factor of the input number
False (2 ) then a is not a prime number.
No
a >= 2
Yes
isprime =
True
i=2
No isprime Yes Output: prime
i number
= True
Yes
a%i Yes isprime = No
== 0 False
No
i += 1 Output: non-
prime End
number 51
[break] Ex02: Input a number. Check if this
number is a prime number.
Another idea: if is a factor of the input
number (2 ) then a is not a prime
number.
52
continue statement
• The continue statement is used inside a loop.
• The continue statement skips the current iteration and
starts the next one.
• Typically, the continue statement is used with an if
statement to skip the current iteration once a condition is
True.
else in For/While Loop
• The else keyword in a for or while loop specifies a block
of code to be executed when the loop is finished.
54
else in For/While Loop - Example
58
Exercises
59
1. Find the addition, subtraction, multiplication, and division
of two integers.
2. Compute the perimeter and area of a circle, given its
radius.
3. Check if a year is a leap year. Leap years satisfy one of
two conditions: (1) The year is multiple of 400. (2) The
year is multiple of 4 and not multiple of 100.
4. Compute the surface area and volume of a sphere, given
its radius (Area = , Volume =
5. Given 3 positive integers. Check if they are side lengths of
a triangle. If yes, compute the triangle perimeter and area.
6. Swap the values of two numbers.
60
7. Write a program that allows users input N integers.
a) Compute the sum and average of all numbers.
b) Count the number of all even numbers.
c) Compute the average of all even numbers.
8. Write a program that allows users input integers from keyboard
until a zero is entered. Compute sum of all numbers.
9. Write a program to input a positive integer n (if a non-positive
number is entered, ask for another one). Compute:
a) T = n!
b) S=
c) S=
61