R Repeat - r - learn r - r programming



  • In general, the Repeat loop executes the same condition many times until a stop condition is satisfied.
  • A repeat loop is used to iterate over a block of code multiple number of times.
 repeate loop

Syntax

repeat
{ 
   commands 
   if(condition) 
{
      break
   }
}

Sample Code

x <- 1
repeat 
{
   print(x)
   x = x+1
if (x == 10)
{
break
   }
}

Code Explanation

 repeate loop structure
  1. x specifies to string variable. Here x is initialized to value as 1.
  2. Here print() is used to print the value and that value stored in x variable.
  3. x = x+1, which means the x = 1 is incremented by 1 and becomes 9. If the condition to check and exit the loop when x takes the value of 10. Here break statement is used to exit the loop.

Output

 repeate loop output
  1. Here we have used a condition to check and exit the loop when x takes the value of 10. Hence, we have displayed the output that only values from 1 to 9 get printed.

Related Searches to R Repeat