5

I am confused now! First I learned it is not possible in R, but I often forget it and it sometimes works. And then it doesn't work again! I start to recognize the pattern - it works in for loop or in another block statement, but not outside:

for (i in 1:10) {

    if (0) 
        a <- 1
    else
        a <- 2

    b <- 3
}

Doesn't make sense to me... any explanation? And manual reference? In every R resource I read it seemed like brackets are necessary:

if (0) {
    a <- 1
} else {
    a <- 2
}

PS: not a duplicate, the marked question don't even talk about the variant without brackets, which is core of my question. It is talking about the necessity of the line breaks.

Tomas
  • 57,621
  • 49
  • 238
  • 373
  • 3
    @ZheyuanLi this is not a duplicate man, they don't even talk about the variant without brackets, which is core of my question. They are talking about the necessity of the line breaks. – Tomas Sep 28 '16 at 15:57
  • 1
    @ZheyuanLi man, I don't know what you are talking about. *This is a different question*. It might be due to the same part of the R-parser, but I don't see that far. – Tomas Sep 28 '16 at 16:01
  • this is the difference between running a chunk of code and running code line-by-line, yes? – rawr Sep 28 '16 at 16:15
  • Possible duplicate of [Unexpected 'else' in "else" error](http://stackoverflow.com/questions/14865435/unexpected-else-in-else-error) – Caleb Sep 28 '16 at 16:16

3 Answers3

7

When the entire "if" statement is enclosed in curly brackets (as in the body of a function), you do not need the "else" to be on the same line as the closing bracket of the "if".

This code yields a syntax error:

a <- 1
if (a >1)
{
  print("a is greater than 1")
}
else
{
  print ("a is not greater than 1")
}

While this does not, solely because of the addition of the first and last brackets:

{
  a <- 1
  if (a >1)
  {
    print("a is greater than 1")
  }
  else
  {
    print ("a is not greater than 1")
  }
}
tommylicious
  • 309
  • 5
  • 11
0

any explanation?

The short answer here is that you need to either use brackets or put the else clause on the same line as the if. From the documentation:

When the if statement is not in a block the else, if present, must appear on the same line as the end of statement2. Otherwise the new line at the end of statement2 completes the if and yields a syntactically complete statement that is evaluated.

So, you can use this:

for (i in 1:10) {
    if (0) {
        a <- 1
    }
    else {
        a <- 2
    }
    b <- 3
}

Or this:

for (i in 1:10) {
    if (0) a <- 1 else a <- 2
    b <- 3
}
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Sorry, I don't understand your answer. How do you explain the difference between the if statement being outside or inside the for loop? – Tomas Sep 28 '16 at 16:04
  • Oops, sorry -- missed the `for` aspect. Add semicolons after your statements to separate them. I'll update the answer as well. – Caleb Sep 28 '16 at 16:08
0

I would like to contribute to the why part of the question - why there are two different possibilities

hrbrmstr states following in this SO question

When the initial if is followed by a compound expression (indicated by the {} pair) the parser by default is going to expect the expression followed by else to be compound as well. The only defined use of else is with compound expressions. This is even stated in the docs: if(cond) cons.expr else alt.expr where cons.expr and alt.expr are defined to be compound. As @Berry indicated, you can use the way R parses function definitions to work around this, but it'd be better to be consistent in bracket use (IMO).

Berry Boessenkool writes:

R reads these commands line by line, so it thinks you're done after executing the expression after the if statement. Remember, you can use if without adding else.

  • I'm having trouble seeing this. Could you maybe explain what is the meaning of "compound expression" and how it pertains to this? – Mike M Nov 26 '20 at 21:48