If Then Else 是程式設計中最基礎的決策結構。本文從單行、區塊、多條件 ElseIf、巢狀結構到防禦性程式設計模式,完整涵蓋 ASP 開發者必須掌握的條件判斷技巧。
<%
If condition Then statement
If condition Then statement1 Else statement2
' 範例
If IsEmpty(Session("UserID")) Then Response.Redirect "/login.asp"
If age >= 18 Then Response.Write "成年" Else Response.Write "未成年"
%>
<%
If condition Then
' 多行陳述
statement1
statement2
Else
' 多行陳述
statement3
statement4
End If
%>
<%
If condition1 Then
' 邏輯 1
ElseIf condition2 Then
' 邏輯 2
ElseIf condition3 Then
' 邏輯 3
Else
' 預設邏輯
End If
%>
| 運算子 | 說明 | 範例 |
|---|---|---|
= | 等於(數值/字串) | score = 100 |
<> | 不等於 | status <> "done" |
>, <, >=, <= | 比較運算子 | age >= 18 |
And | 邏輯且(短路:否) | age >= 18 And hasID |
Or | 邏輯或(短路:否) | role="admin" Or role="mod" |
Not | 邏輯非 | Not IsEmpty(val) |
Xor | 邏輯互斥或 | flag1 Xor flag2 |
Eqv | 邏輯等價 | a Eqv b |
Imp | 邏輯推導 | a Imp b |
Is | 物件參考比較 | obj Is Nothing |
Like | 模式比對 | filename Like "*.jpg" |
<%
Dim score, grade
score = 85
If score >= 90 Then
grade = "A"
ElseIf score >= 80 Then
grade = "B"
ElseIf score >= 70 Then
grade = "C"
ElseIf score >= 60 Then
grade = "D"
Else
grade = "F"
End If
Response.Write "分數:" & score & ",等級:" & grade
' 輸出:分數:85,等級:B
%>
<%
Function ProcessPayment(orderId, amount, paymentMethod)
' 守衛子句:前置驗證失敗即返回
If orderId = "" Or Not IsNumeric(orderId) Then
ProcessPayment = "錯誤:無效訂單編號"
Exit Function
End If
If amount <= 0 Or Not IsNumeric(amount) Then
ProcessPayment = "錯誤:金額必須大於 0"
Exit Function
End If
If paymentMethod = "" Then
ProcessPayment = "錯誤:請選擇付款方式"
Exit Function
End If
' 核心業務邏輯(扁平化,無深層巢狀)
Dim conn, rs, sql
Set conn = GetConnection()
' 檢查訂單狀態
sql = "SELECT Status FROM Orders WHERE OrderID = ?"
Set rs = conn.Execute(sql, Array(orderId))
If rs.EOF Then
ProcessPayment = "錯誤:訂單不存在"
rs.Close : Set rs = Nothing
Exit Function
End If
If rs("Status") <> "PENDING" Then
ProcessPayment = "錯誤:訂單狀態不允許付款:" & rs("Status")
rs.Close : Set rs = Nothing
Exit Function
End If
rs.Close
' 執行付款邏輯...
' ...
ProcessPayment = "成功"
End Function
%>
<%
' ❌ 箭頭型程式碼(難讀、難維護)
If userLoggedIn Then
If userHasPermission Then
If resourceExists Then
If resourceAccessible Then
' 核心邏輯
Else
Response.Write "資源不可存取"
End If
Else
Response.Write "資源不存在"
End If
Else
Response.Write "權限不足"
End If
Else
Response.Write "請先登入"
End If
' ✅ 扁平化:守衛子句 + 早期返回
If Not userLoggedIn Then Response.Write "請先登入" : Response.End
If Not userHasPermission Then Response.Write "權限不足" : Response.End
If Not resourceExists Then Response.Write "資源不存在" : Response.End
If Not resourceAccessible Then Response.Write "資源不可存取" : Response.End
' 核心邏輯從這裡開始,零巢狀
%>
<%
' VBScript 無原生三元運算子,可自訂 IIf 函數
Function IIf(condition, truePart, falsePart)
If condition Then IIf = truePart Else IIf = falsePart
End Function
' 使用範例
Dim statusMsg
statusMsg = IIf(userLoggedIn, "歡迎回來," & Session("UserName"), "請先登入")
Response.Write statusMsg
' ⚠️ 注意:IIf 會「評估所有參數」,不具短路特性
' IIf(x = 0, 1, 1/x) 會在 x=0 時產生除零錯誤!
%>
Else If(有空格)會被視為巢狀 If,需 End If 結束StrComp(str1, str2, 1) = 0 不區分If var = Null 永遠為 False,必須用 IsNull(var)If obj = Nothing 錯誤,必須用 If obj Is NothingIf IsEligibleForDiscount(user) Then