After completing this chapter, you will be able to:
- Understand the
if...elsestatement. - Explain why the
if...elsestatement is used. - Differentiate between
ifandif...else. - Write programs using
if...else. - Understand the execution flow of decision-making programs.
In many situations, a program must execute one block of code when a condition is True and another block when the condition is False.
For example:
- If a student passes, display "Pass"; otherwise, display "Fail".
- If the password is correct, allow login; otherwise, display "Invalid Password".
- If the account balance is sufficient, allow withdrawal; otherwise, display "Insufficient Balance".
To perform such operations, Python provides the if...else statement.
The if...else statement is a decision-making statement that executes one block of code when the condition is True and another block when the condition is False.
The if...else statement is used when there are two possible outcomes.
Examples:
- Pass / Fail
- Login Success / Login Failed
- Eligible / Not Eligible
- Even / Odd
- Positive / Negative
if condition:
statements_if_true
else:
statements_if_false Condition
/ \
True False
| |
if Block Executes else Block Executes
\ /
\ /
Next Statement
- Python evaluates the condition.
- If the condition is True, the
ifblock executes. - If the condition is False, the
elseblock executes. - Program execution continues with the next statement.
Write a Python program to check whether a student has passed or failed.
# Student marks
marks = 65
# Check result
if marks >= 35:
print("Student Passed")
else:
print("Student Failed")Student Passed
START
Create variable marks
Assign value 65
Check marks >= 35
If True
Display "Student Passed"
Else
Display "Student Failed"
STOP
marks = 65Stores the student's marks.
if marks >= 35:Checks whether the marks are greater than or equal to 35.
print("Student Passed")Executes when the condition is True.
else:Executes when the condition is False.
print("Student Failed")Displays the failure message.
Write a Python program to check whether a number is even or odd.
number = 18
if number % 2 == 0:
print("Even Number")
else:
print("Odd Number")Even Number
Write a Python program to check whether a person is eligible for voting.
age = 16
if age >= 18:
print("Eligible for Voting")
else:
print("Not Eligible for Voting")Not Eligible for Voting
| if | if...else |
|---|---|
| Executes only when the condition is True. | Executes one of two blocks. |
| May skip execution completely. | Always executes one block. |
| Used for single decision. | Used for two-way decision. |
The if...else statement is used in:
- Login Systems
- ATM Applications
- Banking Software
- Student Result Processing
- Online Payment Systems
- Hospital Management Systems
- Railway Reservation Systems
- Handles two possible outcomes.
- Improves decision-making.
- Makes programs more readable.
- Reduces unnecessary code.
❌ Incorrect
if age >= 18
print("Eligible")
else
print("Not Eligible")✅ Correct
if age >= 18:
print("Eligible")
else:
print("Not Eligible")❌ Incorrect
if age >= 18:
print("Eligible")
else:
print("Not Eligible")✅ Correct
if age >= 18:
print("Eligible")
else:
print("Not Eligible")An if...else statement is a decision-making statement that executes one block of code when the condition is True and another block when the condition is False.
The if statement executes code only when the condition is True, whereas if...else always executes one of the two blocks.
No. An else block must always be associated with an if statement.
Yes. Python uses indentation to define the code blocks.
Only one else block can be associated with an if statement.
if...elsehandles two-way decisions.- One block executes when the condition is
True. - The other block executes when the condition is
False. - A colon (
:) is mandatory after bothifandelse. - Proper indentation is required.
In this chapter, you learned:
- What the
if...elsestatement is. - Why it is used.
- Syntax and execution flow.
- Practical programs.
- Difference between
ifandif...else. - Real-world applications.
- Common mistakes.
- Interview questions.