In this tutorial, we will learn about the next statement in R. We will cover the basics of using the next statement to skip the current iteration of a loop and proceed with the next iteration.
A next statement is used to skip the current iteration of a loop and proceed with the next iteration. When a next statement is encountered, the remaining code inside the loop for the current iteration is skipped, and the loop continues with the next iteration.
The syntax for the next statement in R is:
next
The next statement can be used in for loops and while loops to skip the current iteration and proceed with the next iteration.
for (i in 1:10) {
if (i %% 2 == 0) {
next
}
print(i)
}
[1] 1 [1] 3 [1] 5 [1] 7 [1] 9
i
and initialize it to 1.i
is less than or equal to 10.i
is odd.i <- 1
while (i <= 10) {
if (i %% 2 != 0) {
i <- i + 1
next
}
print(i)
i <- i + 1
}
[1] 2 [1] 4 [1] 6 [1] 8 [1] 10