CBSE Class 11 Computer Science: Chapter 11 - Conditional and Looping Constructs NCERT Solutions
This set of NCERT Solutions for CBSE Class 11 Computer Science, Chapter 11, focuses on Conditional and Looping Constructs. It provides clear explanations and answers for various types of questions, including very short answer types. The solutions cover the fundamental concepts of conditional statements (if, if-else) and looping constructs, along with the use of the 'break' statement for loop control. It also delves into logical operators and their evaluation within conditional statements, as well as file handling within loops. These solutions are designed to help students understand the practical application of these programming concepts, reinforcing their learning and aiding in effective exam preparation by offering step-by-step guidance and accurate answers.
Quick info
| Board | CBSE |
|---|---|
| Class | Class 11 |
| Subject | Computer Science |
| Session | 2026 |
| Language | English |
| Type | NCERT Solutions |
| Chapter | 11. Conditional and Looping Constructs |
Chapter summary
Chapter 11 of the CBSE Class 11 Computer Science syllabus introduces students to Conditional and Looping Constructs. This NCERT Solutions set covers essential programming control flow structures. It includes explanations and solutions for 'break' statements, logical evaluations in 'if' conditions, and the syntax of 'if' and 'if-else' statements. The exercises also touch upon loop execution, file operations within loops, and the use of regular expressions for pattern matching in strings, providing a solid foundation for problem-solving in programming.
Learning outcomes
- Understand the purpose and usage of the 'break' statement in loops.
- Evaluate boolean expressions involving logical operators (and, or, not).
- Determine the output of Python code involving nested conditional statements.
- Write the correct syntax for 'if' and 'if-else' statements in Python.
- Analyze the execution flow of loops and their interaction with file operations.
- Apply regular expression functions like 'match' and 'search' to find patterns in strings.
Topics covered
Paper topics
- Conditional Statements
- Looping Constructs
- Break Statement
- Logical Operators (and, or, not)
- Boolean Expression Evaluation
- Python Syntax for if statements
- Python Syntax for if-else statements
- File Handling in Loops
- Regular Expressions (re module)
- re.match() function
- re.search() function
- Code Execution Flow
Important topics
- Conditional and Looping Constructs
- Logical Operator Precedence and Evaluation
- Break Statement Usage
- Regular Expression Functions (match vs search)
- Syntax of if and if-else Statements
PDF preview
Read page by page below. PDF is streamed from the official NCERT website — no download button on this page.
Questions and Solutions
Question 1
Question 2
x = True
y = False
z = False
if x or y and z:
print("yes")
else:
print("no")
- The expression is evaluated based on operator precedence: 'and' is evaluated before 'or'.
- First, 'y and z' is evaluated:
False and Falseresults inFalse. - Next, 'x or (y and z)' is evaluated:
True or Falseresults inTrue. - Since the condition is
True, the code inside the 'if' block is executed, printing "yes".
Question 3
x = True
y = False
z = False
if not x or y:
print(1)
elif not x or not y and z:
print(2)
elif not x or y or not y and x:
print(3)
else:
print(4)
- First condition:
if not x or y:not xisnot Truewhich isFalse.False or yisFalse or Falsewhich isFalse. This condition is false. - Second condition:
elif not x or not y and z:not xisFalse.not yisnot Falsewhich isTrue.not y and zisTrue and Falsewhich isFalse.not x or (not y and z)isFalse or Falsewhich isFalse. This condition is false. - Third condition:
elif not x or y or not y and x:not xisFalse.yisFalse.not yisTrue.not y and xisTrue and Truewhich isTrue. The expression becomesFalse or False or True. Evaluating from left to right:False or FalseisFalse. Then,False or TrueisTrue. This condition is true. The code prints 3.
Question 4
f = None
for i in range(5):
with open("data.txt", "w") as f:
if i > 2:
break
print f.closed
True. Here's why:
- The loop starts with
i = 0. - The
with open(...)statement opens the filedata.txtin write mode. Theas fassigns the file object to the variablef. - The condition
if i > 2is checked. Fori = 0, 1, 2, this condition is false. - The loop continues for
i = 0, 1, 2. In each of these iterations, thewithstatement ensures the file is properly closed when the block is exited (either normally or due to an exception). - When
i = 3, the conditionif i > 2becomes true. - The
breakstatement is executed, which immediately terminates the loop. - Crucially, the
withstatement guarantees that the file is closed automatically when the block is exited, even if the exit is due to abreak. - After the loop terminates, the statement
print f.closedis executed. Since the file objectfwas managed by thewithstatement and the loop was exited, the file is guaranteed to be closed. Therefore,f.closedevaluates toTrue.
Question 5
for i in range(2):
print i
for i in range(4,6):
print i
- The first loop uses
range(2), which generates numbers starting from 0 up to (but not including) 2. So, it prints 0 and then 1. - The second loop uses
range(4, 6), which generates numbers starting from 4 up to (but not including) 6. So, it prints 4 and then 5. - The output is the sequence of numbers printed by each loop: 0, 1, 4, 5.
Question 6
import re
sum = 0
pattern = 'back'
if re.match(pattern, 'backup.txt'):
sum += 1
if re.match(pattern, 'text.back'):
sum += 2
if re.search(pattern, 'backup.txt'):
sum += 4
if re.search(pattern, 'text.back'):
sum += 8
print sum
sum will be 13.
- First condition:
if re.match(pattern, 'backup.txt'):re.match('back', 'backup.txt')checks if the string 'backup.txt' starts with 'back'. It does. So,sumbecomes0 + 1 = 1. - Second condition:
if re.match(pattern, 'text.back'):re.match('back', 'text.back')checks if the string 'text.back' starts with 'back'. It does not. So,sumremains 1. - Third condition:
if re.search(pattern, 'backup.txt'):re.search('back', 'backup.txt')checks if the substring 'back' exists anywhere in 'backup.txt'. It does (at the beginning). So,sumbecomes1 + 4 = 5. - Fourth condition:
if re.search(pattern, 'text.back'):re.search('back', 'text.back')checks if the substring 'back' exists anywhere in 'text.back'. It does (at the end). So,sumbecomes5 + 8 = 13. - Finally,
print sumoutputs 13.
Question 7
if condition:
# Statement(s) to be executed if the condition is True
Here, condition is an expression that evaluates to either True or False. If the condition is True, the indented block of code following the colon is executed.
Question 8
if condition:
# Statement(s) to be executed if the condition is True
else:
# Statement(s) to be executed if the condition is False
This structure allows you to specify a block of code to run when the condition is True, and an alternative block of code to run when the condition is False.
Common mistakes
- Incorrectly evaluating complex boolean expressions with multiple logical operators.
- Misunderstanding the short-circuiting behavior of 'and' and 'or' operators.
- Errors in predicting the output of loops, especially when combined with conditional breaks.
- Confusing the behavior of `re.match()` (matches from the beginning) with `re.search()` (matches anywhere).
Revision tips
- Practice writing and tracing code for various 'if-elif-else' scenarios.
- Manually execute loop examples to understand how 'break' affects the flow.
- Review the order of operations for logical operators ('not', 'and', 'or').
- Pay close attention to the conditions in 'if' statements and how they evaluate to True or False.
Practice MCQs
Q1. What is the primary purpose of the 'break' statement in a loop?
Explanation: The 'break' statement is used to terminate the loop prematurely, exiting the loop's execution entirely.
Q2. In Python, which operator has the highest precedence among 'not', 'and', 'or'?
Explanation: The 'not' operator has the highest precedence, followed by 'and', and then 'or'.
Q3. What will be printed by the following code snippet? x = False y = True if x or y: print('A') else: print('B')
Explanation: Since 'x' is False and 'y' is True, the condition 'x or y' evaluates to True, and 'A' is printed.
Q4. Which function from the 're' module checks for a match only at the beginning of the string?
Explanation: The `re.match()` function attempts to match the pattern only at the beginning of the string.
Q5. If a loop is iterating and a 'break' statement is encountered, what happens to the variable 'f.closed' in Question 4?
Explanation: The `break` statement exits the loop when `i > 2`. The `print(f.closed)` statement is inside the loop, so it is never reached after the break.
Frequently asked questions
What is the main function of conditional and looping constructs in programming?
Conditional and looping constructs allow programs to make decisions and repeat actions, enabling dynamic and efficient execution of tasks based on specific criteria.
How does the 'break' statement affect a loop's execution?
The 'break' statement immediately terminates the innermost loop it is contained within, transferring control to the statement following the loop.
What is the difference between 're.match()' and 're.search()' in Python?
`re.match()` checks for a match only at the beginning of the string, while `re.search()` scans through the entire string looking for the first location where the pattern produces a match.
Why is understanding logical operator precedence important?
Correctly understanding operator precedence (like 'not' before 'and', and 'and' before 'or') is crucial for accurately evaluating complex boolean conditions in 'if' statements and ensuring the program behaves as intended.
Are these NCERT solutions suitable for exam preparation?
Yes, these solutions provide clear explanations and step-by-step answers for key concepts in conditional and looping constructs, making them excellent for revising and reinforcing understanding for exams.
What does the `if expression: statement(s)` syntax represent in Python?
This is the basic syntax for an 'if' statement in Python. If the 'expression' evaluates to True, the 'statement(s)' indented below it are executed.
Content reviewed by the NCERT Help team. Editorial Team and update policy
NCERT Solutions PDF PDF on NCERT Help. URL unchanged for search indexing.