Question 1 of 13beginner💡 UnderstandShort Answer2 marks

What is the difference between else and elif construct of if statement?

Correct Answer

The else construct allows us to write two alternative paths in an if statement. When the condition is true, the indented block under if is executed; otherwise, the block under else is executed. It provides only one default alternative when the condition is false.

The elif construct is used when multiple conditions need to be checked. The number of elif statements depends on the number of conditions to be tested. If the first condition is false, the next condition is checked, and so on. When one of the conditions becomes true, the corresponding indented block executes, and the if statement terminates immediately.

Exercise: EXERCISE | Q: 1
For More Understanding

Explanation

The textbook explains that else provides two alternative paths based on a single condition, while elif allows checking multiple conditions sequentially. The key distinction is that else has no condition - it simply executes when the if condition fails, whereas each elif has its own condition that is evaluated in order.