Flow Control Statement of Python
Flow Control Statement of Python: Flow control describes the order in which statements will be executed at runtime.
There are 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 :
- if:
- if-else:
- if-elif-else:
if :
In this statement, we have only 1 condition. If the condition is true, the program will be executed; otherwise, it comes out from the program.
Examples :
Q: How do you write a program that executes if the right name is given?
Soln :
name = input(“Enter your Name :”)
if name ==‘keshav’:
print(“You print the right name”)
if-else :
If condition is true 1st Action will be executed otherwise 2nd Action.
Example :
Q: Passed or failed percentage-wise?
Soln :
p = int(input(‘Enter Your Percentage’))
if p>33 and p<100:
print(‘Pass’)
else:
print(‘Fail’)
if-elif-else :
While using this statement, we create a program with multiple conditions.
Example :
Q: Make a Voting age criteria program.
Sol :
age = int(input(“Enter Your Age:”))
if age<18:
print(‘You are under age\nYou are not eligible’)
elif age<=18 and age<90:
print(‘You are eligible for voting’)
else:
print(‘You are over age’)
Iterative Statements :
These are the statements that help execute a group of statements multiple times.
There are 2 types of statements in Python:
- for loop
- while loop
Let’s learn where these statements are to be used:
for loop:
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.
Examples :
Q: If you want to print the number. from 1 to 100,?
Sol : 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:
print(i)
Q => How to print any word/Name alphabet line by line?
Soln :
a = ‘Keshav’
for z in a:
print(z)
Q: How do I you print the index value with list items?
Sol: For this, make a list of items. After that, read the code below:
a = [‘a’, ‘b’, ‘c’, ‘d’]
i = 0
for x in a:
print(i,x)
i = i+1
Nested for loop :
for loop inside another for loop is known as Nested for loop.
Q: How to
Sol :
a = [‘Abhishek’,‘Nikhil’,‘Tarun’,‘Manyu’,‘Shivam’]
b = [‘How are You’]
for i in a:
print(i)
for j in b:
print(j)
while loop:
The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.
Examples :
Q: How do I print a number? from 11 to 20 by using a while loop?
Soln :
x = 11
while x <=20:
print(x)
x = x+1
Q: How do you display the sum of the first n numbers?
Soln :
n = 18
sum = 0
i = 1
while i<=n:
sum = sum+i
i = i+1
print(‘The sum of first’,n,‘numbers is :’,sum)
Q: How do you write a program in which the user enters some name until they enter the right name?
Soln :
name=”
while name!=‘ksv’:
name=input(“Enter 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 ?
Soln :
i = 0
while True:
i = i+1
print(i,‘ksv’)
Nested while loop :
while loop inside another while loop is known as Nested while loop.
Q: Send it 5 times when Keshav is online?
Soln :
a = 1
while a<3:
print(‘Keshav is online)
b = 1
while b<6:
print(‘hii’)
b+=1
a+=1
Transfer statements:
These are the statements used to continue or break the execution of the loop while giving some conditions.
There are 2 types:
- break
- continue
break :
A break statement is used inside loops to break loop execution based on some condition.
Examples :
Q What is the break value when it exceeds 50 inside a loop?
Soln :
for i in range(60):
if i ==51:
print(‘Completed’)
break
print(i,end=‘,’)
continue :
It can be used to skip the current iteration and continue the next iteration.
Example :
Q: Print the pass and fail students by verifying their marks.
Soln :
marks = [23,45,64,34,30,33,67,89,31,34,98]
for result in marks:
if result >=33 and result<=100:
print(‘Pass’)
continue
else:
print(‘Fail’,end=‘,’)
print(result)