VBScript If..ElseIf..Else 語句



一個 If 語句後跟一個或多個 ElseIf 語句,其中包含布林表示式,然後後跟一個預設 else 語句,當所有條件變為 false 時執行此語句。

語法

VBScript 中 If-ElseIf-Else 語句的語法為 ―

If(boolean_expression) Then
   Statement 1
	.....
	.....
   Statement n
ElseIf (boolean_expression) Then
   Statement 1
	.....
	....
   Statement n
ElseIf (boolean_expression) Then
   Statement 1
	.....
	....
   Statement n
Else
   Statement 1
	.....
	....
   Statement n
End If

流程圖

VBScript If..Elseif..Else statement

示例

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Dim a  
         a = -5

         If a > 0 Then
            Document.write "a is a POSITIVE Number"
         ElseIf a < 0 Then
            Document.write "a is a NEGATIVE Number"
         Else
            Document.write "a is EQUAL than ZERO"
         End If
      </script>
   </body>
</html>

執行以上程式碼時,將產生以下結果 ―

a is a NEGATIVE Number
vbscript_decisions.htm
廣告
© . All rights reserved.