Reputation: 1409
I am having some difficulty in using the function else
in R.
When I input ?else
, I didn't get any help about the function else
.
When I run the following program:
i=1
if(i>1){print("aa")}
else{print("bb")}
the else
didn't work. Can someone tell me the reasons?
Upvotes: 3
Views: 194
Reputation: 7475
to get help type
?'else'
look at the paragraph in help
Note that it is a common mistake to forget to put braces ({ .. }) around your statements, e.g., after if(..) or for(....). 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(i>1){print("aa")
}else{print("bb")}
or
if(i>1){print("aa")}else{print("bb")}
will presumably work for you.
i=1
{
if(i>1){print("aa")}
else{print("bb")}
}
would also work. The key is to let the parser know to expect more input.
Upvotes: 6