Skip to content

R bug? (if-else problem in main program)

5 messages · Anita Gulyasne Goldpergel, Lorenz Gygax, Peter Dalgaard +1 more

#
Hi everybody, 

I've found a very interesting problem in R: the if-else statement doesn't
work in a main program. Sounds crazy, but true. 

I tried this very easy example, and I got syntax error at the "else" line. 

Example:
 -------
a <- 1 

if ( a == 1 )
 print("yes")
else
 print("no")
 -------- 

I tried on Windows and on Linux, but none of them works.
(Detailed version info at the end of the mail.) 

Temporally you could fix it in two way: 

1. Put it in 1 line:
if ( a == 1) print("yes") else print("no")
will work. 

2. Put it in a function. 

If you have any idea for better resolve of this problem,
please send me an email, I'm not subscribed to the list. 

Thanks,
Anita 

Version info: 

1. Windows:
platform i386-pc-mingw32
arch     i386
os       mingw32
system   i386, mingw32
status
major    1
minor    5.0
year     2002
month    04
day      29
language R 

2. Linux:
platform i686-pc-linux-gnu
arch     i686
os       linux-gnu
system   i686, linux-gnu
status
major    1
minor    5.1
year     2002
month    06
day      17
language R 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
If you want to split this over several lines, you need curly braces:

a <- 1

if ( a == 1 ) {
  print ('yes')
} else {
  print ('no')
}

Cheers, Lorenz


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
"Lorenz Gygax" <gygax at ifi.unizh.ch> writes:
Yep. And it isn't a bug, but a design issue. In interactive mode we
print the result of an expression as soon as it is syntactically
complete, so upon seeing
[1] "yes"

we assume that we are done with that statement. After all, the next
line might be completely unrelated, e.g. "b <- 2", so we cannot wait
and see whether it starts with "else". So you need to ensure somehow
that the line is not syntactically complete, using braces as above, or,
e.g.,
+ print("no")
[1] "yes"
#
This is documented in many places.  See the help page for `if', or in
fuller detail, section 3.2 of the Language Manual.

The solution is to use braces

if(a == 1) {
    print("yes")
} else {
    print("no")
}

as described in ?"if".
On Tue, 5 Nov 2002, Anita Gulyasne Goldpergel wrote:

            
This is not `a main program' but using R interactively at the top (prompt)
level.  It will work in a function, the only sort of `programs' that exist
in R.

The help and manuals are your friends!

[...]
#
Thank you very much everybody for your answers!
Before I wrote my message, I had tried to use
brackets. So I've discovered something, what can be
useful for everyone: 

If I put the end-bracelet in different line from else,
it doesn't work: 

if ( a == 1 ) {
 print("yes")
 }
else {
 print("no")
} 

But if I put it one line, as you wrote, like } else {
it works. 

Thanks again, 

Anita 

Lorenz Gygax writes:
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._