Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

while-loop

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 r, 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.

like image 325
user12197328 Avatar asked Dec 07 '25 12:12

user12197328


1 Answers

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
 }
like image 85
user2974951 Avatar answered Dec 09 '25 20:12

user2974951



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!