Bird
0
0

Which of the following is the correct syntax for a while loop in Ruby?

easy📝 Syntax Q12 of 15
Ruby - Loops and Iteration
Which of the following is the correct syntax for a while loop in Ruby?
Awhile condition # code loop
Bwhile (condition) { # code }
Cwhile condition: # code
Dwhile condition do # code end
Step-by-Step Solution
Solution:
  1. Step 1: Recall Ruby while loop syntax

    Ruby uses while condition do ... end or while condition ... end without braces.
  2. Step 2: Check each option

    while condition do # code end matches Ruby syntax. Options B and C use other languages' syntax. while condition # code loop is invalid.
  3. Final Answer:

    while condition do\n # code\nend -> Option D
  4. Quick Check:

    Ruby while loop uses do/end [OK]
Quick Trick: Ruby uses do/end, not braces or colons for while [OK]
Common Mistakes:
MISTAKES
  • Using braces {} like in JavaScript
  • Using colon : like in Python
  • Ending with 'loop' keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes