[R] Problem with parser and if/else

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Nov 14 09:22:05 CET 2003

On Fri, 14 Nov 2003, Paul Lemmens wrote:

> I'm trying to grasp this: if you're saying (or are you saying) that the 
> only way to have if() know that an else will be present is to put it on the 
> same line as the closing curly brace of the if() (compound) statement. But 
> if I look at some code from, e.g., aov and lm, I see plenty violations of 
> that rule.

The actual rule is given in my reference (the one that Ben Bolker did not 
bother to look up) earlier in this thread.

You need to ensure that the code is syntactically incomplete when `else'
is encountered.  That will always be true inside a braced expression such 
as the bodies of the functions you quote.  But at top-level, you do need
to write

if(condition) something else something_else


if(condition) {
} else {


if(condition) {
else {

fails the test.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list