The repeat loop in R

To execute a block of code more than once in a continuous loop, the repeat keyword or loop is utilised in R. However, the repeat statement presents no condition to exit from the loop, and this is where the need for the break statement arises.

Process flow for Repeat loop:

Step 1: Initialization of variables.

Step 2: Execution of the block of statements inside the repeat loop.

Step 3: Use an expression inside the repeat loop to exit the loop.

Step 4: Check for the exit condition.

Step 5: Execution of the break statement to exit from the loop for the true condition.

Step 6: Repeat the execution of the block of statements inside the repeat loop for the false condition.

Syntax:

repeat {
   commands
   if(condition) {
      break
   }
}

Example 1:

var <- 2
repeat {
    print(var)
    var = var+2
if (var == 10){
        break
    }
}

Output:

[1] 2
 
[1] 4
 
[1] 6
 
[1] 8

Example 2:

var <- 1
repeat {
  print("Process in Progress");
  if(var >= 5)
    break
  var<-var+1
}

Output:

[1] "Process in Progress"
 
[1] "Process in Progress"
 
[1] "Process in Progress"
 
[1] "Process in Progress"
 
[1] "Process in Progress"

Example 3:

msg <- c("I","am","working","on","it.")
count <- 1
repeat {
   print(msg)
   count <- count + 1
   if(count > 5) {
      break
   }
}

Output:

[1] "I" "am" "working" "on" "it."
 
[1] "I" "am" "working" "on" "it."
 
[1] "I" "am" "working" "on" "it."
 
[1] "I" "am" "working" "on" "it."
 
[1] "I" "am" "working" "on" "it."

Example 4:

sum <- 0
{
    num <-100
    num <-as.integer(num)
}
repeat{
    sum <- sum + num
    num = num+1
    if(num>105){
        break
    }
}
cat("100+101+102+103+104+105 =",sum)

Output:

100+101+102+103+104+105 = 615

Example 5:

sum <- 0
num <-5
repeat{
sum = sum + num
num = num + 1
cat("\nSUM =",sum)
if(num>20){
        break
    }
}

Output:

SUM = 5
 
SUM = 11
 
SUM = 18
 
SUM = 26
 
SUM = 35
 
SUM = 45
 
SUM = 56
 
SUM = 68
 
SUM = 81
 
SUM = 95
 
SUM = 110
 
SUM = 126
 
SUM = 143
 
SUM = 161
 
SUM = 180
 
SUM = 200

Example 6:

var <- 2
repeat {
  if(var == 20)
    break
  if(var == 10){
    var = var+2
    next
  }
  print(var)
  var <- var+2
}

Output:

[1] 2
 
[1] 4
 
[1] 6
 
[1] 8
 
[1] 12
 
[1] 14
 
[1] 16
 
[1] 18

Example 7: Square of a Number:

iteration_count <- 10
iteration_count <- as.integer(iteration_count)
num <-1
repeat{
    cat("\nSquare of",num,"=",(num*num))
    if(num == iteration_count)
        break
    num <- num+1
}

Output:

Square of 1 = 1
 
Square of 2 = 4
 
Square of 3 = 9
 
Square of 4 = 16
 
Square of 5 = 25
 
Square of 6 = 36
 
Square of 7 = 49
 
Square of 8 = 64
 
Square of 9 = 81
 
Square of 10 = 100

Example 8: Cube of a Number:

iteration_count <- 10
iteration_count <- as.integer(iteration_count)
num <-1
repeat{
    cat("\nCube of",num,"=",(num*num*num))
    if(num == iteration_count)
        break
    num <- num+1
}

Output:

Cube of 1 = 1
 
Cube of 2 = 8
 
Cube of 3 = 27
 
Cube of 4 = 64
 
Cube of 5 = 125
 
Cube of 6 = 216
 
Cube of 7 = 343
 
Cube of 8 = 512
 
Cube of 9 = 729
 
Cube of 10 = 1000

Example 9: Multiplication Table:

iteration_count <- 10
iteration_count <- as.integer(iteration_count)
num <-5
factor <-1
cat("Multiplication Table of 5\n")
repeat{
    cat("\n",num,"*",factor,"=",(num*factor))
    if(factor == iteration_count)
        break
    factor <- factor+1
}

Output:

Multiplication Table of 5
 
5 * 1 = 5
 
5 * 2 = 10
 
5 * 3 = 15
 
5 * 4 = 20
 
5 * 5 = 25
 
5 * 6 = 30
 
5 * 7 = 35
 
5 * 8 = 40
 
5 * 9 = 45
 
5 * 10 = 50
 
&nbsp;
Please follow and like us:
Content Protection by DMCA.com