Search code examples
rwhile-loop

Why is the next function not working in R studio for me here


I need to iterate from 2 to 6 and skip the number 4.

This is what I got so far

#/!/usr/bin/env/R

i <-2
 while(i < 7)
{
  if( i == 4)
  {
    next 
  }
  print(i)
  i = i + 1
 }

when I run this in , it just stops at 3 and when ever I try to add the next command, it automatically adds bracket changing it to next().

It also messes up the console as nothing works until in the console until I stop the console and refresh it, the code works perfectly fine if I replace next with break but doesn't work this way.


Solution

  • You still need to iterate i in the if statement, otherwise it will loop forever.

    i <-2
     while(i < 7)
    {
      if( i == 4)
      {
        i=i+1
        next  
      }
      print(i)
      i = i + 1
     }