After completing this chapter, you will be able to:
- Understand the
ifstatement. - Explain why the
ifstatement is used. - Write simple decision-making programs.
- Understand the flow of an
ifstatement. - Develop basic logical thinking for problem-solving.
In programming, we often need to execute a block of code only when a specific condition is True.
For example:
- If the student has passed, display "Pass".
- If the password is correct, allow login.
- If the age is 18 or above, allow voting.
To perform such decision-making operations, Python provides the if statement.
The if statement is a decision-making statement that executes a block of code only when the specified condition evaluates to True.
If the condition is False, the block of code is skipped.
The if statement is used whenever a program needs to make a decision.
Examples include:
- Login Validation
- ATM PIN Verification
- Student Result Processing
- Online Payment Verification
- Age Verification
- Employee Eligibility Check
if condition:
statement1
statement2Note: The statements inside the
ifblock must be indented.
Condition
│
┌──────┴──────┐
True False
│ │
Execute Block Skip Block
│ │
└──────┬───────┘
│
Next Statement
- Python evaluates the condition.
- If the condition is True, the statements inside the
ifblock are executed. - If the condition is False, the
ifblock is skipped. - Control moves to the next statement after the
ifblock.
Write a Python program to check whether a person is eligible to vote.
# Age of the person
age = 20
# Check voting eligibility
if age >= 18:
print("Eligible for Voting")Eligible for Voting
START
Create variable age
Assign value 20
Check age >= 18
If True
Display "Eligible for Voting"
STOP
age = 20Creates a variable named age and stores the value 20.
if age >= 18:Checks whether the age is greater than or equal to 18.
The condition evaluates to True.
print("Eligible for Voting")Since the condition is True, the message is displayed.
Write a Python program to check whether a number is positive.
# Store a number
number = 15
# Check whether the number is positive
if number > 0:
print("Positive Number")Positive Number
START
Create variable number
Assign value 15
Check number > 0
If True
Display "Positive Number"
STOP
Write a Python program to check whether a student has passed.
# Store marks
marks = 65
# Check pass condition
if marks >= 35:
print("Student Passed")Student Passed
The if statement is commonly used in:
- ATM Machines
- Login Systems
- Banking Applications
- Hospital Management Systems
- Online Shopping Websites
- Online Examination Systems
- Simple decision making.
- Improves program logic.
- Easy to understand.
- Widely used in real-world applications.
- Forms the foundation for advanced control statements.
❌ Incorrect
if age >= 18
print("Eligible")✅ Correct
if age >= 18:
print("Eligible")❌ Incorrect
if age >= 18:
print("Eligible")✅ Correct
if age >= 18:
print("Eligible")The if statement is a decision-making statement used to execute a block of code only when a specified condition is True.
The if block is executed only when the condition evaluates to True.
The statements inside the if block are skipped, and execution continues with the next statement after the block.
Indentation defines the block of code that belongs to the if statement. Without proper indentation, Python raises an IndentationError.
Yes. An if statement can be used independently without an else statement.
ifis a decision-making statement.- It executes only when the condition is
True. - A colon (
:) is mandatory after the condition. - Indentation is mandatory for the block of code.
- If the condition is
False, the block is skipped.
In this chapter, you learned:
- What the
ifstatement is. - Why it is used.
- Syntax of the
ifstatement. - Flow of execution.
- Practical programs.
- Flow diagram.
- Interview questions.
- Common mistakes.
- Quick revision.