Python if Statement Class 11 CBSE Computer Science: Syntax, Flowchart, Examples and Programs
Class 11 · Computer Science
Python if Statement (CBSE Class 11 Computer Science)
In real life, we make decisions every day based on certain conditions. For example, a student is allowed to appear for an examination only if the attendance is at least 75%. Similarly, Python uses the if statement to execute a block of code only when a specified condition is True.
Learning Objectives
- Understand decision making in Python.
- Learn the syntax of the
ifstatement. - Understand flowchart representation.
- Trace the execution of an
ifstatement. - Write simple decision-making programs.
Quick Syntax Reference
if condition:
statement(s)
What is the if Statement?
The if statement is a conditional statement that executes a block of code only if the given condition evaluates to True. If the condition is False, the block is skipped.
if statement is used to execute one or more statements only when a specified condition is true.
Flowchart
Start
│
▼
Check Condition
│
┌─────┴─────┐
│ │
True False
│ │
▼ │
Execute Block │
│ │
└─────┬─────┘
▼
End
Algorithm
- Start.
- Evaluate the condition.
- If the condition is True, execute the statements inside the
ifblock. - If the condition is False, skip the block.
- Stop.
Pseudocode
IF condition is True THEN
Execute statements
END IF
Example 1: Voting Eligibility
age = 20
if age >= 18:
print("Eligible to Vote")
Output
Eligible to Vote
Dry Run Table
| Step | Statement | Value | Output |
|---|---|---|---|
| 1 | age = 20 | 20 | - |
| 2 | age >= 18 | True | - |
| 3 | print() | - | Eligible to Vote |
Trace Table
| Condition | Result | Block Executed? |
|---|---|---|
| 20 ≥ 18 | True | Yes |
Example 2: Positive Number
num = -8
if num > 0:
print("Positive Number")
Output
No Output
Since the condition is False, the if block is skipped.
Program Flow
Condition True
│
▼
Execute Block
│
▼
Continue Program
Condition False
│
▼
Skip Block
│
▼
Continue Program
Think Like a Programmer
Predict the output.
marks = 45
if marks >= 33:
print("Pass")
print("Exam Completed")
Click to View Answer
Pass
Exam Completed
Output Prediction Challenge
x = 5
if x > 10:
print("Greater")
print("Done")
Click to View Answer
Done
Debugging Exercise
Find and correct the error.
age = 18
if age >= 18
print("Eligible")
Error: Missing colon (:) after the if condition.
Competency-Based Question
A school allows students to participate in an inter-school competition only if they have secured at least 60% attendance. Write a Python program using an if statement to display "Eligible" if attendance is 60 or above.
Real-Life Example
- If it is raining, carry an umbrella.
- If the traffic signal is green, move ahead.
- If your attendance is at least 75%, you can appear for the examination.
Common Beginner Mistakes
- Forgetting the colon (
:). - Incorrect indentation.
- Using
=instead of==when comparing values. - Expecting output even when the condition is False.
Visual Memory Box
- Condition True → Execute Block
- Condition False → Skip Block
- Colon (:) is mandatory.
- Indentation defines the block.
Memory Trick
IF = "If True, Execute"
Exam Tips
- Always end the
ifstatement with a colon. - Maintain proper indentation.
- Practice output-based questions.
- Remember that the block executes only when the condition is True.
CBSE Important Questions
2 Marks
- What is the purpose of the
ifstatement? - Why is indentation necessary in an
ifstatement?
3 Marks
- Explain the syntax and working of the
ifstatement with an example. - Draw the flowchart of an
ifstatement.
5 Marks
- Write a Python program using the
ifstatement to determine voting eligibility and explain its execution with a dry run.
Practice Yourself
- Write a program to check whether a number is positive.
- Write a program to check if a student has passed (marks ≥ 33).
- Write a program to check whether a person is eligible for voting.
- Trace the execution of an
ifstatement for different inputs. - Create your own real-life example using an
ifstatement.
Frequently Asked Questions (FAQs)
1. What happens if the condition is False?
The statements inside the if block are skipped.
2. Is indentation compulsory?
Yes. Python uses indentation to identify code blocks.
3. Can an if statement contain more than one statement?
Yes. All statements with the same indentation belong to the if block.
4. Can we use relational and logical operators in an if condition?
Yes. Conditions can use relational, logical, membership, and identity operators.
5. What is the syntax of the if statement?
if condition: followed by an indented block of statements.
Summary
- The
ifstatement is used for decision making in Python. - The block executes only when the condition evaluates to True.
- A colon (
:) and proper indentation are mandatory. - Flowcharts, dry runs, and trace tables help understand program execution.
- The
ifstatement forms the foundation for more advanced conditional statements such asif-elseandif-elif-else.