Professional Documents
Culture Documents
Conditional Statements
▪ Sometimes the program needs to do different things for different conditions. In that
case the following statements are used:
▪ if statement
▪ if…else
▪ if…elif…else
if Statement
a=int(input(“Enter a number:”))
if a<100:
print(“The number is less than 100”)
if …else statement
▪ Syntax:
If:
[Statements to be executed if condition 1 is true]
else:
[Statements to be executed if condition 1 is false]
Example:
M=int(input(“Enter the marks:”))
if M >= 35:
print(“Congrats! You have passed the test!”)
else:
print(“Sorry! You have not passed the test””)
Explanation:
If the user enters 40, the message Congrats! You have passed the test! will appear on the screen.
If the user enters 30, the message Sorry! You have not passed the test will appear on the screen.
Program to find the Eligibility for voting
# Voting Eligibility
age=int(input("Enter your age")) //Simple Statement
pass //Empty Statement
if age>17
print(’Eligible to vote’)
else Compound
Statement
print(’Not Eligible to
vote’)
What happen when ”if condition” does not meet
Example:
a = 33
b = 200
if b > a:
print("b is greater than
a")
else:
print(“a is greater than
b")
WAP to check whether the given year is
leap year or not.
▪ By using "elif" condition, you are telling the program to print out the third condition or
possibility when the other condition goes wrong or incorrect.
▪ Demonstrate:
x=int(input("Enter num1:"))
y=int(input("Enter num2:"))
if (x<y):
print("x is less than y")
elif (x == y):
print("x is same as y")
else:
print("x is greater than y")