Dear Peter,

--On donderdag 13 november 2003 15:05 +0100 Peter Dalgaard <[EMAIL PROTECTED]> wrote:

"Brown, Simon" <[EMAIL PROTECTED]> writes:

Dear r-help people,

could you confirm that this is correct behaviour for R? I am using RH9.

the code:
x1 <- 1:6
t1 <- 5
if (length(x1) >= t1) {
        cat("in the if \n")
} else {
        cat("in the else\n")
}

runs fine:
> source("test_if_else.R")
in the if
>

but the code:
x1 <- 1:6
t1 <- 5
if (length(x1) >= t1) {
        cat("in the if 2\n")
}
else {
        cat("in the else\n")
}

fails with the error:
> source("test_if_else2.R")
Error in parse(file, n, text, prompt) : syntax error on line 6
>

Could someone explain this to me please?

Again? This has been hashed over several times before. The basic issue is whether a statement can be assumed to be syntactically complete at the end of a line. It is fairly obvious what happens when you type the same expressions at an interactive prompt:

x1 <- 1:6
t1 <- 5
if (length(x1) >= t1) {
+ cat("in the if 2\n")
+ }
in the if 2
else {
Error: syntax error
cat("in the else\n")
in the else
}
Error: syntax error

Notice that the first right curly brace is seen as terminating the if
construct. Otherwise, R would need to wait and check whether the
*next* line starts with an "else" which would certainly be confusing
in interactive use. So R assumes the expression is finished and
evaluates it. Then it gets an "else" keyword that it doesn't know what
to do with and barfs.

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.

regards,
Paul


-- Paul Lemmens NICI, University of Nijmegen ASCII Ribbon Campaign /"\ Montessorilaan 3 (B.01.03) Against HTML Mail \ / NL-6525 HR Nijmegen X The Netherlands / \ Phonenumber +31-24-3612648 Fax +31-24-3616066

______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help

Reply via email to