0
0
Rubyprogramming~20 mins

Why Ruby has multiple control flow styles - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Control Flow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Ruby if-else vs ternary operator
What is the output of this Ruby code snippet?
Ruby
x = 5
result = if x > 3
  "big"
else
  "small"
end
puts result
Asmall
BSyntaxError
Cbig
Dnil
Attempts:
2 left
💡 Hint
Check the condition x > 3 and what the if-else returns.
Predict Output
intermediate
2:00remaining
Output of Ruby case statement with when clauses
What will this Ruby code print?
Ruby
grade = 'B'
case grade
when 'A'
  puts 'Excellent'
when 'B'
  puts 'Good'
else
  puts 'Needs Improvement'
end
AGood
BNeeds Improvement
CExcellent
Dnil
Attempts:
2 left
💡 Hint
Look at the value of grade and which when clause matches.
Predict Output
advanced
2:00remaining
Output of Ruby while vs until loops
What is the output of this Ruby code?
Ruby
i = 0
while i < 3
  print i
  i += 1
end

j = 3
until j == 0
  print j
  j -= 1
end
A0123210
BSyntaxError
C0123
D012321
Attempts:
2 left
💡 Hint
Check how while and until loops run and what they print.
Predict Output
advanced
2:00remaining
Output of Ruby modifier if and unless
What will this Ruby code output?
Ruby
x = 10
puts 'High' if x > 5
puts 'Low' unless x > 5
A
High
Low
BHigh
CLow
Dnil
Attempts:
2 left
💡 Hint
Understand how modifier if and unless work.
🧠 Conceptual
expert
3:00remaining
Reason for multiple control flow styles in Ruby
Why does Ruby have multiple control flow styles like if-else, case, modifier if/unless, and loops like while and until?
ATo allow programmers to write code in different styles for readability and expressiveness depending on the situation.
BBecause Ruby was designed to be confusing and complex for beginners.
CTo force programmers to use the most complicated syntax possible.
DBecause Ruby does not support functions and needs many control flows instead.
Attempts:
2 left
💡 Hint
Think about why a language would offer many ways to do the same thing.