Question Detail

COS 102 If staments Objective Question

Question

When is the `else` clause executed in an `if-else` structure?
Options
A Always, regardless of the `if` condition.
B Only if the `if` condition evaluates to True.
C Only if the `if` condition evaluates to False.
Correct Answer
D If an error occurs in the `if` block.
Correct Answer

Option C is the correct answer.

Detailed Explanation

Page 8, 'The "Else" Clause (The Backup)' slide, explains: 'What if the condition is False? We can provide a backup plan using else.' The else block provides an alternative execution path when the primary if condition is not met (i.e., is False).

Hint

Think of the `else` clause as the 'backup plan' or what happens when the main condition isn't true.

Question Info