Stephanie.Mahevas at ifremer.fr
2007-Mar-06 17:15 UTC
[Rd] parse error with if else (PR#9551)
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") 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
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
On 3/6/2007 12:15 PM, 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") > > 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 ifThis is a documented "feature" of the language, not a bug. (See the language guide, section 3.2.1 "if", around the 4th paragraph.) It arises because R tries to evaluate a statement as soon as it is complete, and if ( 5 > 4 ) cat("ok1") is a complete statement. You can get the behaviour you want by wrapping the whole think in curly brackets so that R doesn't start evaluating too early, e.g. > { + if ( 5 > 4 ) cat("ok1") + else cat("ok2") + } ok1> Duncan Murdoch
Apparently Analagous Threads
- Protection stack overflow when calling setGeneric()/setMethod() from within .onLoad()
- ov_open/ov_test weirdness
- [PULL 5/5] virtio/s390: virtio: constify virtio_config_ops structures
- [PULL 5/5] virtio/s390: virtio: constify virtio_config_ops structures
- [PATCH] s390: virtio: constify virtio_config_ops structures