Skip to study material
← Question index

QUESTION 14 · Selection

Compare an else-if ladder and switch.

5 marks

Answer in brief

Both forms choose among alternatives, but they test conditions in different ways.

Else-if ladder

Each branch can test a different condition. Conditions may use relational and logical operators, such as marks >= 90 or age >= 18 && has_id. Conditions are checked in order until one is true. Only that branch executes. A final else can handle the remaining cases.

Switch

One integer expression is compared with integer constant case values. This makes numbered menus or character-based operation choices clear. A default label handles a missing match. Case labels do not stop execution; a break or another transfer is normally used to prevent unwanted fall-through.

Examples

  1. Use an else-if ladder to classify marks into ranges such as 90–100 and 75–89.
  2. Use switch to process a menu choice such as 1 for Add, 2 for View and 3 for Exit.
  3. For two simple alternatives, a plain if-else is usually sufficient.

Choice and efficiency

Choose the form that expresses the actual condition clearly. An else-if ladder may perform up to k tests for k conditions. A switch is not guaranteed by C to be faster: the compiler chooses an implementation. For a small fixed decision, both may involve only constant work. Correct logic and readability matter more than assuming one keyword always improves speed.

Comparison table

↔ Scroll horizontally on a small screen
Else-if ladder and switch
FeatureElse-if ladderswitch
ConditionSeparate conditions, including rangesOne integer expression and constant case values
Branch controlFirst true branch onlyStarts at matching label; may fall through
Remaining casesFinal elsedefault label
Typical useGrades, eligibility rangesMenus, operator choices

Related exam questions

Related questions in the selection category from the supplied papers. The marks below are the printed allocations, including any combined subquestion.

  • March/April 2023 · Q3(a) · 5 marks

    Discuss decision-making statements with an example.

    Paper P02 · page 1
  • June 2022 · Q2(b) · 8 marks

    Illustrate forms of if statements with syntax and examples.

    Paper P03 · page 1

View this category in the exam question bank →