Python if-else Statement Class 11 CBSE Computer Science: Syntax, Flowchart, Examples and Programs
Class 11 · Computer Science
Python if-else Statement (CBSE Class 11 Computer Science)
In many situations, a program must choose between two alternative actions. For example, if a student's marks are 33 or above, the result is Pass; otherwise, the result is Fail. Python provides the if-else statement to handle such situations.
Learning Objectives
- Understand the
if-elsestatement. - Learn its syntax and working.
- Understand flowcharts and algorithms.
- Trace program execution.
- Write decision-making programs using
if-else.
Quick Syntax Reference
if condition:
statement(s)
else:
statement(s)
What is the if-else Statement?
The if-else statement checks a condition. If the condition is True, the statements inside the if block are executed. Otherwise, the statements inside the else block are executed.
if-else statement is a decision-making statement that executes one block when the condition is true and another block when the condition is false.
Flowchart
Start
│
▼
Check Condition
│
┌─────┴─────┐
│ │
True False
│ │
▼ ▼
Execute IF Execute ELSE
Block Block
│ │
└─────┬─────┘
▼
End
Algorithm
- Start.
- Read the condition.
- If the condition is True, execute the
ifblock. - Otherwise, execute the
elseblock. - Stop.
Pseudocode
IF condition THEN
Execute IF block
ELSE
Execute ELSE block
END IF
Example 1: Pass or Fail
marks = 45
if marks >= 33:
print("Pass")
else:
print("Fail")
Output
Pass
Dry Run Table
| Step | Statement | Result | Output |
|---|---|---|---|
| 1 | marks = 45 | 45 | - |
| 2 | marks >= 33 | True | - |
| 3 | Execute IF block | - | Pass |
Trace Table
| Condition | IF Block | ELSE Block |
|---|---|---|
| True | Executed | Skipped |
| False | Skipped | Executed |
Python IDLE Editor Snapshot
>>> marks = 45
>>> if marks >= 33:
... print("Pass")
... else:
... print("Fail")
Pass
Example 2: Even or Odd
num = 17
if num % 2 == 0:
print("Even Number")
else:
print("Odd Number")
Output
Odd Number
Flow of Execution
Condition True
│
▼
Execute IF Block
│
▼
Continue Program
Condition False
│
▼
Execute ELSE Block
│
▼
Continue Program
Think Like a Programmer
Predict the output.
age = 15
if age >= 18:
print("Adult")
else:
print("Minor")
print("Completed")
Click to View Answer
Minor
Completed
Output Prediction Challenge
x = 12
if x % 3 == 0:
print("Divisible")
else:
print("Not Divisible")
Click to View Answer
Divisible
Debugging Exercise
Find and correct the errors.
marks = 50
if marks >= 33
print("Pass")
else
print("Fail")
Errors:
- Missing colon (
:) afterifandelse. - Incorrect indentation.
Competency-Based Question
A school charges a library fine only if a book is returned after the due date. Write a Python program that displays "Fine Applicable" if the number of delayed days is greater than 0; otherwise, display "No Fine".
Real-Life Examples
- If attendance ≥ 75%, allow the student to appear for the examination; otherwise, do not allow.
- If the password is correct, log in; otherwise, display an error message.
- If the balance is sufficient, complete the withdrawal; otherwise, show "Insufficient Balance".
Common Beginner Mistakes
- Forgetting the colon after
iforelse. - Incorrect indentation.
- Using
=instead of==in conditions. - Writing multiple
elseblocks for oneif.
Visual Memory Box
- Condition True → IF Block Executes
- Condition False → ELSE Block Executes
- Exactly one block executes.
elsehas no condition.
Memory Trick
IF = True Block
ELSE = Everything Else
Exam Tips
- Always use a colon after
ifandelse. - Maintain proper indentation.
- Practice both True and False cases while tracing programs.
- Remember that only one block executes.
CBSE Important Questions
2 Marks
- What is the purpose of the
if-elsestatement? - Differentiate between
ifandif-else.
3 Marks
- Write a program to check whether a number is even or odd using
if-else. - Draw the flowchart of an
if-elsestatement.
5 Marks
- Write a Python program to check whether a student has passed or failed and explain the execution using a dry run and trace table.
Practice Yourself
- Check whether a person is eligible for voting.
- Check whether a number is positive or negative.
- Check whether a student has passed or failed.
- Write a program to compare two numbers and display the greater one.
- Trace the execution of the above programs.
Frequently Asked Questions (FAQs)
1. Can both the if and else blocks execute?
No. Only one block executes based on the condition.
2. Is the else block mandatory?
No. It is optional and used only when an alternative action is required.
3. Does the else statement have a condition?
No. The else block executes automatically when the if condition is false.
4. Why is indentation important?
Indentation defines which statements belong to the if or else block.
5. Can an if-else statement be nested?
Yes. An if-else statement can be placed inside another conditional statement.
Summary
- The
if-elsestatement is used to choose between two alternative actions. - If the condition is true, the
ifblock executes; otherwise, theelseblock executes. - Only one block executes during program execution.
- Proper indentation and colon usage are mandatory.
- The
if-elsestatement is widely used in decision-making programs.