If {...} else {...}: Does the line break between "}" and "else" really matter?

I write the instruction if {...} else {...}in R as follows, because I consider it more readable.

Testifelse = function(number1)
{
     if(number1>1 & number1<=5)
     {
          number1 =1
     }
     else if ((number1>5 & number1 < 10))
     {
          number1 =2
     }
     else
     {
          number1 =3
     }

     return(number1)
}

According to ?Control:

... In particular, you should not have a new line between }and elsein order to avoid a syntax error when entering a structure if ... elseon the keyboard or through the source ...

the above function will throw a syntax error, but actually it works! What's going on here?

Thank you for your help.

+4
source share
1 answer

Original Question and Answer

If we add the R console:

if (1 > 0) {
  cat("1\n");
  }
else {
  cat("0\n");
  }

why doesn't it work?

R - , R- . ( by @JohnColeman: . , Python , . , R, , , (, , ).)

if (1 > 0) {
  cat("1\n");
  }

, . ,

else {
  cat("0\n");
  }

, , , else.

:

if (1 > 0) {
  cat("1\n");
  } else {
  cat("0\n");
  }

.

, C, . "" .


, ,

! {} . , R,

{statement_1; statement_2; ...; statement_n;}

, statement_n.

:

{
  if (1 > 0) {
    cat("1\n");
    }
  else {
    cat("0\n");
    }
}

1.

{} , - , }. , , {}.

+5

All Articles