Skip to content

Exercise 8 of 10 · Loops and collections

Loop Over a Price List

What you will make

A program that prints every price in a list, one per line, alongside its position.

The one new idea: Looping over a vector with length()

Combining a for loop with a vector's length() is the standard R pattern for processing every item in a collection — it's the building block behind sums, averages, searches, and almost any data-processing task.

Go straight to the code ↓

The concept

A very common R pattern combines a for loop with length() to process every element of a vector, regardless of how many elements it has: for (i in 1:length(x)) { ... }. On each pass, i holds the current position, and x[i] gets you the single value sitting at that position. This is more flexible than writing 1:4 directly, because if the vector's size changes later, length(x) automatically adjusts the loop to match — you never have to update a hard-coded number.

A common slip is to use the vector's name by itself inside the loop body instead of indexing into it. Since a vector's name on its own always refers to the whole thing, forgetting the square brackets means every pass through the loop ends up working with all the values instead of just the current one.

Worked example

r
colors <- c("red", "green", "blue")

for (i in 1:length(colors)) {
  cat(i, colors[i], "\n")
}
Output
1 red
2 green
3 blue

Here, length(colors) is 3, so the loop runs for i equal to 1, 2, and then 3. On each pass, colors[i] picks out just one color — the one at the current position — pairing it with i in the printed output.

Your turn

The starter code loops correctly over every position in the prices vector, but inside the loop it prints prices — the entire vector — on every single pass, instead of just the one price at the current position. Fix the cat() call so it indexes into prices using the loop variable, printing a single value per line.

If something goes wrong

If every printed line shows all four prices instead of just one, that confirms the vector is being used without indexing — look for a place where the vector's bare name appears inside the loop body and add square brackets with the loop variable inside them. If nothing prints at all, double-check that the loop's range, 1:length(prices), wasn't accidentally changed or removed.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
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

Writing prices[i] but placing i inside the wrong set of brackets.
It needs to be prices[i] — the square brackets go directly after the vector name to select one element by position; writing something like prices(i) or i[prices] would not work the way indexing does.
Assuming the loop is broken because it doesn't run enough times.
The loop actually runs the correct number of times (once per price); the bug is what gets printed on each pass, not how many passes happen.
Using length(i) instead of length(prices) to build the range.
i is a single number inside the loop, not the vector — you need the length of prices itself to know how many positions exist to loop over.

Want a blank editor instead? Open the R playground.