CBSE Class 11 Computer Science: Chapter 11 - Conditional and Looping Constructs NCERT Solutions

NCERT Solutions PDF Class 11 PDF

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

BoardCBSE
ClassClass 11
SubjectComputer Science
Session2026
LanguageEnglish
TypeNCERT Solutions
Chapter11. 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.

Loading document …
Page of
Loading page …

Questions and Solutions

Question 1

Why do we use the 'break' statement in programming?
Solution: The 'break' statement is used within loops (like 'for' or 'while') to terminate the loop's execution prematurely. When the 'break' statement is encountered, the program immediately exits the current loop and continues with the next statement after the loop block. This is useful when a certain condition is met and further iterations of the loop are unnecessary.

Question 2

What output will be printed with the following Python code?

x = True

y = False

z = False

if x or y and z:

print("yes")

else:

print("no")

Solution: The code will print "yes". Here's the evaluation:
  1. The expression is evaluated based on operator precedence: 'and' is evaluated before 'or'.
  2. First, 'y and z' is evaluated: False and False results in False.
  3. Next, 'x or (y and z)' is evaluated: True or False results in True.
  4. Since the condition is True, the code inside the 'if' block is executed, printing "yes".

Question 3

What output will be printed with the following Python code?

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)

Solution: The code will print 3. Let's trace the execution:
  1. First condition: if not x or y: not x is not True which is False. False or y is False or False which is False. This condition is false.
  2. Second condition: elif not x or not y and z: not x is False. not y is not False which is True. not y and z is True and False which is False. not x or (not y and z) is False or False which is False. This condition is false.
  3. Third condition: elif not x or y or not y and x: not x is False. y is False. not y is True. not y and x is True and True which is True. The expression becomes False or False or True. Evaluating from left to right: False or False is False. Then, False or True is True. This condition is true. The code prints 3.

Question 4

What gets printed with the given code?

f = None

for i in range(5):

with open("data.txt", "w") as f:

if i > 2:

break

print f.closed

Solution: The code will print True. Here's why:
  1. The loop starts with i = 0.
  2. The with open(...) statement opens the file data.txt in write mode. The as f assigns the file object to the variable f.
  3. The condition if i > 2 is checked. For i = 0, 1, 2, this condition is false.
  4. The loop continues for i = 0, 1, 2. In each of these iterations, the with statement ensures the file is properly closed when the block is exited (either normally or due to an exception).
  5. When i = 3, the condition if i > 2 becomes true.
  6. The break statement is executed, which immediately terminates the loop.
  7. Crucially, the with statement guarantees that the file is closed automatically when the block is exited, even if the exit is due to a break.
  8. After the loop terminates, the statement print f.closed is executed. Since the file object f was managed by the with statement and the loop was exited, the file is guaranteed to be closed. Therefore, f.closed evaluates to True.

Question 5

Which numbers are printed by the following Python code?

for i in range(2):

print i

for i in range(4,6):

print i

Solution: The numbers printed will be 0, 1, 4, 5.
  1. 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.
  2. 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.
  3. The output is the sequence of numbers printed by each loop: 0, 1, 4, 5.

Question 6

What value is printed for the variable 'sum' in the following code?

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

Solution: The value printed for sum will be 13.
  1. 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, sum becomes 0 + 1 = 1.
  2. 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, sum remains 1.
  3. 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, sum becomes 1 + 4 = 5.
  4. 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, sum becomes 5 + 8 = 13.
  5. Finally, print sum outputs 13.

Question 7

Write the syntax of an 'if statement' in the Python programming language.
Solution: The basic syntax for an 'if' statement in Python is as follows:

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

Write the syntax of an 'if.....else' statement in the Python programming language.
Solution: The syntax for an 'if...else' statement in Python is:

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?

Q2. In Python, which operator has the highest precedence among 'not', 'and', 'or'?

Q3. What will be printed by the following code snippet? x = False y = True if x or y: print('A') else: print('B')

Q4. Which function from the 're' module checks for a match 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?

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.