Conditional flow in Python with if, elif, and else.

A program executes in python line by line in a sequence.  What if we want to execute a block of code when a condition is satisfied? In conditional execution, a block of code will execute only if a condition is met. If not met another block(optional).

If, Elif and else are the statements for conditional flow in Python. Each conditional statement has an expression next to it. If the expression returns success, start execution just after the condition statement.

 

If conditional statement flow in python
If conditional statement flow in python
if expression evaluatioin :
	#Code Under If condition if Expression
	#Returns True
#Code That will always execute (OutSide of If block);
Example Conditional
expression
Meaning
X == Y True if X is equal to Y.
X > YTrue if the X is Greater
than Y;
X != YTrue if X is not equal to Y.
X < Y True if X is less than Y.
X <= YTrue if X is less than or equal toY.
X > Y True if X is more than Y.
X >= Y True if X is more than or equal to Y.

How an if (elif or else) statement works?

  • The expression next to the statement is evaluated.
  • If it’s True (non zero), the block under if is executed else False (zero), then control reached to the end of bock.
  • The block is decided by the indentation. The first line starts with indentation and ends with no indentation.
if expression evaluation :
	#First Statement
	#Second Statement
	#.....
	#Nth Statement
#Statement Not In Block Body
if expression evaluation : #outer if
	if expression evaluation:  #inner if
		#First Statement (inner if)
		#Second Statement(inner if)
		#.....
		#Nth Statement(inner if)
	#Statements with in Fist if ((outer if)
#Statements Not in If condition
# Example Program for If statement 
X = 10;
Y = 12;
if X < Y:
	print("X Is smaller than Y");
print(" If Condition Checked (Always Printed)");

if isinstance(X, int):
	print("The type of X is int");
print(" Type of X Checked (Always Printed)");
Output-
X Is smaller than Y
If Condition Checked (Always Printed)
The type of X is int
Type of X Checked (Always Printed)
  • Initialized variable X to 10 and Y to 12.
  • Check if X is smaller than Y. The expression return True.
  • The body of if have only one statement [print(“X Is smaller than Y”)].
  • Statement outside of if body is executed.
  • Again Checked if X is or type int. Returns true.
  • Next Statements are executed.

More flow control with if..else – Two-way decision

There could be a else with if. So that if an expression is True, body of if executes if not body of else executes. It is used to make a binary decision. Following is syntax of If..else.

if expression evaluation:
	# First statement --
	# Second statement --
	# .....
	# Nth statement --
else:
	# First statement --
	# Second statement --
	# .....
	# Nth statement --

#Statement always executes
  • If the expression is true the if block executes otherwise the else block executes.
  • Indentation decides the statements for if and else.
#example program to check if an user 
#entered number is 
#positive or negative 

print("Enter a Number");

num = int(input());

if num > 0:
	print("Number is positive");
else:
	print("Number is negative");
Output -> 
Enter a Number
3
Number is positive
Output->
Enter a Number
-5
Number is negative

Multi-way decision with if..elif…else:

With the if..elif..else you can choose a single flow from possible multiple flows. Unlike if..else, there could be multiple elif, when performing decisions.

if conditional expression: 
	# First statement --
	# Second statement --
	# .....
	# Nth statement --
elif conditional expression:
	# First statement --
	# Second statement --
	# .....
	# Nth statement --
elif conditional expression: 
	# First statement --
	# Second statement --
	# .....
	# Nth statement --
else:
	# First statement --
	# Second statement --
	# .....
	# Nth statement --
#example program to check if an user 
#entered number is 
#zero , negative, more than 100 or more than 200 

print("Enter a Number");

num = int(input());

if num == 0:
	print("Number is zero");
elif num > 100 and num < 200:
	print("Number is more than 100");
elif num > 200:
	print("Number is more than 200");
else:
	print("Number is negative");
Output->
Enter a Number
103
Number is more than 100
Output->
Enter a Number
202
Number is more than 200