Flow Control Statement Of Python
Flow Control Statement Of Python – Flow control describe the order in which statements will be executed at runtime.
These are of 3 types :
- Conditional Statements
- Iterative Statements
- Transfer Statements
Conditional Statements :
If we have to write a program in which we want to execute a code on behalf of conditions then we come for Conditional Statements.
We have 3 types of Conditional Statements :
In this statement we have only 1 condition. If the condition is true the program will be executed otherwise, it come out from the program.
Q => How to write a program which executed if right name given ?
name = input(“Enter your Name :”)
if name ==‘keshav’:
print(“You print the right name”)
If condition is true 1st Action will be executed otherwise 2nd Action.
Q => Passed or fail percentage wise ?
p = int(input(‘Enter Your Percentage’))
if p>33 and p<100:
While using this statement we make a program with multiple condition.
Q =>Make a Voting age criteria program ?
age = int(input(“Enter Your Age:”))
print(‘You are under age\nYou are not eligible’)
elif age<=18 and age<90:
print(‘You are eligible for voting’)
print(‘You are over age’)
Iterative Statements :
These are the statements helps to execute a group of statements multiple times.
There are 2 types of statements in python are :
- for loop
- while loop
Let’s learn where these statements are to be used:
It is used to iterate over elements of a sequence or when you have a piece of code which you want to repeat “n” number of time.
Q => If you want to print the no. from 1 to 100 ?
Soln : Let, a is a variable in which we stored a datatype(Range) , with the help of which we can simply print numbers.
a = range(1,100,1)
for i in a:
Q => How to print any word/Name alphabet line by line ?
a = ‘Keshav’
for z in a:
Q => How to print the index value with list items ?
Soln : for this make a list of items after that read the code below:
a = [‘a’, ‘b’, ‘c’, ‘d’]
i = 0
for x in a:
i = i+1
Nested for loop :
for loop inside another for loop is known as Nested for loop.
Q => How to
a = [‘Abhishek’,‘Nikhil’,‘Tarun’,‘Manyu’,‘Shivam’]
b = [‘How are You’]
for i in a:
for j in b:
The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.
Q => How to print a no. from 11 to 20 by using while loop ?
x = 11
while x <=20:
x = x+1
Q => How to display the sum of first n numbers ?
n = 18
sum = 0
i = 1
sum = sum+i
i = i+1
print(‘The sum of first’,n,‘numbers is :’,sum)
Q => How to write a program in which user enter some name until entering the right name ?
print(‘Thanks ksv for the confirmation’)
Infinite loops :
In this loop the given statement iterate infinite times.
Q => Print a statement in which the no. and the word print infinite times ?
i = 0
i = i+1
Nested while loop :
While loop inside another while loop is known as Nested while loop.
Q => Send hii 5 times when Keshav is online ?
a = 1
print(‘Keshav is online’)
b = 1
Transfer Statements :
These are the statements used to Continue or break the Execution of the loop while giving some condition.
These are of 2 types :
break statement is used inside loops to break loop execution based on some condition.
Q =>Break value when it exeed 50 inside a loop ?
for i in range(60):
if i ==51:
It can be used to skip current iteration and continue next iteration.
Q => Print the pass and fail students by verifying their marks ?
marks = [23,45,64,34,30,33,67,89,31,34,98]
for result in marks:
if result >=33 and result<=100: