Control Statements1 4

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

FLOW OF CONTROL

Types o f Statement in Python

▪ Statements are the instructions given to


computer to perform any task.
▪ Task may be simple calculation, checking the
condition or repeating action.
▪ Python supports 3 types of statement:
 Empty statement
 Simple statement
 Compound statement
Empty Statement

▪ It is the simplest statement i.e. a statement


which does nothing.
▪ It is written by using keyword – pass
▪ Whenever python encountered pass it does
nothing and moves to next statement in flow
of control
▪ Required where syntax of python required
presence of a statement but where the logic
of program does. More detail will be explored
with loop.
Simple Statement

▪ Any single executable statement in Python is


simple statement. For e.g.
▪ Name = input(“enter your name “)
▪ print(name)
Compound Statement

▪ It represent group of statement which are


executed as unit. The compound statement
of python are written in a specific pattern:

Compound_Statement_Header :
indented_body containing multiple
simple or compound statement
Compound Statement has:

▪ Header which begins with keyword/function


and ends with colon(:)
▪ A body contains of one or more python
statements each indented inside the header
line.
▪ All statement in the body or under any
header must be at the same level of
indentation.
Control Statements
Control statements are used to control the flow of
execution depending upon the specified condition/logic.
The different types of control statements available in Python are:
1. Sequence control statements(Statements are
executed sequentially)
2. Decision Making Statements(Statements are executed
depending upon a condition test)
3. Iteration Statements (Repeated execution of set of
statement depending upon a condition test)(Loops)
4. Jump Statements (break, continue, pass)
Decision Making Statement

Decision making statement used to control the flow of


execution of program depending upon condition.

There are three types of decision making


statement.
1.if statements
2.if-else statements
3.Nested if-else statement
Decision Making Statement
1. if statements
An if statement is a programming statement
that, if proved true, performs a or displays information.

conditional
function
Decision Making Statement
1. if statements
Syntax:
if(condition):
statement
[statements]
e.g.
noofbooks = 2
if (noofbooks == 2):
print('You have ')
print(‘two books’)
print(‘outside of if statement’)
Output
You have two books
Note:To indicate a block of code in Python, you must indent each line
of the block by the same amount. In above e.g. both print statements
are part of if condition because of both are at same level indented but
not the third print statement.
Decision Making Statement
1. if statements
Using logical operator in if
statement
x=1
y=2
if(x==1 and y==2):
print(‘condition matcing the criteria')
Output :-

condition matcing the criteria

a=100
if not(a == 20):
print('a is not equal to 20')

Output :-
a is not equal to 20
Decision Making Statement
2. if-else Statements
If-else statement executes some code if the test expression is
true (nonzero) and some other code if the test expression is
false.
Decision Making Statement
2. if-else Statements
Syntax:
if(condition):
statements
else:
statements
e.g. a=10
if(a < 100):
print(‘less than 100')
else:
print(‘more than equal 100')

OUTPUT
less than 100
*Write a program in python to check that entered numer is even or odd
Decision Making Statement
3. Nested if-else statement
The nested if...else statement allows you to check for
multiple test expressions and execute different codes for
more than two conditions.
Decision Making Statement
3. Nested if-else statement
Syntax
If (condition):
statements elif
(condition):
statements
else:
statements
E.G.
num = float(input("Enter a number: "))
if num >= 0:
if num == 0: print("Zero")
else:
print("Positive number") else:
print("Negative number") OUTPUT
Enter a number: 5 Positive
number
* Write python program to find out largest of 3 numbers.
Program to sort 3 numbers in
ascending order
a = float(input("Enter a: "))
b = float(input("Enter b: "))
c = float(input("Enter c: "))
if a < b:
if b < c:
print (a, “<", b, "<", c)
else:
if a < c:
print (a, "<", c, "<", b)
else:
print (c, "<", a, "<", b)
else:
if c < b:
print (c, "<", b, "<", a)
else:
if c < a:
print (b, "<", c, "<", a)
else:
print (b, "<", a, "<", c)
Iteration Statements (Loops)
Iteration statements(loop) are used to execute a block of
statements as long as the condition is true.
Loops statements are used when we need to run same code
again and again.

Python Iteration (Loops) statements are of three type :-

1.While Loop

2.For Loop

3.Nested For Loops


Iteration Statements (Loops)
1. While Loop
It is used to execute a block of statement as long as a given
condition is true. And when the condition become false, the
control will come out of the loop. The condition is checked every
time at the beginning of the loop.
Syntax
while (condition):
statement
[statements]
e.g.
x=1
while (x <= 4): Output
print(x) 1
2
x=x+1 3
4
Iteration Statements (Loops)

2. For Loop
It is used to iterate over items of any sequence, such as a
list or a string.
Syntax
for val in sequence:
statements
e.g.
for i in range(3,5):
print(i)

Output
3
4
Iteration Statements (Loops)

2. For Loop continue


Example programs
for i in range(5,3,-1):
print(i)

Output
5
4
range() Function Parameters
start: Starting number of the sequence.
stop: Generate numbers up to, but not including this number.
step(Optional): Determines the increment between each numbers in the
sequence.
Iteration Statements (Loops)

2. For Loop continue


For Loop With Else
e.g.
for i in range(1, 4):
print(i)
else: # Executed because no break in for
print("No Break")

Output
1
2
3

No Break
Iteration Statements (Loops)
2. For Loop continue
Nested For Loop
e.g.
for i in range(1,3):
for j in range(1,11):
k=i*j
print (k, end=' ')
print()

Output
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
Iteration Statements (Loops)
2. For Loop continue
Nested For Loop
e.g.
for i in range(1,3):
for j in range(1,11):
k=i*j
print (k, end=' ')
print()

Output
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
Nested For Loop(Example-1):Pattern

for i in range(0,5):
for j in range(0,i+1):
print (‘*’, end=' ')
print()
Output
*
**
** *
****
*****
Nested For Loop(Example-2)

n=5
K = 2*n-2 #no of spaces
for i in range(1,n): #no of rows
for j in range(0,n): #spaces
print(end=“”)
k=k-2
for j in range(0,i+1):
print (‘*’, end=' ')
print(‘\r’)
Output
*
**
** *
****
*****
Nested For Loop(Example-3)

n=5
K = 2*n-2 #no of spaces
for i in range(1,n): #no of rows
for j in range(0,n): #spaces
print(end=“”)
k=k-2
for j in range(0,i+1):
print (‘*’, end=' ')
print(‘\r’)
Output:
*
**
** *
****
*****
Nested For Loop(Example-4)

n = int(input("enter the no of rows"))


k = 2 *n - 2
for i in range(n,-1,-1):
for j in range(k,0,-1):
print(end=" ")
k=k+1
for j in range(0,i+1):
print('*',end=" ")
print("\r")
Output
*****
****
** *
**
*
Nested For Loop(Example-5)

k=1
for i in range(0, 5):
for j in range(0, i+1):
print(k, end=" ")
k=k+1
print()

Output

1
22
333
4444
55555
Jump Statements – break & continue

break statement in python is used to terminate


the containing loop for any given condition.
Program resumes from the statement
immediately after the loop

Continue statement in python is used to skip


the statements below continue statement
inside loop and forces the loop to continue
with next value.
Example – break
for i in range(1,20):
if i % 6 == 0:
break
print(i)
print(“Loop Over”)

The above code produces output


1
2
3
4
5
Loop Over
when the value of i reaches to 6 condition will becomes True and
loop will end and message “Loop Over” will be printed
Example – continue
for i in range(1,20):
if i % 6 == 0:
continue
print(i, end = ‘ ‘)
print(“Loop Over”)

The above code produces output


1 2 3 4 5 7 8 9 10 11 13 14 15 16 17 19
Loop Over
when the value of i becomes divisible from 6, condition will
becomes True and loop will skip all the statement below
continue and continues in loop with next value of iteration.
Q:Differentiate between break and continue???
Loop .. else .. Statement

▪ Loop in python provides else clause with loop


also which will execute when the loop
terminates normally i.e. when the test
condition fails in while loop or when last value
is executed in for loop but not when break
terminates the loop
Example (“else” with for)
names=["allahabad","lucknow","varanasi","kanpur","agra","ghaziabad"
,"mathura","meerut"]
city = input("Enter city to search: ")
for c in names:
if c == city:
print(“City Found")
break Output
else: Enter city to search : varanasi
City Found
print("Not found") Enter city to search : unnao
Not found

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy