Lesson 8 of 16

Loops

For Loops

R's for loop iterates over elements of a vector:

for (i in 1:5) {
  cat(i, "\n")
}

You can iterate over any vector:

fruits <- c("apple", "banana", "cherry")
for (fruit in fruits) {
  cat(fruit, "\n")
}

While Loops

The while loop runs while a condition is true:

x <- 1
while (x <= 5) {
  cat(x, "\n")
  x <- x + 1
}

Loop Control

Use next to skip to the next iteration, and break to exit the loop:

for (i in 1:10) {
  if (i %% 2 == 0) next  # Skip even numbers
  if (i > 7) break       # Stop after 7
  cat(i, "\n")
}
# Prints: 1 3 5 7

A Note on Vectorization

In R, you should prefer vectorized operations over loops when possible. Loops are fine for learning, but R is optimized for vector operations:

# Slow (loop)
result <- c()
for (i in 1:5) result <- c(result, i ^ 2)

# Fast (vectorized)
result <- (1:5) ^ 2

Your Task

Use a for loop to print the first 7 numbers of the Fibonacci sequence (1, 1, 2, 3, 5, 8, 13), each on its own line.

R runtime loading...
Loading...
Click "Run" to execute your code.