Question:

Which of the following is most suitable for a menu-driven program ?

Show Hint

Use a \text{do-while} loop whenever you need an action to be performed at least one time. Think "do this, then check the condition". This is a classic pattern for user menus.
  • For
  • While
  • Do-While
  • All of these
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is C

Solution and Explanation

Step 1: Understand the requirement of a menu-driven program. A menu-driven program must first display the menu options to the user and then wait for an input. This action of displaying the menu must happen at least once.
Step 2: Analyze the suitability of the loop structures.

(A) \text{for} loop: Best for iterating a known number of times. Not ideal for a menu that runs until the user chooses to exit.
(B) \text{while} loop: This is a pre-test loop. It checks the condition \textit{before} executing the loop body. While it can be used, it's slightly less natural than a do-while loop for this task.
(C) \text{do-while} loop: This is a post-test loop. It executes the loop body \text{at least once} and then checks the condition. This is perfect for a menu, as the menu is displayed first, and then the loop continues based on the user's choice.
Because the menu must be displayed at least once, the \text{do-while} loop is the most suitable structure.
Was this answer helpful?
0
0