当前位置: 首页 > 网络学院 > 客户端脚本教程 > VBScript > VBScript 条件语句
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 声明
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中我们有三种声明可以选择:
You should use the If...Then...Else statement if you want to
使用If...Then...Else声明可以满足你的以下需求
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 |
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 |
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)
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 |
You can also use the SELECT statement if you want to select one of many blocks of code to execute:
你也可以使用SELECTE声明来达到同样的效果:
select case payment |
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值一一对应起来,当变量值等于相应的值时就执行对应的代码。