100% found this document useful (1 vote)
29 views19 pages

Final Copy Flow of Control Chapter-6

Uploaded by

rokum9432
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
29 views19 pages

Final Copy Flow of Control Chapter-6

Uploaded by

rokum9432
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Chapter 6- Flow of control 1|Page

Chapter 6: Flow of Control

Key point to remember:

 The if statement is used for selection or decision making.


 The looping constructs while and for allow sections of code to be executed repeatedly
under some condition.
 for statement iterates over a range of values or a sequence.
 The statements within the body of for loop are executed till the range of values is
exhausted.
 The statements within the body of a while are executed over and over until the condition
of the while is false.
 If the condition of the while loop is initially false, the body is not executed even once.
 The statements within the body of the while loop must ensure that the condition
eventually becomes false; otherwise, the loop will become an infinite loop, leading to a
logical error in the program.
 The break statement immediately exits a loop, skipping the rest of the loop’s body.
Execution continues with the statement immediately following the body of the loop.
 When a continue statement is encountered, the control jumps to the beginning of the
loop for the next iteration.
 A loop contained within another loop is called a nested loop.

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 .

Suruchi Padhy (Mtech CSE, B.Tech IT ) Dept. of Computer Science


Chapter 6- Flow of control 2|Page

Q) What is flow of control?


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 or control


statement.
 There are two types of control structures is used while writing python programs.
 They are Selection and Repetition structures.

1.Selection structure(decision or branching 2.Repetition structure(looping or iteration


statement) statement)
I) if statement (one-way branching) I) while loop
II) if else statement (two-way branching) II) for loop
III) if elif statement ( multi way branching)
IV) nested if statement

Q) Define selection structure /statement. Explain different types of selection


statement with syntax and programming example.
Selection structure:
 Selection statement is also called as decision statement /branching statement.
 Selection statement make decisions to execute a block of statement based on the
True values for the condition among several alternatives conditions.
 The condition is either a relational expression or logical expression which is
evaluated into True or False

Types of selection statement


1. if statement
2. if-else statement
3. if-elif statement (if else if statement or else-if ladder)
4. Nested if statement

Suruchi Padhy (Mtech CSE, B.Tech IT ) Dept. of Computer Science


Chapter 6- Flow of control 3|Page

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:

age = int(input("Enter your age "))

if age >= 18:


Output 1: Output 2
print("Eligible to vote")
Enter your age:14 Enter your age:20

Eligible to vote

Program 2: Output 1: Output 2


num=int(input("Enter a number:")) Enter a number: -98 Enter a number:56
if num > 0 : Positive number
print("Positive number")

Program 3: Output 1 Output 2


num=int(input("Enter a number:")) Enter a number:56 Enter a number: 19
if num % 2 == 0 : Even number
print("Even number")

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”)

Suruchi Padhy (Mtech CSE, B.Tech IT ) Dept. of Computer Science


Chapter 6- Flow of control 4|Page

Write a program to find largest among two Output 1:


number using if statement Enter first number:70
a=int(input("Enter first number: ")) Enter second number:85
b=int(input("Enter second number: ")) The largest number is 85

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

Write a program to find largest among three Output 1


number using if 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
large=a

if large < b: Output 2


large = b Enter first number: 100
if large < c: Enter second number: 56
large = c Enter third number: 88
print("The largest number is" , large) The largest number is 100

Write a program to find smallest among Output 1:


two number using if statement
Enter first number:70
a=int(input("Enter first number: "))
Enter second number:85
b=int(input("Enter second number: "))
The smallest number is 70
small=a
if small > b: Output 2:

small = b Enter first number:100

print("The smallest number is" , small) Enter second number:65

The smallest number is 65

Suruchi Padhy (Mtech CSE, B.Tech IT ) Dept. of Computer Science


Chapter 6- Flow of control 5|Page

Write a program to find smallest among


three number using if statement
Enter first number: 80
a=int(input("Enter first number: "))
Enter second number: 56
b=int(input("Enter second number: "))
Enter third number: 25
c=int(input(“Enter third number:”))
The smallest number is 25
small=a
if small > b:
small = b
if small > c:
small=c
print("The smallest number is" , small)

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

Program to check eligibility for vote

age = int(input("Enter your age: "))

if age >= 18:

print("Eligible to vote") Output 1: Output 2:

else: Enter your age:15 Enter your age:20

print("Not eligible to vote") Not Eligible to vote Eligible to vote

Suruchi Padhy (Mtech CSE, B.Tech IT ) Dept. of Computer Science


Chapter 6- Flow of control 6|Page

Program to find largest among two Output 1:


number using if else statement
Enter first number:200
a=int(input("Enter first number: "))
Enter second number:500
b=int(input("Enter second number: "))
The largest number is 500
if a > b :
Output 2:
large = a
Enter first number:88
else:
Enter second number:45
large = b
The largest number is 88
print("The largest number is",large)

Program to check whether the number is


even or odd
num=int(input("Enter a number:")) Output 1: Output 2:
if num % 2 == 0 : Enter a number: 59 Enter a number: 26
print("Even number") Odd number Even number

else:
print("Odd number")

Program to check whether the number is


positive or negative using if else statement

num=int(input("Enter a number:")) Output 1: Output 2:


if num > 0 : Enter a number: 78 Enter a number: -45
print("Positive number") Positive number Negative number
else:

print("Negative number")

Program using if else statement


a=int(input(“Enter first number:”))
Output 1 Output 2
b=int(input(“Enter second number”))
Enter first number: 56 Enter first number: 78
if a>b:
Enter second number:20 Enter second number:100
print(“a is greater than b”)
a is greater than b a is lesser than b
else:
print(“a is lesser than b”)
Suruchi Padhy (Mtech CSE, B.Tech IT ) Dept. of Computer Science
Chapter 6- Flow of control 7|Page

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:"))

if(percent >= 90): Output 2:

print("Grade A") Enter the percentage:68

elif(percent >= 80): Grade D

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")

Suruchi Padhy (Mtech CSE, B.Tech IT ) Dept. of Computer Science


Chapter 6- Flow of control 8|Page

Program 2:Display the appropriate message as per Output 1:


the colour of signal at the road crossing. Enter the colour:green
signal = input("Enter the colour: ") Go
if( signal == "red"):
Output 2:
print("STOP")
Enter the colour:orange
elif (signal == "orange"):
Be slow
print("Be Slow")

elif (signal == "green"): Output 3:

print("Go") Enter the colour:blue

else: No match found

print(“No match found”)

Program 3:Check whether a number is positive,


negative, or zero.
Output 1: Output 2:
num = int(input("Enter a number: ")
Enter a number: -98 Enter a number: 200
if num > 0:
Number is negative Number is positive
print("Number is positive")

elif num < 0: Output 3:

print("Number is negative") Enter a number: 0

else: Number is zero

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)

Suruchi Padhy (Mtech CSE, B.Tech IT ) Dept. of Computer Science


Chapter 6- Flow of control 9|Page

Q) What is looping statement? Explain while and for loop with


syntax and programming example
Repetition structure or looping statement
 The looping statement is also called as an iterative statement or repetitive
statement.
 The looping statements is used to execute a set of statement repeatedly based on
the True values of conditions(expression) .
 So the loop will stop executing statement when the condition became False.
 There two looping statement in python
1. while loop 2. for loop

A)The while Loop


 The while loop executes a block of code repeatedly as long as the control condition
of the loop is true.
Syntax of while Loop
while ( condition) :
is
Statement 1 Condition
?

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:

 Body of the loop is indented with respect to the while statement.


 Similarly, the statements within if are indented with respect to positioning of if
statement
 The statements within the body of the while loop must ensure that the condition
eventually becomes false
 Otherwise the loop will become an infinite loop, leading to a logical error in the
program

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

for item in <sequence/range>:

statement 1
is
statement 2 Condition
?
----------------

statement N
Body of for loop

Suruchi Padhy (Mtech CSE, B.Tech IT ) Dept. of Computer Science


Chapter 6- Flow of control 11 | P a g e

Write program to print Output: Output:


Write a program to
first 5 natural numbers 1 print yours name 8 Rohit kumar
using for loop. times using for loop.
2 Rohit kumar
for count in range(1,6): for count in range(1,9):
3 Rohit kumar
print(count) print(“Rohit kumar”)
4 Rohit kumar

5 Rohit kumar

Rohit kumar

Rohit kumar

Rohit kumar

program to print character in a string


Program to print the characters in the
using for loop
string ‘PYTHON’ using for loop.
3
str=”Welcome” Output:
for letter in 'PYTHON': Output:
4
for x in str: W
print(letter) P
5 e
print(x)
Y
l
T
c
H
o
O
m
N
e

Program to print the numbers in a given sequence using for loop.

count = [10,20,30,40,50] Output:

for num in count: 10

print(num) 20

30

40

50

Suruchi Padhy (Mtech CSE, B.Tech IT ) Dept. of Computer Science


Chapter 6- Flow of control 12 | P a g e

Program to print list of item using for loop

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

for item in x: Output:

print(item, end=” ”) 20 80 95 64 32

Program to print even numbers in a given sequence using for loop.

numbers = [1,2,3,4,5,6,7,8,9,10] Output: 2 is an even Number

for num in numbers: 4 is an even Number

if (num % 2) == 0: 6 is an even Number

print(num,”is an even Number”) 8 is an even Number

10 is an even Number

Program to print list of name using for loop


output

Ravi
y = ["Ravi","Anjali","Mohan","Sonu"]
Anjali
for item in y:
Mohan
print(item)
Sonu

Program to print student tuple using for loop


Output
student=("Rohit","I puc","CEBA",16,80.65) Rohit
for element in student: I puc
print(element) CEBA

16

80.65

Suruchi Padhy (Mtech CSE, B.Tech IT ) Dept. of Computer Science


Chapter 6- Flow of control 13 | P a g e

(B) The range() Function


Q)Define range() function.Explain with syntax and example.
The range() is a built-in function in Python. The range() function is used to create a list
containing a sequence of integers from the given start value upto stop value (excluding
stop value), with a difference of the given step value.

 Syntax of range() function is:


range(start, stop, step)
 In function range(), start, stop and step are parameters.
 The start and step parameters are optional.
 If start value is not specified, by default the list starts from 0.
 If step is also not specified, by default the value increases by 1 in each iteration.
 All parameters of range() function must be integers.
 The step parameter can be a positive or a negative integer excluding zero.
 The function range() is often used in for loops for generating a sequence of numbers

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

for num in range(5): Output: #start and step not specified


Example:

0 1 2 3 4 >>> list(range(10))
print(num, end=” ”)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

for num in range(5): Output: #default step value is 1

print(num) 0 >>> list(range(2, 10))

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]

# step value is -1. Hence, decreasing, sequence is generated

>>> list (range (0, -9, -1))

[0, -1, -2, -3, -4, -5, -6, -7, -8]

Suruchi Padhy (Mtech CSE, B.Tech IT ) Dept. of Computer Science


Chapter 6- Flow of control 14 | P a g e

Q) Compare break and continue statement


 The break and continue statement are called as jump statement
 Both break and continue statement alters the normal flow of execution
 Both break and continue statement used within a loop.

break statement continue statement


 The break statement is used to  The continue statement is used to skip
terminate the loop. part of the loop without executing the
statements.
Syntax: break Syntax continue
 The break statement transfer the  The continue transfer the control to the
control out of the loop. beginning of loop
 The break is used within a loop.  The continue also used within a loop

 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

out of the loop

Suruchi Padhy (Mtech CSE, B.Tech IT ) Dept. of Computer Science


Chapter 6- Flow of control 15 | P a g e

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

row = int(input("Enter the row size: ")) Output:

for i in range(1 , row + 1): Enter the row size = 5

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

print(j, end = " ") 1 2

1 2 3
print()
1 2 3 4

1 2 3 4 5

Note 1: In the pattern program


Note 2
i keep tracks of row number
print() is same as print(end="\n")
j keeps track of column number.

print the patteren

program Output:

row = int(input("Enter the row size: ")) Enter the row size = 5

for i in range(1 , row + 1): *

for j in range(1 , i + 1): * *

* * *
print(*, end = " ")
* * * *
print(end=”\n”)
* * * * *

Suruchi Padhy (Mtech CSE, B.Tech IT ) Dept. of Computer Science


Chapter 6- Flow of control 16 | P a g e

print the patteren

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

Lab program part A (11)

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(j , end=" ")

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")
*

Suruchi Padhy (Mtech CSE, B.Tech IT ) Dept. of Computer Science


Chapter 6- Flow of control 17 | P a g e

Print the following patterns


output
program Enter row size: 5
row=int(input("Enter row size:")) Enter column size: 4
col=int(input("Enter column size:")) * * * *
for i in range(1,row+1): * * * *

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")

program Enter row size:4

row=int(input("Enter row size:")) Enter column size:6

col=int(input("Enter column size:")) 1 2 3 4 5 6

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( j ,end=" ") 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

Programs using for loop and while loop


1. Write a program to print 1 to 10 (increment looping)

using while loop using for loop

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

2. Write a program to print 10 to 1 (decrement looping)

using while loop using for loop


program: program
count=10 for count in range(10 , 0 , -1):
while count > 0: print(count, end=” “)
print(count , end=” ”)
count=count - 1
output:
10 9 8 7 6 5 4 3 2 1

3. Write a program to find factorial of a number

N! = 1x2x3x4x---------------x N

using while loop using for loop


program program
num=int(input("Enter a number:")) num=int(input("Enter a number:"))
count=1 fact=1
fact=1 for count in range(1,num+1):
while(count <= num): fact=fact*count
fact=fact*count print("The factorial of a number",fact)
count=count+1
print("The factorial of a number",fact)

Output 1 Output 2
Enter a number:4 Enter a number:5
The factorial of a number 24 The factorial of a number 120

Suruchi Padhy (Mtech CSE, B.Tech IT ) Dept. of Computer Science


Chapter 6- Flow of control 19 | P a g e

4. Write a program to print sum of N natural number


sum=1+2+3+4+-----------------------------------+N

using while loop using for loop

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

using while loop using for loop

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

6. Write a program to print your name N times

using while loop using for loop

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

Suruchi Padhy (Mtech CSE, B.Tech IT ) Dept. of Computer Science

You might also like