Control Statements
A control statement is a statement that determines the control flow of a set of instructions.
The three fundamental methods of control flow in a programming language are
I. Sequential Control Statements
II. Selection Control Statements
III. Iterative Control Statements
Sequential Control Statements:
The code of Python program is executed sequentially from the first line of the program to its
last line. That is, the second statement is executed after the first, the third statement is
executed after the second, so on and so forth.
Selection Control Statements:
Some cases, execute only a set of statements called as selection control statements. This
method is known as selection control flow.
Iterative Control Statements:
Execute a set of statements repeatedly called as Iterative control statements. This method is
known as Iterative control flow.
SELECTION/CONDITIONAL/DECISION CONTROL STATEMENTS
The selection control statements usually jumps from one part of the code to another
depending on whether a particular condition is satisfied or not. Python supports different
types of conditional statements which are as follows:
If statement
If –else statement
Nested if statement
If-elif-else statement
If Statement:
if statement is a selection control statement based on the value of a given Boolean
expression.
The if structure may contain 1 statement or n statements enclosed within the if block.
First, the test expression(Boolean expression) is evaluated.
If the test expression is True, the statement of if block are executed, otherwise these
statements will be skipped and jump to next statement which is outside of the if block.
Syntax of if statement
if test_expression:
Statement1
Rajiv Gandhi University of Knowledge Technologies 1
Statement2
……. Statement n
Statement x
Flow-chart
Example:
Program to increment a number if it is positive.
x=10
if (x>0):
x=x+1
print (“X Value: “, x)
Output:
X value: 11
Example:
Write a program to determine whether a person is eligible to vote.
age=int(input(“Enter the age: “))
if (age>18):
print (“You are eligible to vote”)
Output:
Enter the age: 35
You are eligible to vote
if –else Statement:
The if…else statement is called alternative execution, in which there are two
possibilities and the condition determines which one gets executed
An if-else statement, first the test expression (Boolean expression) is evaluated.
Rajiv Gandhi University of Knowledge Technologies 2
If the expression is True, if block is executed and else block is skipped. Otherwise, if
the expression is False, else block is executed and if block is ignored.
Syntax of if-else statement
if (condition):
Statement block 1
else:
Statement block 2
Statement x
Example:
Write a program to determine whether a person is eligible to vote or not. If he is not eligible,
display how many years are left to be eligible.
age=int(input(“Enter the age:”))
if (age>=18):
print (“You are eligible to vote”)
else:
yrs=18-age
print (“You have to wait for another ”,yrs,“ years to cast your vote”)
Output:
Enter the age: 10
You have to wait for another 8 years to cast your vote.
Example:
Write a program to find whether the given number is even or odd.
num=int(input("Enter any number :"))
if (num%2==0):
print (num, "is even")
else:
print (num, "is odd")
Output:
Enter any number: 125
125 is odd
Rajiv Gandhi University of Knowledge Technologies 3
Nested if statements:
• We can write an entire if...else statement in another if...else statement called nesting
and the statement is called nested if.
• In such a case, the inner if statement is the statement part of the outer one. Nested if
statements are used to check if more than one condition is satisfied
Example:
num = int(input("Enter a number: "))
if (num >0):
if num % 3==0:
print("divisible by 3")
else:
print("Not divisible by 3")
else:
print("The number is 0 or negative")
Output:
Enter a number: 21
divisible by 3
Enter a number: 22
Not divisible by 3
Enter a number: -3
The number is 0 or negative
Example:
Write a python program to find the given number is positive or negative number, if positive
number then compare with 100, if number is greater than 100, output display as “high”
otherwise display as “low”.
num = int(input("Enter a number: "))
if (num >0):
if num >100:
print("High")
else:
print("Low")
else:
print("Negative number")
Output:
Enter a number: 101
High
Rajiv Gandhi University of Knowledge Technologies 4
if-elif-else Statement:
The elif statement allows to check multiple expressions and executes block of
statements wherever the condition returns as True. From there, it exit from the entire
if-elif-else block.
If any of the expression not returns as true, then it executes the else block code.
Example:
Program to test whether a number entered by the user is negative, positive or equal to Zero?
num=int(input("Enter any number"))
if (num==0):
print ("The value”, num, “is equal to zero")
elif (num>0):
print ("The number “, num, “is positive")
else:
print (num, "The number “, num, “ is negative")
Output:
Enter any number: 4
The number 4 is positive
Rajiv Gandhi University of Knowledge Technologies 5
Assignment II
1. Write a program to find whether a given year is a leap year or not.(if -else)
2. Write a program to determine whether the character entered is vowel or not. (if -else)
3. Write a program to find larger of three numbers.(if-elif-else)
4. Write a python program to input month number and print number of days in that
month. .(if-elif-else)
5. Write a python program to perform calculator operations.(if-elif-else)
6. Write a python program to input basic salary of an employee and calculate its Gross
salary according to following:
Basic Salary <= 10000 : HRA = 20%, DA = 80%
Basic Salary <= 20000 : HRA = 25%, DA = 90%
Basic Salary > 20000 : HRA = 30%, DA = 95%.(if-elif-else)
7. Write a program to take input from the user and then check whether it is a number or
a character. If it is a character, determine whether it is in uppercase or
lowercase.(Nested if)
8. A Company decides to give bonus to all its employees on Diwali. A 5% bonus on salary
is given to the male workers and 10% bonus on salary to the female workers. Write a
Python program to enter the salary of the employee and gender of the employee. If
the salary of the employee is less than Rs/- 10,000 then the employee gets an extra 2%
bonus on salary. Calculate the bonus that has to be given to the employee and display
the salary that the employee will get. (Nested if)
9. Write a python program to count total number of notes in given amount.(Multiple if
statements)
Rajiv Gandhi University of Knowledge Technologies 6