Bird
Raised Fist0
Rubyprogramming~10 mins

While loop in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - While loop
Initialize variable
↓
Check condition
|Yes↓
Execute loop body
↓
Update variable
Back to Check↓
Exit loop
↩Back to Check
The while loop starts by checking a condition. If true, it runs the loop body, updates variables, then checks again. It stops when the condition is false.
Execution Sample
Ruby
i = 0
while i < 3
  puts i
  i += 1
end
This code prints numbers 0, 1, and 2 by looping while i is less than 3.
Execution Table
StepiCondition (i < 3)ActionOutput
10truePrint 0, i = i + 10
21truePrint 1, i = i + 11
32truePrint 2, i = i + 12
43falseExit loop
💡 i reaches 3, condition 3 < 3 is false, loop stops
Variable Tracker
VariableStartAfter 1After 2After 3Final
i01233
Key Moments - 2 Insights
Why does the loop stop when i equals 3?
Because the condition i < 3 becomes false at step 4, so the loop does not run again (see execution_table row 4).
What happens if we forget to update i inside the loop?
The condition stays true forever, causing an infinite loop. The variable i must change to eventually make the condition false.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of i at step 2?
A2
B0
C1
D3
💡 Hint
Check the 'i' column in execution_table row 2.
At which step does the condition become false and the loop stops?
AStep 4
BStep 3
CStep 1
DStep 2
💡 Hint
Look at the 'Condition' column in execution_table where it changes to false.
If we change the condition to i < 5, how many times will the loop run?
A3 times
B5 times
C4 times
D6 times
💡 Hint
Refer to variable_tracker and how i increments until condition is false.
Concept Snapshot
while condition do
  # code to repeat
  update variables
end

- Checks condition before each loop
- Runs body only if condition true
- Stops when condition false
- Update variables inside to avoid infinite loop
Full Transcript
This visual shows how a while loop works in Ruby. We start with i = 0. The loop checks if i is less than 3. If yes, it prints i and adds 1 to i. This repeats until i reaches 3, when the condition becomes false and the loop stops. The variable i changes from 0 to 3 step by step. If we forget to update i, the loop never ends. Changing the condition changes how many times the loop runs.

Practice

(1/5)
1. What does a while loop do in Ruby?
easy
A. Stops the program immediately
B. Runs code only once regardless of condition
C. Repeats code as long as a condition is true
D. Executes code after the loop ends

Solution

  1. Step 1: Understand the purpose of a while loop

    A while loop runs the code inside it repeatedly as long as the condition is true.
  2. Step 2: Compare options with the definition

    Only Repeats code as long as a condition is true correctly describes this behavior.
  3. Final Answer:

    Repeats code as long as a condition is true -> Option C
  4. Quick Check:

    while loop = repeats while true [OK]
Hint: Remember: while loops run only if condition is true [OK]
Common Mistakes:
  • Thinking while loop runs code once
  • Confusing while with if statement
  • Assuming it stops program immediately
2. Which of the following is the correct syntax for a while loop in Ruby?
easy
A. while condition # code loop
B. while (condition) { # code }
C. while condition: # code
D. while condition do # code end

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]
Hint: Ruby uses do/end, not braces or colons for while [OK]
Common Mistakes:
  • Using braces {} like in JavaScript
  • Using colon : like in Python
  • Ending with 'loop' keyword
3. What is the output of this Ruby code?
i = 1
while i <= 3 do
  puts i
  i += 1
end
medium
A. 1 2 3
B. 1 2 3 4
C. 0 1 2 3
D. No output

Solution

  1. Step 1: Trace the loop iterations

    i starts at 1. Loop runs while i <= 3. It prints i then adds 1 each time.
  2. Step 2: List printed values

    Prints 1, then 2, then 3. When i becomes 4, condition fails and loop stops.
  3. Final Answer:

    1 2 3 -> Option A
  4. Quick Check:

    Prints 1 to 3 inclusive [OK]
Hint: Count from start to condition limit inclusive [OK]
Common Mistakes:
  • Including 4 in output
  • Starting from 0 instead of 1
  • No output due to syntax errors
4. Identify the error in this Ruby code:
count = 5
while count > 0
  puts count
end
medium
A. Wrong variable name used in condition
B. Missing increment or decrement causing infinite loop
C. Syntax error: missing 'do' keyword
D. puts should be outside the loop

Solution

  1. Step 1: Analyze loop condition and body

    count starts at 5 and condition is count > 0. Inside loop, count is never changed.
  2. Step 2: Understand loop behavior

    Since count never decreases, condition stays true forever, causing infinite loop.
  3. Final Answer:

    Missing increment or decrement causing infinite loop -> Option B
  4. Quick Check:

    Loop needs variable change to stop [OK]
Hint: Always update loop variable inside while [OK]
Common Mistakes:
  • Forgetting to change loop variable
  • Assuming 'do' is mandatory
  • Thinking puts outside loop fixes error
5. How can you use a while loop to sum all even numbers from 2 to 10 inclusive in Ruby?
hard
A. i = 2 sum = 0 while i <= 10 do sum += i i += 2 end puts sum
B. i = 1 sum = 0 while i <= 10 do if i % 2 == 0 sum += i end i += 1 end puts sum
C. sum = 0 while i <= 10 do sum += i i += 1 end puts sum
D. i = 2 sum = 0 while i < 10 do sum += i i += 2 end puts sum

Solution

  1. Step 1: Understand the goal

    We want to add even numbers from 2 to 10 inclusive.
  2. Step 2: Check each option's logic

    i = 2 sum = 0 while i <= 10 do sum += i i += 2 end puts sum starts at 2 and increments by 2 up to 10 inclusive, summing even numbers correctly. i = 1 sum = 0 while i <= 10 do if i % 2 == 0 sum += i end i += 1 end puts sum checks each number from 1 to 10 and adds if even, also correct but less efficient. sum = 0 while i <= 10 do sum += i i += 1 end puts sum sums all numbers, not just even. i = 2 sum = 0 while i < 10 do sum += i i += 2 end puts sum stops before 10, missing 10.
  3. Step 3: Choose best option

    i = 2 sum = 0 while i <= 10 do sum += i i += 2 end puts sum is more direct and efficient for summing even numbers from 2 to 10 inclusive.
  4. Final Answer:

    Option A code correctly sums even numbers 2 to 10 inclusive -> Option A
  5. Quick Check:

    Increment by 2 starting at 2, sum all [OK]
Hint: Increment by 2 starting at 2 to sum even numbers [OK]
Common Mistakes:
  • Forgetting to include 10 in sum
  • Adding all numbers, not just even
  • Using wrong loop condition
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard