English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

R Loops

Sometimes, we may need to execute the same block of code multiple times. Generally, statements are executed in order: the first statement in the function is executed first, followed by the second statement, and so on.

Programming languages provide various control structures with more complex execution paths.

Loop statements allow us to execute a statement or a group of statements multiple times, and here is a flowchart of loop statements in most programming languages:


The loop types provided by R language include:

  • repeat loop

  • while  loop

  • for  loop

The loop control statements provided by R language include:

  • break statement

  • Next statement

Loop control statements change the execution order of your code, and through them, you can achieve code jumps.

Loop type

repeat

The repeat loop will keep executing the code until the condition statement is false, at which point the loop will exit, and the break statement must be used to exit.

The syntax format is as follows:

repeat { 
    // related code 
    if(condition) {
       break
    }
}

The following example is when the variable cnt is 5 exits the loop, cnt is a counter variable:

v    ,
cnt  
 
   v
   cnt    cnt
   
   cnt

After executing the above code, the input result is:

[1] "Google" "w3codebox
[1] "Google" "w3codebox
[1] "Google" "w3codebox
[1] "Google" "w3codebox

while

As long as the given condition is true, the while loop statement in R language will repeatedly execute a target statement.

The syntax format is as follows:

while(condition)
{
   statement(s);
}

Here, 'statement(s)' can be a single statement or a block of code consisting of several statements.

The 'condition' can be any expression, and it will be true when it is any non-zero value. When the condition is true, the loop is executed. When the condition is false, the loop exits, and the program flow will continue to execute the next statement following the loop.

The following example in the variable cnt is less than 7 when outputting the content of the while statement block, cnt is the counter variable:

v    ,
cnt  
 cnt   
   v
   cnt    cnt

After executing the above code, the input result is:

[1] "Google" "w3codebox
[1] "Google" "w3codebox
[1] "Google" "w3codebox
[1] "Google" "w3codebox
[1] "Google" "w3codebox

for

The for loop statement in R programming language can repeatedly execute specified statements, and the number of repetitions can be controlled in the for statement.

The syntax format is as follows:

for (value in vector) {
    statements
}

The for loop in R language is very flexible, not only can it iterate over integer variables, but also over character vectors, logical vectors, lists, and other data types.

The following example output 26 letters, corresponding to the first four letters:

v  
  i    v 
   i

After executing the above code, the input result is:

[1] "A"
[1] "B"
[1] "C"
[1] "D"

Loop Control

break

The break statement in R language is inserted in the loop body, used to exit the current loop or statement, and start executing the next statement in the script.

If you use nested loops, the break statement will stop the execution of the innermost loop and start executing the outer loop statement.

break is also commonly used in switch statements.

The syntax format is as follows:

break

The following example in the variable cnt is 5 when using break to exit the loop, cnt is the counter variable:

v <- c("Google","w3codebox) cnt <- 2 repeat {   print(v)   cnt <- cnt+1     if(cnt > 5) {      break   } }

After executing the above code, the input result is:

[1] "Google" "w3codebox
[1] "Google" "w3codebox
[1] "Google" "w3codebox
[1] "Google" "w3codebox

next

The next statement is used to skip the current loop and start the next loop (similar to continue in other languages).

The syntax format is as follows:

next

The following example output 26 the front of a letter 6 letters, skip the current loop and start the next loop when the letter is D:

v  
  i    v 
   
    i     
      
   
   i

After executing the above code, the input result is:

[1] "A"
[1] "B"
[1] "C"
[1] "E"
[1] "F"