Google News
logo
R - Interview Questions
When should you apply "next" statement in R? When is it appropriate to use the "next" statement in R?
A data scientist will use next to skip an iteration in a loop. As an example :
> val1 <- 1:30
> for(val in val1){
+ if(val == 25){
+ next
+ }
+ print(val)
+ }
This piece of code will iterate through the numbers from 1 to 30. It will skip 25 as we have used the next statement to skip the iteration from which we move on to the next value. We will obtain the output from 1-24 and 26-30.
Advertisement