OneCompiler

Loops

1. For

For loop is used to iterate a set of statements based on a condition for a spefic number of times.

Syntax

for (value in vector) {
  # code
}

Example

day <- c("Monday", "Tuesday","Wednesday", "Thursday","Friday", "Saturday", "Sunday")
for ( i in day) {
   print(i)
}

Check Result here

2. While

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations is not known in advance.

Syntax

while(condition){  
#code
}  

Example

# printing 1 to 10 using while loop in R
i <- 1
while ( i <= 10) {
   print(i)
   i = i+1;
}

Check result here

3. Repeat

Repeat executes a block of code again and again until stop criteria is met.

Syntax

repeat { 
   #code
   if(condition) {
      break
   }
}

Example

i <- 1
repeat {
   print(i)

   if ( i == 5){
     break
   }
   i = i+1;   
}

Check result here