Final Copy Flow of Control Chapter-6
Final Copy Flow of Control Chapter-6
Q)What is Indentation?
Leading whitespace (spaces and tabs) at the beginning of a statement is called indentation.
Python uses indentation for block as well as for nested block structures.
In Python, the same level of indentation associates statements into a single block of
code.
The interpreter checks indentation levels very strictly and throws up syntax errors if
indentation is not correct.
It is a common practice to use a single tab for each level of indentation.
Note:
In most programming languages, the statements within a block are put inside curly
brackets, but in python indentation is used for block of statement .
1) if statement
If condition is true then statements will be executed otherwise statements has to be
ignored.
The if statement is also called as one-way branching statement.
Flowchart
True
syntax
if condition:
statement(s)
False
Program 1:
Eligible to vote
Program 4:
Output 1 Output 2
a=int(input(“Enter first number:”))
Enter first number: 56 Enter first number: 60
b=int(input(“Enter second number”))
Enter second number:20 Enter second number:85
if a>b:
a is greater than b
print(“a is greater than b”)
large=a
Output 2:
if large < b:
Enter first number:100
large = b
Enter second number:65
print("The largest number is" , large)
The largest number is 100
2)if-else statement
If condition is True then statement 1 will be executed otherwise if condition is False
then statement 2 will be executed.
if condition is True then if block of statement will be executed.
If condition is False then else block of statement will be executed.
The if – else statement is also called as two-way branching statement.
Flowchart
Syntax
False True
if condition:
statement 1
else:
statement 2
else:
print("Odd number")
print("Negative number")
syntax
3)if- elif statement(if-else-if statement)
if condition 1:
Number of elif is dependent on the number of conditions to
be checked. statement 1
If the first condition is False, then the next condition is elif condition 2:
checked, and so on.
statement 2
If one of the conditions is True, then the corresponding
indented block executes, and the if statement terminates. elif condition 3:
It is also called as multiway branching statement statement 3
The if-elif is also called as else if ladder
Flowchart else:
statement 4
Output 1:
Program 1:Grade the students based on percentage
Enter the percentage:75
marks
Grade C
percent=int(input("Enter percentage of marks:"))
print("Grade B")
Output 3:
elif(percent >= 70):
Enter the percentage:92
print("Grade C")
Grade A
elif(percent >= 60):
Output 4:
print("Grade D")
Enter the percentage:55
else:
Grade E
print("Grade E")
print("Number is zero")
Program to find the largest among three number using Output 1
elif statement
Enter first number: 50
a=int(input("Enter first number: "))
Enter second number: 78
b=int(input("Enter second number: "))
Enter third number: 90
c=int(input("Enter third number: "))
The largest number is 90
if a > b and a > c:
large = a Output 2
elif b>c: Enter first number: 100
large = b Enter second number: 56
else: Enter third number: 88
large = c The largest number is 100
print("The largest number is",large)
Statement 2
----------------
Body of while loop
----------------
Statement n
Output:
Write program to print Output: Write program to print Rohit kumar
first 5 natural numbers yours name 8 times
1 Rohit kumar
using while loop. using while loop.
2 Rohit kumar
count = 1 count = 1
3 Rohit kumar
while count <= 5: while count <= 8:
4 Rohit kumar
print(count) print(“Rohit kumar”)
5 Rohit kumar
count =count + 1 count =count + 1
Rohit kumar
Rohit kumar
The control condition of the while loop is tested before any statement inside the loop
is executed.
3
Suruchi Padhy (Mtech CSE, B.Tech IT ) Dept. of Computer Science
4
5
Chapter 6- Flow of control 10 | P a g e
After each iteration, the control condition is tested again and the body of the loop
executed as long as the condition remains true.
When this condition becomes false, the statements in the body of loop are not
executed and the control is transferred to the statement immediately following the
body of while loop.
If the condition of the while loop is initially false, the body of the loop is not executed
even once.
Note:
B) for loop
The for statement is used to iterate over a range of values or a sequence.
The for loop is executed for each of the items in the range or sequence.
for loop is used ,when it is known in advance the number of times the loop will
execute.
With every iteration of the loop, the control variable (item) checks whether each of
the values in the range have been traversed or not.
When all the items in the range are exhausted or sequence, the statements within
loop are not executed; the control is then transferred to the statement immediately
following the for loop.
These item values can be either numeric, or they can be elements of sequence data
type like a string, list, or tuple.
(A) Syntax of the For Loop
statement 1
is
statement 2 Condition
?
----------------
statement N
Body of for loop
5 Rohit kumar
Rohit kumar
Rohit kumar
Rohit kumar
print(num) 20
30
40
50
Output:
x=[20,80,95,64,32] 20
for item in x: 80
print(item, end =”\n”) 95
64
x=[20,80,95,64,32] 32
print(item, end=” ”) 20 80 95 64 32
10 is an even Number
Ravi
y = ["Ravi","Anjali","Mohan","Sonu"]
Anjali
for item in y:
Mohan
print(item)
Sonu
16
80.65
Example result
range(8) 0,1,2,3,4,5,6,7
range(0,8,1) 0,1,2,3,4,5,6,7
range(5,15,2) 5,7,9,11,13
range(10,30,5) 10,15,20,25
range(35,50,4) 35,39,43,47
range(0,-8,-1) 0,-1,-2,-3,-4,-5,-6,-7
range(-4,-15,-2) -4,-6,-8,-10,-12,-14
Program to show the uses of range() function in for loop Example
0 1 2 3 4 >>> list(range(10))
print(num, end=” ”)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1 [2, 3, 4, 5, 6, 7, 8, 9]
2
#step value is 5
3
>>> list(range(0, 30, 5))
4
[0, 5, 10, 15, 20, 25]
The break statement is used when we The continue statement is used when
may want to exit from a loop (comeout we don’t want to execute a part of
of the loop forever) statements in a loop.
Program to show the uses of break statement Program to show the uses of continue
within while loop statement within while loop
count=1 Output count=0 output
while count <= 10: while count<=10:
1 6
if count == 5: count = count+1
break if count<=5: 7
2
print(count) continue
8
count=count+1 3 print(count)
print("Out of the loop") print("out of the loop") 9
4
10
Out of the
loop 11
out of the
loop
Program to show the uses of break statement Program to show the uses of continue
within for loop output statement within for loop
for count in range(1,11): for count in range(0,11): output
if count==5: 1 if count <= 5: 6
break 2 continue
print(count) print(count) 7
print("Out of the loop") 3 print("Out of the loop") 8
4
9
Out of the loop
10
Nested loop
A loop may contain another loop inside it. A loop inside another loop is called a nested
loop.
When outer loop will be executed one time , then inner loop will be executed
completely.
In nested for loop outer for loop keep track of row number and inner for loop keep
track of column number.
Python does not impose any restriction on how many loops can be nested inside a
loop or on the levels of nesting.
Any type of loop (for/while) may be nested within another loop (for/while).
Program to print the pattern for a number input by the user
1 2 3
print()
1 2 3 4
1 2 3 4 5
program Output:
row = int(input("Enter the row size: ")) Enter the row size = 5
* * *
print(*, end = " ")
* * * *
print(end=”\n”)
* * * * *
program Output:
row = int(input("Enter the row size: ")) Enter the row size = 5
1
for i in range(1 , row + 1):
2 2
for j in range(1 , i + 1):
3 3 3
print(i, end = " ")
4 4 4 4
print(end=”\n”)
5 5 5 5 5
program output
1 2 3 4 5
#print the patteren 1 2 3 4
rows=5 1 2 3
1 2
for i in range(rows, 0, -1): 1
for j in range(1, i+1):
print( end=”\n”)
program Output
n=int(input("Enter the value of n:")) Enter the value of n: 6
for i in range(1,n+1): Output
* * * * * *
print("*", end=" ") Enter the value of n: 6
*
program
*
n=int(input("Enter the value of n:"))
*
for i in range(1,n+1):
*
print("*",end="\n")
*
for j in range(1,col+1): * * * *
print("*",end=" ") * * * *
print(end="\n") * * * *
output
program
Enter row size:4
row=int(input("Enter row size:"))
Enter column size:3
col=int(input("Enter column size:"))
Rohit Rohit Rohit
for i in range(1,row+1):
Rohit Rohit Rohit
for j in range(1,col+1):
Rohit Rohit Rohit
print("Rohit",end=" ")
Rohit Rohit Rohit
print(end="\n")
program
Enter row size:4
row=int(input("Enter row size:"))
Enter column size:6
col=int(input("Enter column size:"))
1 1 1 1 1 1
for i in range(1,row+1):
2 2 2 2 2 2
for j in range(1,col+1):
3 3 3 3 3 3
print( i ,end=" ")
4 4 4 4 4 4
print(end="\n")
for i in range(1,row+1): 1 2 3 4 5 6
for j in range(1,col+1): 1 2 3 4 5 6
print(end="\n")
Suruchi Padhy (Mtech CSE, B.Tech IT ) Dept. of Computer Science
Chapter 6- Flow of control 18 | P a g e
program program
count=1
while count <= 10: for count in range(1,10):
print(count , end=" ") print(count , end=" ")
count=count + 1
output
1 2 3 4 5 6 7 8 9 10
N! = 1x2x3x4x---------------x N
Output 1 Output 2
Enter a number:4 Enter a number:5
The factorial of a number 24 The factorial of a number 120
program program
n = int(input("Enter the value of N:")) n = int(input("Enter the value of N:"))
count =1 sum=0
sum=0 for count in range(1,n+1):
while(count <= n): sum = sum + count
sum = sum + count print("The sum of N natural number",sum)
count = count + 1
print("The sum of N natural number",sum)
Output 1: Output 2:
Enter the value of N: 5 Enter the value of N:10
The sum of N natural number 15 The sum of N natural number 55
5. Write a program to print N natural number .
1,2,3,4,5,6,7,8,9,10,11,12,-----------------------------------------------------------N
Program program
n = int(input("Enter the value of N:")) n = int(input("Enter the value of N:"))
count=1 for count in range(1,n+1):
while count <= n: print(count , end=" ")
print(count , end=" ")
count=count + 1
Output
Enter the value of N:20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Program program
n = int(input("Enter the value of N:")) n = int(input("Enter the value of N:"))
count=1 for count in range(1,n+1):
while count <= n: print(“Rohit”)
print(“Rohit”)
count=count + 1
Output
Enter the value of N:5
Rohit
Rohit
Rohit
Rohit
Rohit