Home » R Repeat Loop

R Repeat Loop

by Online Tutorials Library

R repeat loop

A repeat loop is used to iterate a block of code. It is a special type of loop in which there is no condition to exit from the loop. For exiting, we include a break statement with a user-defined condition. This property of the loop makes it different from the other loops.

A repeat loop constructs with the help of the repeat keyword in R. It is very easy to construct an infinite loop in R.

The basic syntax of the repeat loop is as follows:

Flowchart R Repeat Loop

  1. First, we have to initialize our variables than it will enter into the Repeat loop.
  2. This loop will execute the group of statements inside the loop.
  3. After that, we have to use any expression inside the loop to exit.
  4. It will check for the condition. It will execute a break statement to exit from the loop
  5. If the condition is true.
  6. The statements inside the repeat loop will be executed again if the condition is false.

Example 1:

Output

R Repeat Loop

Example 2:

Output

R Repeat Loop

Example 3: Infinity repeat loop

Output

R Repeat Loop

Example 4: repeat loop with next

Output

R Repeat Loop

Example 5:

Output

R Repeat Loop


Next TopicR While Loop

You may also like