0% found this document useful (0 votes)
10 views

Control Flow

Uploaded by

supriya sundaram
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Control Flow

Uploaded by

supriya sundaram
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Control Statements

• Control Flow
 A program's control flow is the order in which the program's
code executes.
 The control flow of a Python program is regulated
by conditional statements, loops, and function calls.
• Python has three types of control structures:
 Sequential - default mode
 Selection - used for decisions and branching
 Repetition - used for looping, i.e., repeating a piece
of code multiple times.
Control Statements
• 1. Sequential
• Sequential statements are a set of statements whose execution
process happens in a sequence.
• The problem with sequential statements is that if the logic has
broken in any one of the lines, then the complete source code
execution will break.
 Example

## This is a Sequential statement

a=20
b=10
c=a-b
print("Subtraction is : ",c)
Control Statements
• 2. Selection/Decision control statements
• The selection statements are also known as Decision control
statements or branching statements.
• The selection statement allows a program to test several
conditions and execute instructions based on which condition is
true.
• Some Decision Control Statements are:
– Simple if
– if-else
– nested if
– if-elif-else
Control Statements
• Simple if:
– If statements are control flow statements that help us to run a particular
code, but only when a certain condition is met or satisfied. A simple
if only has one condition to check.
• Syntax

If condition :

true statements
Control Statements
• Simple if

• Example

n = 10
if n % 2 == 0:
print("n is an even number")
Control Statements
• if-else:
– The if-else statement evaluates the condition and will execute the body
of if , if the test condition is True, but if the condition is False, then the
body of else is executed.
• Syntax

If condition :
true statements
else:
false statements
Control Statements

• Example

n=5
if n % 2 == 0:
print("n is even")
else:
print("n is odd")
Control Statements
• Nested if:
• Nested if statements are an if statement inside another if
statement.
Control Statements
• Nested if:
• Nested if statements are an if statement inside another if
statement.
• Syntax
If condition1:
if condition2:
true statement for condition1,2
else:
false statement for condition2
else:
false statement for condition1
Control Statements
• Example

a=5
b = 10
c = 15
if a > b:
if a > c:
print("a value is big")
else:
print("c value is big")
elif b > c:
print("b value is big")
else:
print("c is big")
Control Statements
• if-elif-else:
• The if-elif-else statement is used to conditionally execute a
statement or a block of statements.

If condition1:
true statements
elif condition2:
true statement for condition2
else:
false statement for cnodition2
Control Statements
• Example

x = 15
y = 12
if x == y:
print("Both are Equal")
elif x > y:
print("x is greater than y")
else:
print("x is smaller than y")
Control Statements
• 3. Repetition

– A repetition statement is used to repeat a group(block) of programming

instructions.

• In Python, we generally have two loops/repetitive statements:

– for loop

– while loop
Control Statements
for loop:
A for loop is used to iterate over a sequence that is either a list, tuple,
dictionary, or a set.
We can execute a set of statements once for each item in a list, tuple, or
dictionary.

Syntax:

for val in sequence:


loop body

Where , val is the variable that takes the value of the item inside the
sequence on each iteration.
Control Statements
for loop Operation:
Loop Operation:

 the loop runs the body lines again and again, once for each element
in the collection.

Each run of the body is called an "iteration" of the loop.

For the first iteration, the variable is set to the first element, and the body
lines run.
Control Statements
for loop Example:

1. Loop Over range()

Combine a for-loop with range() to loop over a series of numbers

for i in range(10):
print(i)

#0123456789
Control Statements
for loop Example:
2. Loop Over All Index Numbers
For a linear collections (e.g. list, string), index numbers 0, 1, ... len-1 indentify
each element in the collection.
The following loop combines for/range/len to run a variable i through all the
index numbers in a string or list.
# string s, loop though all its chars by index number

s = 'Hello'
for i in range(len(s)):
# i.e. range(5)
print(i, s[i]) 0 H
1 e
2 l
3 l
4 0
Control Statements
for loop:
A for loop is used to iterate over a sequence that is either a list, tuple,
dictionary, or a set.
We can execute a set of statements once for each item in a list, tuple, or
dictionary.

lst = [1, 2, 3, 4, 5]
for i in range(len(lst)):
print(lst[i], end = " ")

for j in range(0,10):
print(j, end = " ")
Control Statements
while loop:
In Python, while loops are used to execute a block of statements repeatedly until a
given condition is satisfied.
Then, the expression is checked again and, if it is still true, the body is executed
again. This continues until the expression becomes false.
Syntax :

initialization
while test_expression:
Body of the loop
iteration
Control Statements
while loop:
In Python, while loops are used to execute a block of statements repeatedly until a
given condition is satisfied.
Then, the expression is checked again and, if it is still true, the body is executed
again. This continues until the expression becomes false.

m=5
i=0
while i < m:
print(i, end = " ")
i=i+1
print("End")
Control Statements

Break and continue

•It is used to alter the normal loop.


•Loops iterate over a block of code until the test expression is false,
but sometimes we wish to terminate the current iteration or even the
whole loop without checking test expression.
•The break and continue statements are used in these cases.
Control Statements

Python break statement


The break statement terminates the loop containing it.
 Control of the program flows to the statement immediately after
the body of the loop.
If the break statement is inside a nested loop (loop inside another
loop), the break statement will terminate the innermost loop.

Syntax of break

break
Control Statements

# Use of break statement inside the


loop

for val in "string":


if val == "i":
break
print(val)

print("The end")
Flow chart for break statements

Example
Control Statements

Python continue statement


The continue statement is used to skip the rest of the code inside a
loop for the current iteration only.
Loop does not terminate but continues on with the next iteration.

Syntax of continue
continue
Control Statements

# Program to show the use of


continue statement inside loops

for val in "string":


if val == "i":
continue
print(val)

print("The end")

Flow chart for continue statements Example

You might also like