i

R Programming Complete Tutorial

R repeat loop

A repeat loop is used to iterate over a block of code multiple numbers of times. There is no condition check-in a repeat loop to exit. We must explicitly place a condition inside the loop's body and use the break statement to exit the loop.

Syntax:

Flow Diagram:

First, it will execute statements in the body then check the break statement. If it is true, it will exit the system; otherwise, it will go to the loop body and execute the statement again. It will continue until we get TRUE in break command.

Example:

In this example, we will do the same as we did in the previous two cases. We will add 1 to 100.

First, we initialize 0 to sum and 1 to x.

Now in the repeat loop, it will execute the statements, and our new sum will be 1, and x will be 2. Then it will check whether the break condition is true or not. Certainly, x is less than 101, and it will go to the repeat loop and execute the statements.  In the 2nd iteration sum=3, x=3. In the next iteration, sum 6, x=4. It will continue till x is 100.