0
0
Rubyprogramming~20 mins

While loop in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
While Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple while loop
What is the output of this Ruby code?
Ruby
i = 1
while i <= 3
  puts i
  i += 1
end
A
0
1
2
3
B
1
2
C
1
2
3
4
D
1
2
3
Attempts:
2 left
💡 Hint
Remember the loop runs while the condition is true.
🧠 Conceptual
intermediate
2:00remaining
Understanding loop exit condition
What will be the value of variable count after this code runs?
Ruby
count = 0
while count < 5
  count += 2
end
A6
B5
C4
DInfinite loop
Attempts:
2 left
💡 Hint
Think about when the loop stops and the last increment.
🔧 Debug
advanced
2:00remaining
Identify the error in this while loop
What error will this Ruby code produce when run?
Ruby
i = 0
while i < 3
  puts i
  i = i - 1
end
AInfinite loop
BSyntaxError
CTypeError
DNo output
Attempts:
2 left
💡 Hint
Look at how the variable changes inside the loop.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly uses a while loop?
Select the option that is syntactically correct Ruby code using a while loop to print numbers 1 to 3.
A
i = 1
while i &lt;= 3:
  puts i
  i += 1
B
i = 1
while (i &lt;= 3) {
  puts i
  i += 1
}
C
i = 1
while i &lt;= 3
  puts i
  i += 1
end
D
i = 1
while i &lt;= 3
  puts i
  i++
end
Attempts:
2 left
💡 Hint
Remember Ruby syntax for loops does not use ++ or braces.
🚀 Application
expert
2:00remaining
Calculate sum using while loop
What is the value of sum after running this Ruby code?
Ruby
sum = 0
num = 1
while num <= 5
  sum += num
  num += 1
end
A5
B15
C10
D20
Attempts:
2 left
💡 Hint
Sum numbers from 1 to 5.