Exercise 6 of 10 · Loops
Count from One to Five
What you will make
A short program that prints the numbers 1 through 5, one per line, using a loop.
The one new idea: for loops over a numeric range (1:n)
Loops let a program repeat an action without writing it out by hand for every value — this is the tool you'll reach for anytime you need to process a range of numbers or, soon, every item in a collection.
Go straight to the code ↓The concept
A for loop repeats a block of code once for each value in a sequence. The most common way to build a numeric sequence in R is with the : operator: 1:5 produces the sequence 1, 2, 3, 4, 5. Written as for (i in 1:n) { ... }, the loop variable i takes on each of those values in turn, running the body once per value. This is far more convenient than writing out five separate cat() calls by hand, and it still works correctly if n changes later.
One subtlety worth knowing: : has higher precedence than + or -. That means 1:n - 1 doesn't mean "from 1 to (n minus 1)" the way it might in ordinary math — R builds 1:n first, then subtracts 1 from every number in that sequence.
Worked example
laps <- 3
for (i in 1:laps) {
cat("Lap ", i, "\n", sep = "")
}Lap 1
Lap 2
Lap 3Here, 1:laps builds the sequence 1, 2, 3, and the loop prints one labeled line per value. Because there's no stray arithmetic mixed into the range, i takes on exactly the values 1, 2, and 3, in that order — nothing shifted.
Your turn
The starter code is meant to loop over 1 through 5, but its range expression has an extra - 1 tacked onto the end. Because : is evaluated before -, that extra subtraction shifts every value in the sequence down by one. Remove the subtraction from the range so the loop counts through the intended numbers.
If something goes wrong
If the printed numbers start at 0 instead of 1, or seem to be missing the highest value, that's the telltale sign of this exact precedence issue — the sequence is being built correctly but then shifted afterward. Try temporarily printing the full range on its own, like cat(1:n - 1) versus cat(1:n), to see the difference directly, then remove whichever piece is producing the shifted sequence.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Trying to fix this by changing n from 5 to 6.
- That would shift the range to 1:6 - 1, which still starts at 0 due to the same precedence issue — the real problem is the stray subtraction, not the value of n.
- Assuming 1:n - 1 means '1 through (n minus 1)'.
- That's how it would read in ordinary math notation, but R evaluates ':' before '-' when there are no parentheses, so it actually means '(1 through n), each minus 1' — a classic R precedence trap.
- Not noticing the loop still runs without any error.
- This bug doesn't crash the program at all — it just silently produces the wrong sequence of numbers, so you have to check the actual printed values, not just whether the code runs.
Want a blank editor instead? Open the R playground.