Flow of Control · Class 11 · CBSE · Science Computer Science
Consider the following Python code: count = 0 for n in range(2, 7): if n % 2 == 0: count += 1 else: continue if count == 2: break else: print("comple…
Practice question from Flow of Control (Class 11 · CBSE · Science Computer Science), with the correct answer and a step-by-step solution.
hard · Subjective · Q-A455C7
Consider the following Python code:
count = 0
for n in range(2, 7):
if n % 2 == 0:
count += 1
else:
continue
if count == 2:
break
else:
print("completed")
print(count)
State the output and explain why the else block of the loop is or is not executed.
break terminates the loop; therefore, the else block is not executed because a loop else runs only when the loop finishes normally without break.Solution
For n = 2 and n = 4, count is incremented. When count becomes 2, break stops the loop, so the loop's else block is skipped and the final value 2 is printed.
More from Flow of Control
- What is printed by the following Python code? x = 7 if x > 10: print("A") elif x > 5: print("B") else: print("C")
- What is the output of the following Python code? for i in range(1, 6): if i % 2 == 0: continue print(i, end=" ")
- What is printed by the following Python code? x = 3 while x < 10: x += 2 if x == 7: break print(x)
- What is the output of the following Python code? total = 0 for i in range(1, 4): for j in range(i): if j == 1: break total += 1 print(total)
Practise more Computer Science
Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.
Start practising freeFree to start · No credit card needed