Loops

R has the usual for and while loops, though idiomatic R often reaches for vectorized operations instead once you're past the basics — loops are still the right place to start learning the logic.

for: looping over a vector

R for-loop.R
for (score in c(88, 92, 79)) {
  print(score)
}
Console
[1] 88
[1] 92
[1] 79

score takes on each value of the vector in turn, one per iteration — no index variable or manual counting required.

while: looping on a condition

R while-loop.R
count <- 3
while (count > 0) {
  print(count)
  count <- count - 1
}
Console
[1] 3
[1] 2
[1] 1
Worth knowing: Loops work fine in R, but a lot of real R code reaches instead for vectorized arithmetic (like scores + 5 from the Vectors lesson) or the apply-family of functions (sapply(), lapply()) for anything more involved. Both are usually faster and shorter than the equivalent loop — worth keeping in mind as you get more comfortable, even though plain loops are the clearest place to start.