4.1 Control Flow Part 2

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

Control Flow

(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

✓ A loop allows us to execute a


group of statements several
times

✓ 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

When to use a For Loop?


Used for iterating over a sequence.
FOR LOOP
- executes a block of code only a certain
number of times. It knows beforehand when the
iteration ends.

When you iterate over a sequence of objects, it


is called traversal.
FOR LOOP
FOR LOOP
FOR LOOP
FOR LOOP

friends = ["Juan", "Pedro", "Maria"]

for friend in friends:


print(friend)

Juan
Pedro
Maria
FOR LOOP
FOR LOOP
FOR LOOP
Range Function

this function is used for generating a


sequence of numbers.
Range Function

range(start, stop, step)


Where:
range is a function name
start is the starting number
stop is where the sequence ends
step-size is the interval between the sequence.
Range Function
Range Function
Range Function
Range Function
Range Function
WHILE LOOP
while loop executes a block of
statements repeatedly as long
as the condition is TRUE.

Syntax of while Loop

while test_expression:
body of while
WHILE LOOP

it is the indefinite type of loop where it depends upon a


test expression before it will iterate or stop the
iteration.
WHILE LOOP
WHILE LOOP
WHILE LOOP
WHILE LOOP
WHILE LOOP
WHILE LOOP
CONTROL STATEMENT
A way to control or override the normal execution of a program.
BREAK STATEMENT

this statement can be used if you wish to


stop the loop. The keyword break is placed
inside the loop to stop it from executing.
BREAK STATEMENT
BREAK STATEMENT
CONTINUE STATEMENT

this statement is used to skip the remaining


code inside a loop for the current iteration
only. It does not end the loop; instead it will
cease to execute the rest of code and will
proceed to the next iteration.
CONTINUE STATEMENT
CONTINUE STATEMENT
PASS STATEMENT
• In Python programming, the pass statement is a
null statement.
• The difference between a comment and
a pass statement in Python is that while the
interpreter ignores a comment entirely, pass is
not ignored.
• However, nothing happens when the pass is
executed. It results in no operation (NOP).
PASS STATEMENT
PASS STATEMENT
Suppose we have a loop or a function that is
not implemented yet, but we want to implement
it in the future. They cannot have an empty
body. The interpreter would give an error. So,
we use the pass statement to construct a body
that does nothing.
PASS STATEMENT
What is an Error?

An error is a mistake
or an action that is incorrect.

Syntax Error Semantic Error Run-time Error

● 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.

When an exception is encountered, the program


stops executing and raises an exception object.
try except
tests a block handles the
of code exception
Exceptions

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:

•TypeError: raised when an operation or function is


applied to an object of inappropriate type.
•ValueError: raised when an operation or function
receives an argument of the correct type but an
inappropriate value.
•ZeroDivisionError: raised when trying to divide a
number by zero.
Example > TypeError:

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

You might also like