Flow of Control

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

Flow of Control

Introduction
The order of execution of the statements in a program is known as flow of control.

The flow of control can be implemented using control structures.

Python supports two types of control structures—selection and repetition.


Indentation
Types of Flow of Control
Types of Statement / Conditional Statements
1. If statement

An if statement is used for conditional execution of code blocks.

It allows you to execute certain code only if a specified condition is true.

The basic syntax of an if statement is:


If..Else Statement
● if-else statement is used to control the flow of a program based on certain
conditions.
● It allows you to execute different blocks of code depending on whether a
given condition is True or False.
● Syntax:
The If- Elif - else statement
● the if, elif, and else statements are used

for conditional execution of code blocks.

● They allow you to make decisions

in your code based on certain conditions.


Nested IF statements
● Any number of these statements

can be nested inside one another.

● Indentation is the only way to

figure out the level of nesting.


WAP to check whether the given number is
positive, negative, or 0.
Practical Record Programs
Conditional Statement Programs

1. WAP to check whether the number is even or odd


2. WAP to check the grade of a student by accepting 4 subjects
marks out of 50 from user.
Hint: Score greater than or equal to 90 is A grade
Score greater than or equal to 80 is B grade
Score greater than or equal to 70 is C grade
Score greater than or equal to 60 is D grade
3. Greatest among two numbers
Iterative/Repetitive/ Looping statements
Iterative statements in Python are used to execute a block of code repeatedly as
long as a certain condition is satisfied.

There are 2 types of loops in Python:

● for loop
● while loop
For Loop
A for loop allows you to repeat a block of code a specific number of times or
iterate over a sequence of values

Syntax

for variable in sequence:

# Code to be executed during each iteration

● for is the keyword that starts the loop.


● variable is a placeholder that represents the current value
● sequence is what you're iterating over.
range () function
Syntax
range(lowerlimit , upperlimit,steps)
Example
for i in range(1, 6): for i in range(1, 6,2):

print(i) print(i)

1 1

2 3

3 5

5
1. Write a program to accept a number from user and print the multiple of the
number.
2. Write a program to print all odd numbers between 1 to 100.
3. Write a program to print the number and it square.

4. Python program to calculate the sum of all numbers from 1 to a given number.
5. Python program to check the number is prime or not.
6. Python program to display numbers from a list using a “for” loop.
Pyramid pattern of numbers

1 1
12 22
123 333
1234 4444
12345 55555
number = int(input("Enter the Number: "))

sum = 0

for value in range(1, number + 1):

sum = sum + value

print(sum)
number = int(input("Enter the Number: "))

factorial = 1

for i in range(1,n + 1):

factorial = factorial*i

print("The factorial of",n,"is",factorial)


WAP to find average of set of numbers within a
range
WAP to find total number of even numbers within
a range
while Loop
A loop that executes a single statement or a group of statements for the given true
condition. The keyword used to represent this loop is "while".

A "while" loop is used when the number of iterations is unknown. The statement
repeats itself till the boolean value becomes false.

Syntax

while expression:

statement(s)
Example
count = 0
The count is: 0
while (count < 9): The count is: 1
The count is: 2
print 'The count is:', count The count is: 3
The count is: 4
count = count + 1
The count is: 5
The count is: 6
The count is: 7
print "Good bye!" The count is: 8
Good bye!
1. Program to print odd numbers from 1 to 10
2. Find the sum of even numbers using while loop
3. Write a program to reverse a number.
4. WAP to check whether the number is a palindrome or not.
5. WAP to check whether the string is Palindrome or not.
Loop Control Statements

Loop control statements change execution from their normal


sequence. When execution leaves a scope, all automatic
objects that were created in that scope are destroyed. Python
supports the following control statements.

1. break - jump statement


2. continue - jump statement
3. pass
We can stop the loop even if the while condition is true:

The break Statement

i=1
while i < 6:
print(i) 1
if i == 3: 2
break 3
i += 1
We can stop the current iteration, and continue with the next:

The continue Statement

i = 0
while i < 5: 1
i += 1 2
if i == 3: 4
continue
print(i)
The pass Statement

The pass statement does nothing, and is essentially a null statement.

Nothing happens when the pass is executed. It results in no operation


(NOP).

for n in range(0, 5):

if n == 2:

pass 0 1 3 4

else:

print(n)
The else Clause

while <expr>:

<statement(s)>

else:

<additional_statement(s)>
Python - Exceptions Handling

When an error occurs, or exception as we call it, Python will normally stop and
generate an error message.

These exceptions can be handled using the try and except statement:

The try block lets you test a block of code for errors.

The except block lets you handle the error.


try :

#statements in try block

except :

#executed when error in try block

You might also like