Bird
0
0

You want to print different messages based on a variable status that can be 'open', 'closed', or 'pending'. Which Ruby control flow style is best and why?

hard📝 Application Q15 of 15
Ruby - Control Flow
You want to print different messages based on a variable status that can be 'open', 'closed', or 'pending'. Which Ruby control flow style is best and why?
AUse multiple <code>if</code> statements because they are simpler
BUse a <code>for</code> loop to iterate over status values
CUse a <code>while</code> loop to repeat checking status
DUse a <code>case</code> statement because it clearly handles multiple fixed choices
Step-by-Step Solution
Solution:
  1. Step 1: Identify the nature of the problem

    Status has multiple fixed values and needs different messages for each.
  2. Step 2: Choose the control flow style

    A case statement is best for multiple fixed options because it is clear and concise.
  3. Final Answer:

    Use a case statement because it clearly handles multiple fixed choices -> Option D
  4. Quick Check:

    Multiple fixed choices = case statement [OK]
Quick Trick: Multiple fixed options? Use case statement [OK]
Common Mistakes:
MISTAKES
  • Using multiple ifs making code longer and less clear
  • Using loops which are for repetition, not choice
  • Confusing for loops with case statements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes