Loops in R | For Loop, While Loop and Repeat loop

Loops are among the fundamental concepts of computer programming. In its simplest form, a loop repeats the same things multiple times. One’s can use loops to execute a code block multiple times according to a condition or until a condition is achieved.

The ability to repeat single or group instructions can be instrumental in many situations. Consider, for example, a program that has to find the sum of 100 integers. As one can imagine, adding these numbers individually would be very time-consuming and challenging.

Therefore, we can use loops to make this process much more manageable. Hence, knowledge of loops is a fundamental concept for any programmer.

All programming languages have their flavor of loops. Similar to other languages, R language also offers different kinds of loops. Here, we will see what types of loops are available in R, along with an example to show how to use them.

Loops in R: The Overview

In the R language, loops are used whenever we require a code block to be executed several times in our program. Instead of writing the block multiple times, we can write the block of code in a loop statement which makes our code more readable and efficient.

In R, loops has two parts: the control statement and the loop body. The control statement is used to specify the number of times we want the code block to be executed.

It generally contains a breaking condition to determine when the loop will terminate. The loop body contains the block of code that will be executed a specified number of times according to the control statement.

Loops in R Programming
Loops in R Programming

Types of Loops in R

R language offers three different kinds of loops. These loops are

  1. for Loop
  2. while Loop
  3. repeat Loop

for Loop in R

The for loop is the most commonly used loop in any programming language. for loop is used when we want to repeatedly execute a code block for a fixed number of times. In other words, for loop is used when the number of iterations is known before entering the loop.

The syntax for a for loop in R is given below.

#sytax of for loop in r
for (variable_name <in> sequence ){
  #block of code to execute

Program to display the first 10 positive integers using for loop:

for (i in 1:10){
  #print statement
  • for loop contains our control statement in the parenthesis which includes an iterator and a sequence or a range for that iterator.
  • It will keep in check that the iterator i remains between 1 and 10. As soon as the iterator exceeds 10, it will terminate the loop.
  • The body of the loop prints the value of the iterator which is incremented after each iteration of the loop. Thus, our loop will print numbers in sequence from 1 to 10.

while Loop in R

while loop is similar to for loop to execute a block of code several times, however, they differ because this loop can be used when the number of iterations is not known at the start. Or the loop needs to be terminated when some condition is met rather than when the counter runs out.

The syntax for while loop in R is given below

while (condition){
  #code block to execute

Program to display the first 10 positive integers using a while loop.

num = 1
while (num <= 10) {
  num = num + 1
  • We start by declaring num variable and then define the control statement of the while loop in the parenthesis.
  • The loop executes as long as the control statement is true. The body prints the value of num and increments its value by 1.
  • It ensures that the loop will not run indefinitely but for specific iterations until the condition gets false.

repeat Loop in R programming language

repeat loop is very similar to other loops in that they will execute a code block multiple times, however, they differ in that repeat loop will always execute the code block at least once. repeat loop is not used as often as for or while loop is generally used when the number of iterations are not known & the condition for terminating the loop is based on some kind of user input.

The syntax for a repeat loop in R is given below.

  #loop body
  if (condition){ #conditional statement

Program to display the first 10 positive integers using a repeat loop:

num = 1
     num = num + 1
     if (num <= 10){
  • Here, we again declare the variable but unlike the while loop, write the body of the loop first.
  • It will print the value of the num variable and increment it.
  • Then, it checks the break statement at line 6 and terminates when the controlling statement does not hold.

Stay in the Loop

Get the weekly email from Algoideas that makes reading the AI/ML stuff instructive. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement -

You might also like...