# 9 三心二意

if (條件一) {

# 程式一

} else if (條件二) {

# 程式二

} else {

# 程式三

}

## 9.1 兩個分支

weather <- sample(c("sunny", "rainy"), size = 1)
weather
if (weather == "sunny"){
print("Running outdoors!")
} else {
print("Working out in the gym!")
}

> 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 <- 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 三個分支

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 <- 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 <- 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 <- 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 四個分支或更多

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 <- 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 <- 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 <- 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 <- 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."