Examples
实例
The if...then...else statement
This example demonstrates how to write the if...then..else statement.
怎样写if...then...else 声明
The if...then...elseif... statement
This example demonstrates how to write the if...then...elseif statement.
怎样写if..then...elseif 声明
The select case statement
This example demonstrates how to write the select case statement.
怎样写 select case 声明
Conditional Statements
条件声明
Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.
写代码的时候想要根据不同的条件来执行不同的代码,这个时候你就应该使用条件声明了。
In VBScript we have three conditional statements:
在VBScript中我们有三种声明可以选择:
if statement - use this statement if you want to execute a set of code when a condition is true
如你想当一条件为真就执行一段代码的话可以使用这个声明
if...then...else statement - use this statement if you want to select one of two sets of lines to execute
当你想要根据条件从两部分代码中执行其中的一个就可以选择使用这个声明
if...then...elseif statement - use this statement if you want to select one of many sets of lines to execute
如果你想要根据条件从多个代码行中选择执行其中的一个那么可以使用这个声明
select case statement - use this statement if you want to select one of many sets of lines to execute
多选一的时候可以使用
If....Then.....Else
You should use the If...Then...Else statement if you want to
使用If...Then...Else声明可以满足你的以下需求
execute some code if a condition is true
当条件为真时执行某串代码
select one of two blocks of code to execute
从两个块代码里选择其中的一个来执行
If you want to execute only one statement when a condition is true, you can write the code on one line:
当你只想在条件为真时只执行一条声明的话,你可以将代码写成一行:
if i=10 Then msgbox "Hello"
There is no ..else.. in this syntax. You just tell the code to perform one action if the condition is true (in this case if i=10).
在这句语法中没有使用到...else...这串代码只在条件为真(当i=10为真)的时候执行一次动作。
If you want to execute more than one statement when a condition is true, you must put each statement on separate lines and end the statement with the keyword "End If":
当你想要在条件为真时执行不止一条声明的话,你就必须将每条声明放到单独的一行中去,并在想要结束的时候加上关键字"End if"
if i=10 Then
msgbox "Hello"
i = i+1
end If
There is no ..else.. in this syntax either. You just tell the code to perform multiple actions if the condition is true.
在这句语法中同样没有用到...else...。你只是让代码在条件为真的情况下执行了多个动作。
If you want to execute a statement if a condition is true and execute another statement if the condition is not true, you must add the "Else" keyword:
如果你想要针对两种条件(真和假)分别执行不同代码串的话,你就必须得加上关键字"Else":
if i=10 then
msgbox "Hello"
else
msgbox "Goodbye"
end If
The first block of code will be executed if the condition is true, and the other block will be executed otherwise (if i is not equal to 10).
第一块代码将在条件为真的情况下执行(i等于10)而第二块则在不满足该条件的时候执行(也就是i不等于10)
If....Then.....Elseif
You can use the if...then...elseif statement if you want to select one of many blocks of code to execute:
当你想要从多个代码块中选择其中的一个根据不同的条件来执行的话,可以选择使用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
You can also use the SELECT statement if you want to select one of many blocks of code to execute:
你也可以使用SELECTE声明来达到同样的效果:
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
This is how it works: First we have a single expression (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each Case in the structure. If there is a match, the block of code associated with that Case is executed.
这串代码是这样运作的:首先我们得写一行表达式(大多数情况为变量)。变量将在下面每个条件里的case值一一对应起来,当变量值等于相应的值时就执行对应的代码。

