【发布时间】:2020-11-27 17:51:46
【问题描述】:
我对 R 编程不熟悉了。当我尝试编写我的第一个 if else 语句时,我遇到了一个我不理解的奇怪行为。
当我运行以下代码时:
x = 4;
y=4;
if (x==y) {
print('they are equal');
} else {
print('they are not equal');
}
我没有收到任何错误,并且得到了预期的输出。但是,当我更改以下相同代码的缩进时:
if(x==y){print('they are equal');}
else{print('they are not equal');}
我收到一条错误消息,提示“错误:“else”中出现意外的“else”。
那么这是否意味着 R 是一种像 Python 一样的缩进敏感语言?
【问题讨论】:
-
来自文档:
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.
标签: r if-statement