Conditional Statements:
In conditional branching, a program decides whether next statements should be
executed or not based upon the current expression evaluated.
Consider with following flowchart:
Conditional statements in Python are implemented using following ways:
1. If Statement
2. If –Else Statement
3. If-Elif-Else Statement
1- If Statement:
If Condition tests a particular condition, if condition becomes true, an action or
set of actions is executed, otherwise action or set of actions are ignored.
If statements are written as follows:
if(expression):
statement (s)
For Eg:
if(per>=50):
print(“Student Passed”)
Note That :
1. Here expression can be and can’t be enclosed in ( open/close parenthesis
)
2. Here is no need of { } braces for grouping statements. Statements are just
grouped using Indent.
3. Statements written outside the indent is treated as out of conditional area.
Ques Exercise : WAP to input the price and apply 10% discount if amount
exceeds 1000 otherwise no discount should made.
2- If –else Statement:
Using if –else we can also reply for false condition using if –else.
The syntactic form of if-else condition is as follows:
If(expression):
Block1
true part statements
else:
Block2
False part statements
Note : If condition becomes true then Block,1 true part statements will execute
otherwise Block2 False part statements will execute.
Consider with following flowchart as per above Syntax:
Eg :
If (x>y):
print(“ x is Greater”)
else:
print(“ y is Greater”)
Ques Exercise 1- : WAP to input a number and check whether it is Even or Odd.
Ques Exercise 2- : WAP to input a number and check whether it is negative or
positive.
Ques Exercise 3- : WAP to input a Year and check whether it is Leap year or Not
Leap Year.
Part-2
3- If –Elif-Else:
The if...else statement is used to execute a block of code among two
alternatives.
However, if we need to make a choice between more than two alternatives, then
we use the if...elif...else statement.
The syntax of the if...elif...else statement is:
If(expression1):
Block1
true part statements for expression 1
elif(expression2):
Block2
true part statements for expression 2
elif(expression3):
Block3
true part statements for expression 3
………..
…………
else:
Block 4
true part statements for expression else_part
Note That: 1 - In multiple if elif else statement only one statement will
execute.
Eg:1 :
Consider with Following Example:
number = 5
if number > 0:
print("Positive number")
elif number == 0:
print('Zero')
else:
print('Negative number')
print('This statement is always executed')
Ques : Exercise :
1. Write a program to print the greatest among 3 Numbers.
2. WAP to Input 5 Subject Marks and print the Division “First” , “Second” or
“Third” or “Fail.”
3. Calculate the Electricity Bill.
Part-3
Python Nested if statements
We can also use an if statement inside of an if statement. This is known as
a nested if statement.
The syntax of nested if statement is:
# outer if statement
if condition1:
# statement(s)
# inner if statement
if condition2:
# statement(s)
Example:
1 WAP to input a number and check whether number is 0 or positive or
negative.
num = int(input(“Enter the Number:”))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
WAP to input 3 Numbers and Print the Greatest among all numbers.
x=int(input("Enter the Number:"))
y=int(input("Enter the Number:"))
z=int(input("Enter the Number:"))
if(x>y):
if(x>z):
print(x)
else:
print(z)
elif(y>z):
print(y)
else:
print(z)
WAP to print