Introduction:
Let’s imagine you’re working on a project, in that you have a requirement that some block of code has to be executed only when a condition is met, in this case, we will use if-else conditional statements
Definition:
if-else statements can be defined as conditional statements because they are executed when a condition is met.
In this tutorial we will learn:
- if statement
- if-else
- if – elif- else
- Nested if-else
if statement:
Executes code in the “if” block if the condition specified is true, otherwise, control of the program goes to the next line without executing code inside the “if” block.
Syntax:
1)if(condition): 2) ..code.. 3) .........
Note: Make sure to give correct indentation for all the lines inside the if block.
Example :
Check whether a given number is strictly greater than 25
Code:
Python Code
num = 29
if(num > 25):
print("Yes", num, "is greater than 25!")
Output:
Yes 29 is greater than 25!
Explanation: The condition in the “if” block is true ( 29 > 25), so the code in the “if” block is executed. If n is < 25, say 13 then code inside the “if” block will not be executed.
if-else Statement:
Syntax:
1)if(condition): 2) ..code.. 3) ......... 4)else: 5) ..code.. 6) ........
Execute code in the “if” block if the condition specified is true, otherwise execute the “else” part and then move on to the next lines.
Example:
Given the age of a person check whether he/she is eligible for voting, print “Yes” if eligible else print “No”
Note: A person is eligible for voting if that person’s age is greater than 18
Code:
Python Code
age = 39
if(age > 18):
print("Eligible for voting")
else:
print("Not eligible for voting")
Output:
Eligible for voting
if – elif – else statement:
Syntax:
1)if(condition): 2) ..code.. 3) ......... 4)elif(condition): 5) ...code.. 6) ......... 7)else: 8) ..code.. 9) ........ 10)......
“if” condition in line 1 is true ⇒ then code inside the “if” block will be executed and after execution, program control will go to line 10
“if” condition in line 1 is false ⇒ then program control will go to the “elif” part (line 4) and if the “elif” part is true then it will execute, otherwise program control will go to the “else” part.
“else” will execute only when none of its above statements turns out to be true.
Code:
Python Code
if(10 > 20): #false, so this will not get executed
print("if part executed")
elif(10 < 20): #true, so this will get executed
print("elif executed")
else: #it will not get executed because above statement got executed
print("else part executed")
Output: elif executed
Nested if-else :
When we want to check for multiple conditions at a time and execute a specific code block, then we can use an if-else block inside an if block or nested if-else.
Example:
Problem statement: Given a number, we need to check if that number is divisible by 9. If it is divisible by 9, then we need to check if it’s even or odd and print “Divisible by 9 and even” or “Divisible by 9 and odd”. If it’s not divisible by 9, we print “Not divisible by 9”.
Here first we check if the number is divisible by 9. If it is divisible, then we check if it is even or odd.
Code:
Python Code
n = 45
if(n % 9 == 0):
if(n % 2 == 0):
print("Divisible by 9 and Even")
else:
print("Divisible by 9 and Odd")
else:
print("Not divisible by 9")
Output:
Divisible by 9 and Odd
Special thanks to Sai bargav Nellepalli for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article