
- Swift 教程
- Swift - 首頁
- Swift - 概述
- Swift - 環境
- Swift - 基本語法
- Swift - 變數
- Swift - 常量
- Swift - 字面量
- Swift - 註釋
- Swift 運算子
- Swift - 運算子
- Swift - 算術運算子
- Swift - 比較運算子
- Swift - 邏輯運算子
- Swift - 賦值運算子
- Swift - 位運算子
- Swift - 其他運算子
- Swift 高階運算子
- Swift - 運算子過載
- Swift - 算術溢位運算子
- Swift - 恆等運算子
- Swift - 範圍運算子
- Swift 資料型別
- Swift - 資料型別
- Swift - 整數
- Swift - 浮點數
- Swift - Double
- Swift - 布林值
- Swift - 字串
- Swift - 字元
- Swift - 類型別名
- Swift - 可選型別
- Swift - 元組
- Swift - 斷言和前提條件
- Swift 控制流
- Swift - 決策
- Swift - if 語句
- Swift - if...else if...else 語句
- Swift - if-else 語句
- Swift - 巢狀 if 語句
- Swift - switch 語句
- Swift - 迴圈
- Swift - for in 迴圈
- Swift - while 迴圈
- Swift - repeat...while 迴圈
- Swift - continue 語句
- Swift - break 語句
- Swift - fall through 語句
- Swift 集合
- Swift - 陣列
- Swift - 集合
- Swift - 字典
- Swift 函式
- Swift - 函式
- Swift - 巢狀函式
- Swift - 函式過載
- Swift - 遞迴
- Swift - 高階函式
- Swift 閉包
- Swift - 閉包
- Swift - 逃逸閉包和非逃逸閉包
- Swift - 自動閉包
- Swift 面向物件
- Swift - 列舉
- Swift - 結構體
- Swift - 類
- Swift - 屬性
- Swift - 方法
- Swift - 下標
- Swift - 繼承
- Swift - 重寫
- Swift - 初始化
- Swift - 析構
- Swift 高階
- Swift - ARC 概述
- Swift - 可選鏈
- Swift - 錯誤處理
- Swift - 併發
- Swift - 型別轉換
- Swift - 巢狀型別
- Swift - 擴充套件
- Swift - 協議
- Swift - 泛型
- Swift - 訪問控制
- Swift - 函式與方法
- Swift - SwiftyJSON
- Swift - 單例類
- Swift 隨機數
- Swift 不透明型別和裝箱型別
- Swift 有用資源
- Swift - 線上編譯
- Swift - 快速指南
- Swift - 有用資源
- Swift - 討論
Swift - if...else if...else 語句
一個if語句後面可以跟著一個可選的else if...else語句,這在使用單個if...else if語句測試各種條件時非常有用。或者我們可以說,if… else if…else語句允許你按順序檢查多個條件。
它建立了一個決策樹,其中每個條件都按順序進行評估,第一個為真的條件的語句塊將被執行。如果沒有任何條件為真,則 else 部分將被執行。if…else if…else 語句也稱為 if… else 階梯。
使用if、else if 和 else語句時,需要記住以下幾點。
一個if可以有零個或一個 else,並且它必須出現在任何 else if 之後。
一個if可以有零到多個 else if,並且它們必須出現在 else 之前。
一旦一個else if成功,則不會測試任何剩餘的else if或else。
語法
以下是 if…else if…else 語句的語法:
if boolean_expression1{ /* Execute when the boolean expression 1 is true */ } else if boolean_expression2{ /*Execute when the boolean expression 2 is true*/ } else if boolean_expression3{ /*Execute when the boolean expression 3 is true*/ }else{ /* Execure when none of the above conditions is true */ }
流程圖
下面的流程圖將展示 if…else if…else 語句的工作原理。

示例
Swift 程式演示if… else if…else語句的使用。
import Foundation var varA:Int = 100; /* Check the boolean condition using the if statement */ if varA == 20 { /* If the condition is true then print the following */ print("varA is equal to than 20"); } else if varA == 50 { /* If the condition is true then print the following */ print("varA is equal to than 50"); } else { /* If the condition is false then print the following */ print("None of the values is matching"); } print("Value of variable varA is \(varA)");
輸出
它將產生以下輸出:
None of the values is matching Value of variable varA is 100
示例
Swift 程式使用if…else階梯檢查當前季節。
import Foundation let myMonth = 5 // Checking Season using if...else ladder if myMonth >= 3 && myMonth <= 5 { print("Current Season is Spring") } else if myMonth >= 6 && myMonth <= 8 { print("Current Season is Summer") } else if myMonth >= 9 && myMonth <= 11 { print("Current Season is Autumn") } else { print("Current Season is Winter") }
輸出
它將產生以下輸出:
Current Season is Spring
示例
Swift 程式使用 if…else if…else 語句計算學生的成績。
import Foundation let marks = 89 // Determine the grade of the student if marks >= 90 { print("Grade A") } else if marks >= 80 { print("Grade B") } else if marks >= 70 { print("Grade C") } else if marks >= 60 { print("Grade D") } else { print("Grade F") }
輸出
它將產生以下輸出:
Grade B
廣告