假如我们希望在条件为 true 时执行某条语句,并当条件不为 true 时执行另一条语句,就必须添加关键词 "Else":
if i=10 then msgbox "Hello" else msgbox "Goodbye" end If
当条件为 true 时会执行第一段代码,当条件不成立时执行第二段代码(当 i 不等于 10 时)。
If....Then.....Elseif
假如你希望选择多套代码之一来执行,可以使用if...then...elseif语句:
if payment="Cash" then msgbox "You are going to pay cash!" elseif payment="Visa" then msgbox "You are going to pay with visa." elseif payment="AmEx" then msgbox "You are going to pay with American Express." else msgbox "Unknown method of payment." end If
Select Case
假如你希望选择多套代码之一来执行,可以使用 SELECT 语句:
select case payment case "Cash" msgbox "You are going to pay cash" case "Visa" msgbox "You are going to pay with visa" case "AmEx" msgbox "You are going to pay with American Express" case Else msgbox "Unknown method of payment" end select
以上代码的工作原理:首先,我们需要一个简单的表达式(常常是一个变量),并且这个表达式会被做一次求值运算。然后,表达式的值会与每个 case 中的值作比较,如果匹配,被匹配的 case 所对应的代码会被执行。