In the field of Computer Science, the Loops can be as a structure that keeps on repeats a sequence until the given condition is met. Likewise, we have the loops in R programming i.e. For, While, and Repeat loop. Let’s see each of them in this article.

loops in r

Purpose of Loops

The main motive or the task of the loops is to repeat a sequence of tasks until the given input condition is met. There are multiple types of loops that you can use based on the given requirements. Different types of loops in R are used to perform different looping tasks. 

1. For Loops in R programming

For loop is one of the prominent and widely used loops in R. The for loop in R is most commonly used to iterate over the sequenced items. It can be also called as the entry focused loop, because it first tests the test condition and later executes the body of the loop.

#For loop in R
for (val in 1:10)
print(val)
1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10

wow!!!

You just executed a For loop in R. In the above for loop, the motive is to iterate over the statement and return the values and It did the same above.

The for loop has printed the values from 1 to 10.

#creates a list
numbers<-c('one','two','three','four','five','six','seven','eight','nine','ten')

#The for loop in R
for (word in numbers)
    print(word)
[1] "one"
[1] "two"
[1] "three"
[1] "four"
[1] "five"
[1] "six"
[1] "seven"
[1] "eight"
[1] "nine"
[1] "ten"

In the above for loop, we have created a list and then created a for loop to iterate over the list and print each value in it.

2. While Loops in R programming

The while loop in R is one of the controlled statement, which runs the statement / statements till the condition is false.

The While loop also first executes the test condition and then executes the body of the loop if and only if the test condition is FALSE.

#while loop execution 
val <- 5
while (val <= 15 )  
 { 
     # input statements 
     print(val) 
    val = val + 1
 } 
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
[1] 11
[1] 12
[1] 13
[1] 14
[1] 15

Well, the above while loop in R, it takes the input condition and continues the execution until the condition becomes false as shown above.

3. Repeat Loops in R programming

The repeat loop is also one of the controlled statements in R programming. The repeat loop in R programming is used to repeat the statements as per the input.

Let’s see how it works in R!

i <- 0

repeat 
 { 
     # statement to be executed 
     print("I love HackAnons!") 
     
     # incrementing 
     i = i + 1
     
     #stop condition 
     if (i == 5) 
     { 
         # using break statement to terminate   
         break
     } 
 } 
[1] "I love HackAnons!"
[1] "I love HackAnons!"
[1] "I love HackAnons!"
[1] "I love HackAnons!"
[1] "I love HackAnons!"

In the above repeat loop, we have initialized a variable with 0 and after each iteration, it increments by 1.

When it goes greater than 5, the break statement will come into play and terminates the loop.

Wrapping Up

The loops are the most important concepts in the Programming. We have 3 loops in R programming –

  • For loop
  • While loop
  • Repeat loop

I hope this article will make you feel better about the basic idea of working in a loop in R programming.

There are some changes in writing loops in R compared to Python and other languages and those changes are really small. 

That’s all for now. Happy Looping!!!

More read: Loops in R programming