YouTip LogoYouTip

R Loop

R Loops

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

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

Loop statements allow us to execute a statement or group of statements multiple times. Below is a flowchart of loop statements found in most programming languages:

Image 1: Loop Structure

R provides the following types of loops:

  • repeat loop
  • while loop
  • for loop

R provides the following loop control statements:

  • break statement
  • next statement

Loop control statements change the execution order of your code, allowing you to jump within loops.

Loop Types

repeat

The repeat loop executes code indefinitely until a condition becomes true, at which point it exits using the break statement.

The syntax is as follows:

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

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

Example

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

Executing the above code produces the following output:

 "Google" ""
 "Google" ""
 "Google" ""
 "Google" ""

while

In R, the while loop statement repeatedly executes a target statement as long as the given condition is true.

The syntax is as follows:

while (condition) {
    statement(s);
}

Here, statement(s) can be a single statement or a block of statements.

condition can be any expression. It evaluates to true when it is any non-zero value. The loop executes while the condition is true. When the condition becomes false, the loop exits, and program flow continues with the next statement immediately following the loop.

The following example outputs the content of the while block as long as the variable cnt is less than 7, where cnt is a counter variable:

Example

v <- c("Google", "")
cnt <- 2
while (cnt < 7) {
    print(v)
    cnt = cnt + 1
}

Executing the above code produces the following output:

 "Google" ""
 "Google" ""
 "Google" ""
 "Google" ""
 "Google" ""

for

In R, the for loop statement can repeatedly execute specified statements, with the number of repetitions controlled within the for statement.

The syntax is as follows:

for (value in vector) {
    statements
}

R's for loop is very flexible. It can iterate not only over integer variables but also over character vectors, logical vectors, lists, and other data types.

The following example outputs the first four letters of the 26-letter alphabet:

Example

v <- LETTERS[1:4]
for (i in v) {
    print(i)
}

Executing the above code produces the following output:

 "A"
 "B"
 "C"
 "D"

Loop Control

break

The break statement in R is inserted into the loop body to exit the current loop or statement and begin executing the next statement in the script.

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

break is also commonly used in switch statements.

The syntax is as follows:

break

The following example uses break to exit the loop when the variable cnt is 5, where cnt is a counter variable:

Example

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

Executing the above code produces the following output:

 "Google" ""
 "Google" ""
 "Google" ""
 "Google" ""

next

The next statement is used to skip the current iteration of a loop and proceed to the next iteration (similar to continue in other languages).

The syntax is as follows:

next

The following example outputs the first 6 letters of the 26-letter alphabet, skipping the current iteration when the letter is D and proceeding to the next iteration:

Example

v <- LETTERS[1:6]
for (i in v) {
    if (i == "D") {
        # D will not be printed, skip this iteration and go to the next
        next
    }
    print(i)
}

Executing the above code produces the following output:

 "A"
 "B"
 "C"
 "E"
 "F"
← R StringPhp Csprng β†’