Conditional
Statement
Duration: 4 period (50 minute)
1. if - else statement
2. Nested if statement
3. elif statement
4. Multiple condition in if and elif statement
5. pass statement
Duration: 4 period (50 minute)
The conditional statements in programming
languages decide the direction of the flow of
program execution. It is used for decision-
making.
The decision is made based on the condition
provided to the if and elif
statement(conditional statements). However,
if none of the conditions gets fulfilled, else
part will be executed.
Introduction
if-else statement
An if statement is used for conditional execution. It is
used to decide whether a certain statement or block of
statements will be executed or not i.e. if a certain
condition is true then a block of statement is executed
otherwise else statement is executed if present.
Syntax:
if condition:
Statement
else:
Statement
***else statement will not take
any condition. Else statement will
be executed only if the if
statement condition fails.
You should know comparison
operators to write condition
Click here for more detail on comparison
operators.
if - else Example
age = int(input("Enter your age: "))
if age <= 12:
print("You are a kid!")
else:
print("You are an adult!")
Output:
# Output sample 1
Enter your age: 10
You are a kid!
=================
# Output sample 2
Enter your age: 15
You are an adult!
Write a python program to check whether you
are eligible to vote or not?
Your program should get the age of the voter
from the user and if their age is 18 and above
let them vote otherwise deny them from
voting.
elif statement
The keyword ‘elif’ is short for ‘else if’, and is useful
to avoid excessive indentation and used when
there are multiple conditions to be checked. If the
condition is not true, it will check for elif condition.
Otherwise, it will directly execute the else
statement.
Syntax:
if condition 1:
Statement
elif condition 2:
Statement
elif condition 3:
Statement
elif condition can continue………
Example
age = int(input("Enter your age:"))
if age <= 12:
print("You are a kid!")
elif age == 18:
print("you are 18 and in adulthood.")
elif age >= 19:
print("You are above 18.")
Output:
# Possible output
Enter your age:10
You are a kid!
=================
Enter your age:18
you are 18 and in
adulthood.
=================
Enter your age:20
You are above 18.
Write a python program that will check for the
following conditions:
● If the light is green – Car is allowed to go
● If the light is yellow – Car has to wait
● If the light is red – Car has to stop
● Other signal - unrecognized signal. Example black, blue,
etc...
1. Traffic light:
Write a program to check students' grades.
Your program should fulfill the following
conditions:
1. Grade A - Outstanding
2. Grade B - Excellent
3. Grade C - Very Good
4. Grade D - Good
5. Grade E – Satisfactory
6. Others - Unrecognized
A program should also ask to enter the
student's name, class, and section. The
expected output is attached below.
2. Students Grade
Expected output:
Multiple condition in if and elif statement
The if and elif statement can execute
multiple conditions at the same time.
Multiple conditions can be used using
logical operators.
You should know logical operators to
write multiple condition
Click here for more detail logical
operators
Example
age = int(input("Enter your age:"))
if age < 12 and age > 0:
print("You are a kid!")
elif age > 12 and age < 25:
print("you are a youth!")
elif age > 25 and age < 54:
print("you are a man!")
else:
print("You are an old man!")
Output:
# Output
Enter your age:8
You are a kid!
=================
Enter your age:23
you are a youth!
=================
Enter your age:45
you are a man!
=================
Enter your age:70
You are an old man!
Modify the earlier program students’ grades in such a way that they
should take in five subject marks. Find the total mark and their
percentage. Your program should check for the following conditions:
• If the percentage falls below 45, they are considered fail.
• If the percentage is between 45 and 60, grade them as pass.
• If the percentage is between 60 and 75, grade them as good.
• If the percentage is between 75 and 85, grade them as very good.
• If the percentage is between 85 and 100, grade them excellent.
• If the percentage is below zero or above 100, it’s an error.
The expected output is attached below.
Student result with grade Expected output:
Nested if statement
Nested if statements mean an if statement inside
another if statement.
Syntax:
if condition 1:
Statements
if condition 1.1:
Statement
else:
Statement
else:
Statement
Example
age = int(input("Enter your age:"))
if age <= 12:
print("You are a kid!")
if age < 5: #nested if condition
print("and also below 5!")
else:
print("but not below 5.")
else:
print("you are above 12!")
Output:
# Three possible output
Enter your age:3
You are a kid!
and also below 5!
=================
Enter your age:7
You are a kid!
but not below 5.
=================
Enter your age:20
you are above 12!
Write a program to trace your subject mark. Your program should fulfill the following
conditions:
• If the subject mark is below 0 and above 100, print “error: mark should be between 0
and 100 only”
• Students will fail in the subject if their mark is below 50.
• Students will pass in the subject if they score 50 and above.
• If subject mark is between 50 and 60, grade student as good.
• If subject mark is between 60 and 80, grade student as very good.
• If subject mark is between 80 and 100, grade student as outstanding.
Make sure to print their mark in every statement to prove that the condition is fulfilled.
Moreover, name, class, and section should be also displayed along with the marks and their
grade.
Pass statement
The pass statement does nothing. It can be
used when a statement is required
syntactically correct but the program requires
no action.
The pass can be also used as a placeholder
for a function or conditional body when you
are working on a new code, allowing you to
keep thinking at a more abstract level. The
pass is silently ignored.
Example
age = 13
if age <= 12:
pass
Output:
In the above example, nothing will be printed and it
won’t generate any error.
Conditional-Statement.pdf

Conditional-Statement.pdf

  • 1.
  • 2.
    1. if -else statement 2. Nested if statement 3. elif statement 4. Multiple condition in if and elif statement 5. pass statement Duration: 4 period (50 minute)
  • 3.
    The conditional statementsin programming languages decide the direction of the flow of program execution. It is used for decision- making. The decision is made based on the condition provided to the if and elif statement(conditional statements). However, if none of the conditions gets fulfilled, else part will be executed. Introduction
  • 4.
    if-else statement An ifstatement is used for conditional execution. It is used to decide whether a certain statement or block of statements will be executed or not i.e. if a certain condition is true then a block of statement is executed otherwise else statement is executed if present. Syntax: if condition: Statement else: Statement ***else statement will not take any condition. Else statement will be executed only if the if statement condition fails.
  • 5.
    You should knowcomparison operators to write condition Click here for more detail on comparison operators.
  • 6.
    if - elseExample age = int(input("Enter your age: ")) if age <= 12: print("You are a kid!") else: print("You are an adult!") Output: # Output sample 1 Enter your age: 10 You are a kid! ================= # Output sample 2 Enter your age: 15 You are an adult!
  • 7.
    Write a pythonprogram to check whether you are eligible to vote or not? Your program should get the age of the voter from the user and if their age is 18 and above let them vote otherwise deny them from voting.
  • 8.
    elif statement The keyword‘elif’ is short for ‘else if’, and is useful to avoid excessive indentation and used when there are multiple conditions to be checked. If the condition is not true, it will check for elif condition. Otherwise, it will directly execute the else statement. Syntax: if condition 1: Statement elif condition 2: Statement elif condition 3: Statement elif condition can continue………
  • 9.
    Example age = int(input("Enteryour age:")) if age <= 12: print("You are a kid!") elif age == 18: print("you are 18 and in adulthood.") elif age >= 19: print("You are above 18.") Output: # Possible output Enter your age:10 You are a kid! ================= Enter your age:18 you are 18 and in adulthood. ================= Enter your age:20 You are above 18.
  • 10.
    Write a pythonprogram that will check for the following conditions: ● If the light is green – Car is allowed to go ● If the light is yellow – Car has to wait ● If the light is red – Car has to stop ● Other signal - unrecognized signal. Example black, blue, etc... 1. Traffic light:
  • 11.
    Write a programto check students' grades. Your program should fulfill the following conditions: 1. Grade A - Outstanding 2. Grade B - Excellent 3. Grade C - Very Good 4. Grade D - Good 5. Grade E – Satisfactory 6. Others - Unrecognized A program should also ask to enter the student's name, class, and section. The expected output is attached below. 2. Students Grade Expected output:
  • 12.
    Multiple condition inif and elif statement The if and elif statement can execute multiple conditions at the same time. Multiple conditions can be used using logical operators.
  • 13.
    You should knowlogical operators to write multiple condition Click here for more detail logical operators
  • 14.
    Example age = int(input("Enteryour age:")) if age < 12 and age > 0: print("You are a kid!") elif age > 12 and age < 25: print("you are a youth!") elif age > 25 and age < 54: print("you are a man!") else: print("You are an old man!") Output: # Output Enter your age:8 You are a kid! ================= Enter your age:23 you are a youth! ================= Enter your age:45 you are a man! ================= Enter your age:70 You are an old man!
  • 15.
    Modify the earlierprogram students’ grades in such a way that they should take in five subject marks. Find the total mark and their percentage. Your program should check for the following conditions: • If the percentage falls below 45, they are considered fail. • If the percentage is between 45 and 60, grade them as pass. • If the percentage is between 60 and 75, grade them as good. • If the percentage is between 75 and 85, grade them as very good. • If the percentage is between 85 and 100, grade them excellent. • If the percentage is below zero or above 100, it’s an error. The expected output is attached below. Student result with grade Expected output:
  • 16.
    Nested if statement Nestedif statements mean an if statement inside another if statement. Syntax: if condition 1: Statements if condition 1.1: Statement else: Statement else: Statement
  • 17.
    Example age = int(input("Enteryour age:")) if age <= 12: print("You are a kid!") if age < 5: #nested if condition print("and also below 5!") else: print("but not below 5.") else: print("you are above 12!") Output: # Three possible output Enter your age:3 You are a kid! and also below 5! ================= Enter your age:7 You are a kid! but not below 5. ================= Enter your age:20 you are above 12!
  • 18.
    Write a programto trace your subject mark. Your program should fulfill the following conditions: • If the subject mark is below 0 and above 100, print “error: mark should be between 0 and 100 only” • Students will fail in the subject if their mark is below 50. • Students will pass in the subject if they score 50 and above. • If subject mark is between 50 and 60, grade student as good. • If subject mark is between 60 and 80, grade student as very good. • If subject mark is between 80 and 100, grade student as outstanding. Make sure to print their mark in every statement to prove that the condition is fulfilled. Moreover, name, class, and section should be also displayed along with the marks and their grade.
  • 19.
    Pass statement The passstatement does nothing. It can be used when a statement is required syntactically correct but the program requires no action. The pass can be also used as a placeholder for a function or conditional body when you are working on a new code, allowing you to keep thinking at a more abstract level. The pass is silently ignored.
  • 20.
    Example age = 13 ifage <= 12: pass Output: In the above example, nothing will be printed and it won’t generate any error.