Skip to content

Control Statements

These are used to change the normal flow of program execution.
 
Three types of control Statements:
1. Selections/Decisions/Branching/Conditional
2. Loops/Iteration/Repetition
3. Jumps
 
Selection constructs supporting by Python:
1. simple if
2. Multiple if
3. if…else
4. Nested if
5. elif
 
Simple if
This is used when we want to execute some block of code when a specified condition is returned True.
 
>>>Execution Flow diagram
 
Exercise:
1. write python code to create a variable by initialising with desired value and display a message when the number is natural number
 
Algorithm:
1. Start
2. declare n
3. n:=12
4. if n>0 then
print “n is natural number”
    end if
5. stop
🐍
filename.py
#Python Code
n=12
if(n>0):
    print(n,"is natural number")
print("The End")

Practice Questions:
1. Write python program to check whether a desired value is multiple of 5 and display “Multiple of 5”, if yes.
Hint: n%5==0

 

2. Write python code to accept age from the user and display “You are eligible for voting” when his age is greater than 18.
Hint: age>18

 

3. Write python program to accept two numbers from the user and display which is big
Hint:
a>b
b>a

 

4. Write python program to accept a number and check and display whether it is even or odd
Hint:
x%2==0
x%2!=0

 

5. Write Python code to accept a number from the user and display whether it is positive, negative or equal to zero.
Hint:
n>0
n<0
n==0

 

6. Write Python program to accept any number from 2,4,6,8 from the user and display their Words Format

Example Output:
Enter any number from 2,4,6,8: 4
FOUR

Multiple if
It is used when we want to check more than one conditions and execute corresponding code based on their evaluation.
 
Example:
write python program to accept any number from 2,4,6 or 8 and display corresponding message.
 
1. Start
2. declare n
3. print “Enter any number from 2,4,6 or 8”
4. read n
5. if n==2 then
print “TWO”
    end if
6. if n==4 then
print “FOUR”
    end if
7. if n==6 then
print “SIX”
    end if
8. if n==8 then
print “EIGHT”
    end if
9. Stop
🐍
filename.py
#Example of Multiple if
num=int(input("Enter any number from 2,4,6 or 8:"))

if(num==2):
    print("TWO")

if(num==4):
    print("FOUR")
    
if(num==6):
    print("SIX")
    
if(num==8):
    print("EIGHT")
    
if(num!=2 and num!=4 and num!=6 and num!=8):
    print("Invalid Input")

print("Rest of the program")
if…else
it is used when we want to check a condition and perform a block of statements to be executed when the condition is returned true and another block to be executed when the condition is returned false.
 
else block is the optional part of simple if.
there can be if without else, but else cannot be without if.
 
Example:
Write Python program to determine big of two user entered numbers
 
algorithm:
1. Start
2. declare a,b
3. print “Enter first number”
4. read a
5. print “Enter second number”
6. read b
7. if (a>b) then
print “a is big”
    else
print “b is big”
    end if
8. stop
🐍
filename.py
#Example of if...else
first=int(input("Enter your first number:"))
second=int(input("Enter your second number:"))

if(first > second):
    print(first,"is big")
else:
    print(second,"is big")

print("Rest of the program")

Practice Questions:
1. Write a program to accept a number from the user and determine whether it is even or odd.
Hint: x%2==0 then it is even

 

2. Write a program to accept two numbers and one arithmetic operator from the user and return result of the operation based on the operator entered

Example output:
Enter first number: 4
Enter second number: 5
Enter operator: +
Sum=9

 

3. Write python program to accept any letter from English Vowels and display corresponding message.

Sample output1:
Enter any English Vowel: a
Your Vowel is : a

Sample output2:
Enter any English Vowel: x
x is not Vowel

Nested if
One if statement under the control of another if. This is used when we want to specify some code to be executed when sequential true returning of two conditions.

 

>>>Execution flow diagram

🐍
filename.py
#Example of nested if
nat="India"
age=21

if nat=="India":
    if age>18:
        print("You are eligible for voting")

print("Rest of the program")

Outer if can also have other program statements along with inner if.

🐍
filename.py
#Example of nested if
a=int(input("Enter any number:"))

if(a>0):
    print(a,"is natural number")
    if(a%2==0):
        print(a,"is even number too")
    print("other statements under outer if")

print("Rest of the program")

Outer if can have else part

Inner if can have else part

🐍
filename.py
#Example of nested if
a=int(input("Enter any number:"))

if(a>0):
    print(a,"is natural number")
    if(a%2==0):
        print(a,"is even number too")
    else:
        print(a,"is odd number too")
    print("other statements under outer if")
else:
    print(a,"is not natural number")

print("Rest of the program")

else…if
inner if appear inside the false part (else block) of outer if.

 

>>>Execution Flow Diagram

🐍
filename.py
#Example
a=int(input("Enter any number:"))

if(a>0):
    print("Positive")
else:
    if(a<0):
        print("Negative")
    else:
        print("Equal to zero")
🐍
filename.py
#Example of else.....if
a=int(input("Enter any number from 2,4,6 or 8 only:"))

if(a==2):
    print("TWO")
else:
    if(a==4):
        print("FOUR")
    else:
        if(a==6):
            print("SIX")
        else:
            if(a==8):
                print("EIGHT")
            else:
                print("Invalid input")

print("The End")

The elif
It is used when we want check further condition when a specific condition is returned false.

🐍
filename.py
#Example of elif
a=int(input("Enter any number from 2,4,6 or 8 only:"))

if(a==2):
    print("TWO")
elif(a==4):
    print("FOUR")
elif(a==6):
    print("SIX")
elif(a==8):
    print("EIGHT")
else:
    print("Invalid input")

print("The End")

Practice:
1. Write python program to accept any three numbers from the user and display which is big.
Logic1: nested if and else…if combination
Logic2: ‘and’ operator with elif

 

2. Write a program to accept marks of a student in an examination and display his Grade based on the following criteria:

Marks                        Grade
>=0 and <35            Fail
>=35 and <55          C
>=55 and <75          B
>=75 and <95          A
>=95 and <=100     A+