The if-else statement is a decision-making construct that allows you to execute one block of code if a condition is True and another block of code if the condition is False. It is one of the most commonly used control structures in programming for managing conditional logic. The else block is optional, but it is often used to handle the alternative scenario when the condition is not met.

Table of Contents
- What is an
if-elseStatement? - Syntax of the
if-elseStatement - Example of a Simple
if-elseStatement - Nested
if-elseStatements - Using Logical Operators with
if-else if-elsewith Multiple Conditions- Common Pitfalls with
if-else - Mini-Project: Positive, Negative, or Zero Number Checker
- Interview Questions and Answers (Google, Amazon, TCS, Infosys, Zoho)
1. What is an if-else Statement?
The if-else statement allows your program to make decisions based on conditions. The program will check the condition after the if statement:
- If the condition is
True, the block of code underifis executed. - If the condition is
False, the code under theelseblock is executed.
This makes it easy to control the flow of a program based on the outcome of a condition.
2. Syntax of the if-else Statement
The general structure of the if-else statement is:
if condition:
# Code to execute if condition is True
else:
# Code to execute if condition is False
- condition: A Boolean expression that is evaluated.
- Code block under
if: Executes when the condition isTrue. - Code block under
else: Executes when the condition isFalse.
Example:
age = 20
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
In this case, if the age is greater than or equal to 18, it will print “You are an adult.” Otherwise, it will print “You are a minor.”
3. Example of a Simple if-else Statement
Let’s look at a simple if-else example to demonstrate its basic usage:
number = 15
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
Output:
The number is odd.
Here, the program checks if number % 2 == 0. Since 15 is not divisible by 2, the condition is False, and the program prints “The number is odd.”
4. Nested if-else Statements
Just like with regular if statements, you can nest if-else statements inside one another to create more complex decision trees.
Example:
age = 16
has_parent_permission = False
if age >= 18:
print("You are an adult.")
else:
if has_parent_permission:
print("You are a teenager with parent permission.")
else:
print("You are a teenager without parent permission.")
Here, if the person is under 18, the program checks whether they have parent permission.
Output:
You are a teenager without parent permission.
5. Using Logical Operators with if-else
You can combine multiple conditions using logical operators like and, or, and not within an if-else statement to make more complex decisions.
a) Using and Operator
The and operator checks if both conditions are True.
Example:
age = 25
has_voter_id = True
if age >= 18 and has_voter_id:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
In this example, the user must be both 18 or older and have a voter ID to be eligible to vote.
b) Using or Operator
The or operator checks if at least one condition is True.
Example:
has_ticket = False
has_invitation = True
if has_ticket or has_invitation:
print("You can attend the party.")
else:
print("You cannot attend the party.")
In this case, the person can attend the party if they have either a ticket or an invitation.
c) Using not Operator
The not operator negates the condition.
Example:
is_sunny = False
if not is_sunny:
print("It might rain today.")
else:
print("It's a sunny day.")
In this example, the not operator checks if the weather is not sunny and suggests rain.
6. if-else with Multiple Conditions
When dealing with multiple conditions, you can use elif (else if) to check for additional conditions beyond the first if.
Syntax:
if condition1:
# Code to execute if condition1 is True
elif condition2:
# Code to execute if condition1 is False and condition2 is True
else:
# Code to execute if both condition1 and condition2 are False
Example:
temperature = 75
if temperature > 90:
print("It's a hot day.")
elif temperature > 70:
print("It's a warm day.")
else:
print("It's a cool day.")
In this example, the program checks whether the temperature is above 90, between 70 and 90, or below 70 and prints the appropriate message.
Output:
cssCopyEditIt's a warm day.
7. Common Pitfalls with if-else
- Indentation Errors: Python relies on indentation to define the scope of the
if,else, and other code blocks. Always use consistent indentation (typically 4 spaces). - Missing
elseBlock: If you miss theelseblock, make sure your code properly handles all cases. It’s often useful to include anelseto catch any unexpected cases. - Unnecessary
else: If theifstatement covers all the necessary conditions, you might not need theelsepart. Avoid adding it when not needed.
8. Mini-Project: Positive, Negative, or Zero Number Checker
Let’s create a Python program to check if a number entered by the user is positive, negative, or zero.
Objective:
The program will ask the user to input a number and will print whether it is positive, negative, or zero.
def check_number():
number = int(input("Enter a number: "))
if number > 0:
print(f"{number} is positive.")
elif number < 0:
print(f"{number} is negative.")
else:
print("The number is zero.")
check_number()
Sample Output:
Enter a number: -5
-5 is negative.
This mini-project demonstrates the use of an if-else structure to handle different conditions.
Interview Questions and Answers
Q: What is the output of the following code?
x = 5
if x == 5:
print("Equal")
else:
print("Not Equal")
A: The output will be "Equal".
Amazon
Q: Can you use an if-else statement without the else part?
A: Yes, it’s perfectly fine to use an if statement without an else block. If you don’t need an alternative action, the else part is optional.
TCS
Q: How can we check if a number is positive or negative?
A: You can use an if-else statement:
if number > 0:
print("Positive")
else:
print("Negative")
Infosys
Q: Can we compare multiple conditions in a single if-else statement?
A: Yes, you can combine multiple conditions using logical operators such as and, or, and not inside an if-else statement.
Zoho
Q: How would you handle multiple conditions for a discount eligibility program?
A: Using if-elif-else:
if total_purchase > 1000:
print("Eligible for 10% discount.")
elif total_purchase > 500:
print("Eligible for 5% discount.")
else:
print("No discount eligible.")
Conclusion
The if-else statement is an essential tool in Python that lets you make decisions in your programs. It helps you to control the flow of execution and respond dynamically to different conditions. Whether using simple if-else or more complex decision-making with multiple conditions, mastering this structure is key to writing effective Python code.
If-Else
Question
Your answer:
Correct answer:
Your Answers