0

The simplest script ever, so why do I get an error:

> x <- -5
> if(x > 0){
+     print("Non-negative number")
+ } 
> else{
Error: unexpected 'else' in "else"
>     print("Negative number")
[1] "Negative number"
> }
Error: unexpected '}' in "}"

If I simply put the else after } then there is no problem.

> x <- -5
> if(x > 0){
+     print("Non-negative number")
+ } else{
+     print("Negative number")
+ }
[1] "Negative number"

Thing is I have always written it the first way without problem; am I going crazy?

Jimmy
  • 1
  • 1
  • 1
  • 5
  • 2
    Please tag the question with the programming language you're using. This is probably an artifact of whatever shell/console you're typing the commands into. – Matt Ball Oct 31 '15 at 16:45
  • Also the same: http://stackoverflow.com/questions/23944698/error-unexpected-in-if-print-else-print – Rich Scriven Oct 31 '15 at 17:15

3 Answers3

6

If you're entering code interactively, R thinks the if clause is done as soon as it sees the first close-bracket. Then it "thinks" the else is starting a new statement, which is not allowed. From help("else"):

In particular, you should not have a newline between ‘}’ and ‘else’ to avoid a syntax error in entering a ‘if ... else’ construct at the keyboard or via ‘source’. For that reason, one (somewhat extreme) attitude of defensive programming is to always use braces, e.g., for ‘if’ clauses.

If you use R CMD BATCH then this will work. You could also use brackets, as suggested by the help file:

x <- -5
{ if(x > 0){
     print("Non-negative number")
  } 
  else {
     print("Negative number")
  }
}

or just include the else on the same line as the close-bracket.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • they're putting the if/else constructs inside function bodies as one way to get them to be within a block (which can be done more minimally by putting them within () or {} ...) But they are answering essentially the same question. I guess I'm glad that my answer adds a little bit. – Ben Bolker Oct 31 '15 at 17:13
1

It's not your fault. That's the way the R console works! When you type the closing bracket, the console expects there won't be any else if or else clauses, so it executes the if clause.

A workaround is to enclose the whole if.. else clause in brackets, like this:

x <- -5
{   
  if(x > 0){
     print("Non-negative number")
  } 
  else{
     print("Negative number")
  }
}
codingEnthusiast
  • 3,800
  • 2
  • 25
  • 37
  • 1
    Thanks guys, on reflection I virtually always write my if else statements inside for loops or within a function and so the issue didn't come up before for me. – Jimmy Oct 31 '15 at 17:12
  • It's OK, I'm glad to help and I'm also glad that you've solved your problem. – codingEnthusiast Oct 31 '15 at 17:13
0

From this Documentation it states that you need to type it this way.

It is important to note that else must be in the same line as the closing braces of the if statements.

So even if you're not in interactive mode, you may experience issues.

ergonaut
  • 6,929
  • 1
  • 17
  • 47