[Rd] parse error with if else (PR#9551)

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue Mar 6 19:33:36 CET 2007



Stephanie.Mahevas at ifremer.fr wrote:
> Full_Name: Stephanie MAHEVAS
> Version: 2.4.1
> OS: Windows NT
> Submission from: (NULL) (134.246.55.50)
> 
> 
> 
> the two following instructions provide a synthax  error :
> 
> if ( 5 > 4 ) cat("ok1")
> else cat("ok2")

This is not a bug!

Since you are allowed to omit the else, R cannot know whether you want 
to provide it and thinks you have finished, since
     if ( 5 > 4 ) cat("ok1")
already is a valid and complete expression.
Therefore
     else cat("ok2")
is a *new* expression which obviously is not valid without if() before.

If you want that R looks at the whole at first, either do as below or 
make it a whole expression by putting it into braces as in:

{
     if ( 5 > 4 ) cat("ok1")
     else cat("ok2")
}


Uwe Ligges




> and
> 
> if ( 5 > 4 ){ cat("ok1")}
> else cat("ok2")
> 
> whereas these ones don't
> 
> if ( 5 > 4 ) cat("ok1") else cat("ok2")
> 
> and
> 
> if ( 5 > 4 ){ cat("ok1")
> }else cat("ok2")
> 
> It looks like a parser problem. If else is not on the same line as if or if the
> end of block statement of if } is not paste to else, else does not seem linked
> with if
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list