Loops
for
Loops
Use for
loops to iterate over elements in a vector, list, or other sequence.
while
Loops
Use while
loops when you want to repeat something until a condition becomes FALSE
.
break
break
exits the loop immediately.
next
next
skips the rest of the current loop iteration and moves on to the next.
Infinite Loop Danger
If the while
condition is never FALSE
, the loop will run forever.
Common Errors
Some frequent mistakes encountered when working with loops in R.