Friday, November 27, 2020

R next Statement

 The next statement is used to skip any remaining statements in the loop and continue executing. In simple words, a next statement is a statement which skips the current iteration of a loop without terminating it. When the next statement is encountered, the R parser skips further evaluation and starts the next iteration of the loop.

This statement is mostly used with for loop and while loop.

Note: In else branch of the if-else statement, the next statement can also be used.

Syntax

There is the following syntax for creating the next statement in R

  1. next  

Flowchart

R next statement

Example 1: next in repeat loop

  1. a <- 1            
  2. repeat {          
  3.   if(a == 10)    
  4.     break    
  5.   if(a == 5){    
  6.     next       
  7.   }    
  8.   print(a)    
  9.   a <- a+1    
  10. }    

Output:

R next statement

Example 2: next in while loop

  1. a<-1    
  2. while (a < 10) {    
  3.   if(a==5)    
  4.     next    
  5. print(a)    
  6.   a = a + 1    
  7. }    

Output:

R next statement

Example 3: next in for loop

  1. x <- 1:10  
  2. for (val in x) {  
  3. if (val == 3){  
  4. next  
  5. }  
  6. print(val)  
  7. }  

Output:

R next statement

Example 4

  1. a1<- c(10L,-11L,12L,-13L,14L,-15L,16L,-17L,18L)  
  2. sum<-0  
  3. for(i in a1){  
  4.     if(i<0){  
  5.         next  
  6.     }  
  7.     sum=sum+i  
  8. }  
  9. cat("The sum of all positive numbers in array is=",sum)  

Output:

R next statement

Example 5

  1. j<-0  
  2. while(j<10){  
  3.     if (j==7){  
  4.         j=j+1  
  5.         next  
  6.     }  
  7.     cat("\nnumber is =",j)  
  8.     j=j+1  
  9. }  

Output:

R next statement

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...