← Home · Ruby skill tree

An interactive Ruby lesson

Loops

A loop repeats code. Build on Conditions and Arrays to repeat a calculation or visit array elements with while.

p displays an inspected value, with quotes around strings. Predict the output, press Run, then open the expected output to check your answer. Edit the code and run it again. Reset restores that example; Stop ends a running example.

The first Run downloads Ruby into your browser. JavaScript and a network connection are needed to run examples. Each run starts fresh, so define every variable in the editor you run. The code and answers remain readable without JavaScript.

1. Count upward

while checks a condition before running its body, the code between while and end. After the body finishes, Ruby checks the condition again. These comparisons return true or false. A condition of false or nil stops the loop; other values let it run.

Here count += 1 increases the counter after each display. This moves count toward 4, where count < 4 becomes false. Without the update, the condition would stay true. Predict each number and the line after the loop.

Show expected output
1
2
3
"Finished"

Try it: Change the condition to count < 5. Predict the last number displayed.

2. Check the condition first

count starts at 3, so count < 3 is false at the first check. The body runs zero times. The p(count) after end still runs. Predict the output and how many lines it has.

Show expected output
3

Try it: Start count at 0. Predict the numbers from the body and the final value displayed after the loop.

3. Count down

remaining starts positive. Each remaining -= 1 subtracts one, moving it toward 0. At 0, remaining > 0 is false. Predict the numbers displayed inside the loop and after it.

Show expected output
3
2
1
0

Try it: Start remaining at 2. Predict how many times the body runs.

4. Visit array elements

index starts at 0 and increases by one after each element. For this array, the last valid index is names.length - 1, which is 2. The condition uses < so the body stops before reading past the end. Predict each name in order.

Show expected output
"Ana"
"Ben"
"Cora"

Try it: Change "Cora" to "Dee". Then predict whether an empty array, [], would display any names.

5. Build a running total

total starts at 0. Each pass adds the current score to it, then moves to the next index. Only the final total is displayed. Predict the output.

Show expected output
12

Try it: Change the last score from 6 to 1. Predict the new total. What total would an empty array produce?

6. Stop early with break

break exits this while loop as soon as a number is greater than 5. The index += 1 below break does not run on that pass. Code after the loop still runs. Predict the matching number and the final index.

Show expected output
7
1

Try it: Replace 7 with 5. Predict the output when no number matches. The bound index < numbers.length still stops the loop after the last element.

Practice on your own

Replace the code in the last editor with a program for these tasks. Predict the output, then run it.

  1. Create prices = [3, 5, 2], index = 0, and total = 0.
  2. Use while index < prices.length to add the current price to total. Increase index by one each time.
  3. After the loop, use p to display total, then index.
  4. Explain why the loop stops. Predict both lines if prices is [].
Show one solution and its output
prices = [3, 5, 2]
index = 0
total = 0
while index < prices.length
  total += prices[index]
  index += 1
end
p(total)
p(index)

Output:

10
3

Ruby documentation

The Ruby 4.0 documentation explains while loops and break.

Return to the Ruby skill tree to choose another topic.