OneBite.Dev - Coding blog in a bite size

do a while loop in R

Code snippet on how to do a while loop in R

i <- 1
while(i <= 5) {
  print(i)
  i <- i + 1
}

This code creates a while loop in R that will print the numbers 1 through 5. To begin, a variable “i” is declared as equal to 1. Then, the while loop is executed as long as “i” is less than or equal to 5. Inside the while loop, the number stored in “i” is printed, followed by an increase of “i” so that it is now one number more than before. This loop continues until the value of “i” is no longer less than or equal to 5, at which point the while loop is finished and the program moves on.

r