Open In App

R Next Statement

Last Updated : 03 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Next statement in R is used to skip any remaining statements in the loop and continue the execution of the program. In other words, it is a statement that skips the current iteration without loop termination. 

‘next’ is a loop control statement just like the break statement. But ‘next’ statement works opposite to that of the break statement, instead of terminating the loop, it forces to execute the next iteration of the loop.In R, the next statement is a control statement that allows you to jump to the next iteration of a loop without running any of the statements that are still in the loop for the current iteration. To process or skip specific cases or circumstances inside the loop, this phrase is frequently used in conjunction with conditional statements (such as if statements).

Syntax – Next Statement in R Programming Language 

next

Next Statement in R Programming Flowchart

R - Next StatementGeeksforgeeks

R – Next Statement

Next Statement in R Language Example 

Using next in the for loop 

R




# R program to illustrate next in for loop
 
val <- 6:11
 
# Loop
for (i in val)
{
    if (i == 8)
    {
        # test expression
        next 
    
    print(i) 


Output: 

[1] 6
[1] 7
[1] 9
[1] 10
[1] 11

This R program iterates through the elements of the val vector, which has values between 6 and 11, using the for loop. An if clause inside the loop determines if the value of i is now equal to 8 or not. If so, the next keyword is used to skip to the next loop iteration without running any additional words in the loop body.

Here’s what the program does for each iteration of the loop:

  • In the first iteration, i is 6. The if statement is not true, so print(i) is executed and the output is “6”.
  • In the second iteration, i is 7. The if statement is not true, so print(i) is executed and the output is “7”.
  • In the third iteration, i is 8. The if statement is true, so the next keyword is executed and the loop skips to the next iteration without printing anything.
  • In the fourth iteration, i is 9. The if statement is not true, so print(i) is executed and the output is “9”.
  • In the fifth iteration, i is 10. The if statement is not true, so print(i) is executed and the output is “10”.
  • In the sixth iteration, i is 11. The if statement is not true, so print(i) is executed and the output is “11”

The next statement can be used with any other loop also like ‘while’ or ‘repeat’ loop in a similar way as it is used with for loop above.

Using next in the while loop 

R




# R program to illustrate next in while loop
 
val <- 6
i = 11
# Loop
while(i>val)
{
    if (i == 8)
    {
        # test expression
        next 
    
    print(i) 
    i = i - 1


Output: 

[1] 11
[1] 10
[1] 9

This R programme iterates over a series of decreasing values starting at 11 until it reaches the value of 6, using the while loop. An if clause inside the loop determines if the value of i is now equal to 8 or not. If so, the next keyword is used to skip to the next loop iteration without running any additional words in the loop body.

Here’s what the program does for each iteration of the loop:

  • In the first iteration, i is 11. The if statement is not true, so print(i) is executed and the output is “11”. Then, i is decreased by 1 to become 10.
  • In the second iteration, i is 10. The if statement is not true, so print(i) is executed and the output is “10”. Then, i is decreased by 1 to become 9.
  • In the third iteration, i is 9. The if statement is not true, so print(i) is executed and the output is “9”. Then, i is decreased by 1 to become 8.
  • In the fourth iteration, i is 8. The if statement is true, so the next keyword is executed and the loop skips to the next iteration without printing anything. Then, i is decreased by 1 to become 7.
  • In the fifth iteration, i is 7. The if statement is not true, so print(i) is executed and the output is “7”. Then, i is decreased by 1 to become 6.
  • In the sixth iteration, i is 6. The loop condition i>val is no longer true, so the loop exits without executing any further statements.

Using next in the repeat loop 

R




# R program to illustrate next in repeat loop
 
i = 0
 
# Loop
repeat
{
 
  if(i == 10)   
    break
     
  if(i == 5)
  {   
    next      
  
    print(i) 
    i = i + 1


Output: 

[1] 0
[1] 1
[1] 2
[1] 3
[1] 4

The statements in the loop body are iterated over in this R program’s repeat loop until the break statement is reached. The current value of i is tested inside the loop using an if statement to see if it equals 10. If so, the break statement is carried out, ending the loop. Another if clause determines whether the value of i at the moment equals 5. If so, the next keyword is used to skip to the next loop iteration without running any additional words in the loop body.

Here’s what the program does for each iteration of the loop:

  • In the first iteration, i is 0. The if statement is not true, so print(i) is executed and the output is “0”. Then, i is increased by 1 to become 1.
  • In the second iteration, i is 1. The if statement is not true, so print(i) is executed and the output is “1”. Then, i is increased by 1 to become 2.
  • In the third iteration, i is 2. The if statement is not true, so print(i) is executed and the output is “2”. Then, i is increased by 1 to become 3.
  • In the fourth iteration, i is 3. The if statement is not true, so print(i) is executed and the output is “3”. Then, i is increased by 1 to become 4.
  • In the fifth iteration, i is 4. The if statement is not true, so print(i) is executed and the output is “4”. Then, i is increased by 1 to become 5.
  • In the sixth iteration, i is 5. The if statement is true, so the next keyword is executed and the loop skips to the next iteration without printing anything. Then, i is increased by 1 to become 6.
  • In the seventh iteration, i is 6. The if statement is not true, so print(i) is executed and the output is “6”. Then, i is increased by 1 to become 7.
  • This process continues until i reaches 10. At that point, the if statement with break is true, so the loop is exited.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads