Repeat Loop in R

It is a simple loop that will run the same statement or a group of statements repeatedly until the stop condition has been encountered. Repeat loop does not have any condition to terminate the loop, a programmer must specifically place a condition within the loop’s body and use the declaration of a break statement to terminate this loop. If no condition is present in the body of the repeat loop then it will iterate infinitely.

R – Repeat loop Syntax: 

repeat 
{ 
   statement
 
   if( condition ) 
   {
      break
   }
}

Repeat loop Flow Diagram: 

To terminate the repeat loop, we use a jump statement that is the break keyword. Below are some programs to illustrate the use of repeat loops in R programming.

Example 1: Program to display numbers from 1 to 5 using a repeat loop in R. 

R




# R program to demonstrate the use of repeat loop
 
val = 1
 
# using repeat loop
repeat
{
    # statements
    print(val)
    val = val + 1
 
    # checking stop condition
    if(val > 5)
    {
        # using break statement
        # to terminate the loop
        break
    }
}


Output: 

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

In the above program, the variable val is initialized to 1, then in each iteration of the repeat loop the value of val is displayed and then it is incremented until it becomes greater than 5. If the value of val becomes greater than 5 then a break statement is used to terminate the loop.

Example 2: Program to display a statement five times. 

R




# R program to illustrate
# the application of repeat loop
 
# initializing the iteration variable with 0
i <- 0
 
# using repeat loop
repeat
{
    # statement to be executed multiple times
    print("Geeks 4 geeks!")
 
    # incrementing the iteration variable
    i = i + 1
 
    # checking the stop condition
    if (i == 5)
    {
        # using break statement
        # to terminate the loop
        break
    }
}


Output: 

[1] "Geeks 4 geeks!"
[1] "Geeks 4 geeks!"
[1] "Geeks 4 geeks!"
[1] "Geeks 4 geeks!"
[1] "Geeks 4 geeks!"

Here, initially, the variable i is initialized with 0 then in each iteration of the repeat loop after printing Geeks 4 geeks! the value of i is incremented till it becomes 5 and the condition in the if statement becomes true then, the break statement is executed to terminate the repeat loop. 

Loops in R (for, while, repeat)

In R programming, we require a control structure to run a block of code multiple times. Loops come in the class of the most fundamental and strong programming concepts. A loop is a control statement that allows multiple executions of a statement or a set of statements. The word ‘looping’ means cycling or iterating. 

A loop asks a query, in the loop structure. If the answer to that query requires an action, it will be executed. The same query is asked again and again until further action is taken. Any time the query is asked in the loop, it is known as an iteration of the loop. There are two components of a loop, the control statement, and the loop body.  The control statement controls the execution of statements depending on the condition and the loop body consists of the set of statements to be executed.

In order to execute identical lines of code numerous times in a program, a programmer can simply use a loop. 

Similar Reads

There are three types of loops in R programming:

For Loop While Loop Repeat Loop...

For Loop in R

It is a type of control statement that enables one to easily construct an R loop that has to run statements or a set of statements multiple times. For R loop is commonly used to iterate over items of a sequence. It is an entry-controlled loop, in this loop, the test condition is tested first, then the body of the loop is executed, the loop body would not be executed if the test condition is false....

While Loop in R

...

Repeat Loop in R

...

Jump Statements in Loop

...

Contact Us