W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
if語句后面可以是一個可選的else語句,當(dāng)布爾表達式為false時執(zhí)行。
在R中創(chuàng)建if ... else語句的基本語法是 -
if(boolean_expression) {
// statement(s) will execute if the boolean expression is true.
} else {
// statement(s) will execute if the boolean expression is false.
}
如果布爾表達式的計算結(jié)果為真,則將執(zhí)行if代碼塊,否則將執(zhí)行代碼塊。x <- c("what","is","truth")
if("Truth" %in% x) {
print("Truth is found")
} else {
print("Truth is not found")
}
[1] "Truth is not found"
這里“Truth”和“truth”是兩個不同的字符串。if語句后面可以跟一個可選的else if ... else語句,這對于使用single if ... else if語句測試各種條件非常有用。
當(dāng)使用if,else if,else語句有幾點要記住。
在R中創(chuàng)建if ... else if ... else語句的基本語法是 -
if(boolean_expression 1) {
// Executes when the boolean expression 1 is true.
} else if( boolean_expression 2) {
// Executes when the boolean expression 2 is true.
} else if( boolean_expression 3) {
// Executes when the boolean expression 3 is true.
} else {
// executes when none of the above condition is true.
}
x <- c("what","is","truth")
if("Truth" %in% x) {
print("Truth is found the first time")
} else if ("truth" %in% x) {
print("truth is found the second time")
} else {
print("No truth found")
}
當(dāng)上面的代碼被編譯和執(zhí)行時,它產(chǎn)生以下結(jié)果 -[1] "truth is found the second time"
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: