R - if 語句



一個if語句由一個布林表示式和一個或多個語句組成。

語法

在 R 中建立if語句的基本語法為:

if(boolean_expression) {
   // statement(s) will execute if the boolean expression is true.
}

如果布林表示式的值為true,則將執行 if 語句內的程式碼塊。如果布林表示式的值為false,則將執行 if 語句結束後的第一組程式碼(右花括號之後)。

流程圖

R if statement

示例

x <- 30L
if(is.integer(x)) {
   print("X is an Integer")
}

編譯並執行上述程式碼後,將產生以下結果:

[1] "X is an Integer"
r_decision_making.htm
廣告