9 三心二意
流程控制可以幫助我們在程式中撰寫不同的劇本(scenario),根據不同的判斷條件執行不同的程式。在 R 語言中我們使用 if
、else if
與 else
建立出不同的分支,它的外觀架構長得像這樣子:
if (條件一) {
# 程式一
} else if (條件二) {
# 程式二
} else {
# 程式三
}
如果條件一的判斷結果為 TRUE
就執行程式一,條件二的判斷結果為 TRUE
就執行程式二;假如條件一與條件二的判斷結果皆為 FALSE
,就執行程式三。如果有更多的條件,只要增加 else if
的個數即可。
9.1 兩個分支
舉例來說,一個喜愛運動的人早上起床會看天氣決定當天的行程,在兩種天氣條件下,使用 if
跟 else
就可以建立出一個兩種劇本的行程:
weather <- sample(c("sunny", "rainy"), size = 1)
weather
if (weather == "sunny"){
print("Running outdoors!")
} else {
print("Working out in the gym!")
}
如果 weather
為 "sunny"
晴天,就在戶外跑步:
> weather <- sample(c("sunny", "rainy"), size = 1)
> weather
[1] "sunny"
> if (weather == "sunny"){
+ print("Running outdoors!")
+ } else {
+ print("Working out in the gym!")
+ }
[1] "Running outdoors!"
如果 weather
不是 "sunny"
晴天,就上健身房運動:
> weather <- sample(c("sunny", "rainy"), size = 1)
> weather
[1] "rainy"
> if (weather == "sunny"){
+ print("Running outdoors!")
+ } else {
+ print("Working out in the gym!")
+ }
[1] "Working out in the gym!"
9.2 三個分支
在三種天氣條件下,使用 if
、else if
與 else
可以建立出一個三種劇本的行程:
weather <- sample(c("sunny", "rainy", "cloudy"), size = 1)
weather
if (weather == "sunny"){
print("Running outdoors!")
} else if (weather == "cloudy"){
print("Cycling!")
} else {
print("Working out in the gym!")
}
如果 weather
為 "sunny"
晴天,就在戶外跑步:
> weather <- sample(c("sunny", "rainy", "cloudy"), size = 1)
> weather
[1] "sunny"
> if (weather == "sunny"){
+ print("Running outdoors!")
+ } else if (weather == "cloudy"){
+ print("Cycling!")
+ } else {
+ print("Working out in the gym!")
+ }
[1] "Running outdoors!"
如果 weather
為 "cloudy"
陰天,就去騎單車:
> weather <- sample(c("sunny", "rainy", "cloudy"), size = 1)
> weather
[1] "cloudy"
> if (weather == "sunny"){
+ print("Running outdoors!")
+ } else if (weather == "cloudy"){
+ print("Cycling!")
+ } else {
+ print("Working out in the gym!")
+ }
[1] "Cycling!"
如果 weather
既不是 "sunny"
亦不是 "cloudy"
那就去健身房運動:
> weather <- sample(c("sunny", "rainy", "cloudy"), size = 1)
> weather
[1] "rainy"
> if (weather == "sunny"){
+ print("Running outdoors!")
+ } else if (weather == "cloudy"){
+ print("Cycling!")
+ } else {
+ print("Working out in the gym!")
+ }
[1] "Working out in the gym!"
9.3 四個分支或更多
在四種或更多個天氣條件下,依然使用 if
、else if
與 else
建立出一個四種劇本的行程:
weather <- sample(c("sunny", "cloudy", "drizzle", "showers", "storm"), size = 1)
weather
if (weather == "sunny"){
print("Running outdoors!")
} else if (weather == "cloudy"){
print("Cycling!")
} else if (weather == "drizzle") {
print("Working out in the gym!")
} else {
print("Couch potato.")
}
如果 weather
為 "sunny"
晴天,就在戶外跑步:
> weather <- sample(c("sunny", "cloudy", "drizzle", "showers", "storm"), size = 1)
> weather
[1] "sunny"
> if (weather == "sunny"){
+ print("Running outdoors!")
+ } else if (weather == "cloudy"){
+ print("Cycling!")
+ } else if (weather == "drizzle") {
+ print("Working out in the gym!")
+ } else {
+ print("Couch potato.")
+ }
[1] "Running outdoors!"
如果 weather
為 "cloudy"
陰天,就去騎單車:
> weather <- sample(c("sunny", "cloudy", "drizzle", "showers", "storm"), size = 1)
> weather
[1] "cloudy"
> if (weather == "sunny"){
+ print("Running outdoors!")
+ } else if (weather == "cloudy"){
+ print("Cycling!")
+ } else if (weather == "drizzle") {
+ print("Working out in the gym!")
+ } else {
+ print("Couch potato.")
+ }
[1] "Cycling!"
如果 weather
為 "drizzle"
毛毛雨,就去健身房運動:
> weather <- sample(c("sunny", "cloudy", "drizzle", "showers", "storm"), size = 1)
> weather
[1] "drizzle"
> if (weather == "sunny"){
+ print("Running outdoors!")
+ } else if (weather == "cloudy"){
+ print("Cycling!")
+ } else if (weather == "drizzle") {
+ print("Working out in the gym!")
+ } else {
+ print("Couch potato.")
+ }
[1] "Working out in the gym!"
如果 weather
既不是 "sunny"
、"cloudy"
亦不是 "drizzle"
就待在家裡看電視:
> weather <- sample(c("sunny", "cloudy", "drizzle", "showers", "storm"), size = 1)
> weather
[1] "storm"
> if (weather == "sunny"){
+ print("Running outdoors!")
+ } else if (weather == "cloudy"){
+ print("Cycling!")
+ } else if (weather == "drizzle") {
+ print("Working out in the gym!")
+ } else {
+ print("Couch potato.")
+ }
[1] "Couch potato."