4.1 Control Flow Part 2
4.1 Control Flow Part 2
4.1 Control Flow Part 2
(Looping Statement)
Outline
1. Control Flow
❑ Looping Statement
• For Loop
➢ Range Function
• While Loop
2. Control Statement
❑ Break statement
❑ Continue Statement
❑ Pass statement
3. Error Handling
LOOPING STATEMENTS
✓ It executes a statement
repeatedly until a given
condition is satisfied
REPETITION STRUCTURES
TYPES OF LOOPS
WHILE
FOR
FOR LOOP
for loop execute the body of the loop
in a definite number of times and is
used for iterable objects such as
string, list, tuple, etc.
Syntax of for Loop
for var in sequence:
loop body
FOR LOOP
Juan
Pedro
Maria
FOR LOOP
FOR LOOP
FOR LOOP
Range Function
while test_expression:
body of while
WHILE LOOP
An error is a mistake
or an action that is incorrect.
● Incorrect
Not following ● Issues found
●
operation.
the rules. during
● Inaccurate
execution.
results.
Exceptions
In Python, an exception is an error that occurs during
the execution of a program.
In this code, the try block contains the code that might raise an
exception. If an exception is raised, the code in the except block
will be executed. The ExceptionType specifies the type of
exception that should be caught. If you don't specify an exception
type, the except block will catch all exceptions.
Example:
Example:
There are different types of exceptions in Python,
such as:
In the second line, we are trying to add a string and an integer, which
will raise a TypeError.
Example > ValueError:
In the first line, we are trying to convert the string "five" to an integer
using the int() function, which will raise a ValueError.
Example > ZeroDivisionError:
In the third line, we are trying to divide the value of x by the value of
y, which is zero. This will raise a ZeroDivisionError.
Seatwork # 2
Write a Python program that will ask the
user five (5) times using a WHILE Loop
to input the full name, age, and gender
of a person, then only display all the
data when the loop ends.
NOTE: Apply break statement