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.

Answer: The output is 2. The loop encounters 2 and 4, making count equal to 2, and then 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

All of Flow of Control

Practise more Computer Science

Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.

Start practising free

Free to start · No credit card needed