📄 vbs4.htm
字号:
<HTML>
<HEAD>
<TITLE>Using Conditional Statements</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso8859-1">
<META NAME="MS.LOCALE" CONTENT="EN-US">
<META NAME="PRODUCT" CONTENT="Visual Basic Scripting Edition">
<META NAME="TECHNOLOGY" CONTENT="SCRIPTING">
<META NAME="CATEGORY" CONTENT="Tutorial">
<META NAME="Description" CONTENT="Using VBScript Conditional Statements">
</HEAD>
<BODY BGCOLOR=FFFFFF LINK=#0033CC>
<!--TOOLBAR_START-->
<!--TOOLBAR_EXEMPT-->
<!--TOOLBAR_END-->
<FONT FACE="Verdana, Arial, Helvetica" SIZE=2>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%><TR VALIGN=TOP><TD WIDTH=360>
<FONT SIZE=1 COLOR=#660033>Microsoft® Visual Basic® Scripting Edition</FONT><BR>
<FONT SIZE=5 COLOR=#660033><B>Using Conditional Statements</B></FONT>
</TD>
<TD ALIGN=RIGHT>
<FONT SIZE=2> <A HREF="vbstutor.htm">VBScript Tutorial</A> <BR> <A HREF="vbs13.htm">Previous</A> | <A HREF="vbs10.htm">Next</A> <P></FONT>
</TD></TR>
</TABLE>
<P>
<HR NOSHADE SIZE=1>
<H5>Controlling Program Execution</H5>
<BLOCKQUOTE>
You can control the flow of your script with conditional statements and looping statements. Using conditional statements, you can write VBScript code that makes decisions and repeats actions. The following conditional statements are available in VBScript:
<UL>
<LI> <A HREF="vbs585.htm" ><B>If...Then...Else</B></A> statement
<LI> <A HREF="vbs598.htm"><b>Select Case</B></A> statement
</UL>
</BLOCKQUOTE>
<H5>Making Decisions Using If...Then...Else</H5>
<BLOCKQUOTE>
The <B>If...Then...Else</B> statement is used to evaluate whether a condition is <B>True</B> or <B>False</B> and, depending on the result, to specify one or more statements to run. Usually
the condition is an expression that uses a comparison operator to compare one value or variable with another. For information about comparison operators, see <A HREF="vbs224.htm" >Comparison Operators</A>. <B>If...Then...Else</B> statements can be nested to as many levels as you need.<P>
<H5>Running Statements if a Condition is True</H5>
To run only one statement when a condition is <B>True</B>, use the single-line syntax for the <B>If...Then...Else</B> statement. The following example shows the single-line syntax. Notice that this example omits the <B>Else</B> keyword.
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Sub FixDate()
Dim myDate
myDate = #2/13/95#
If myDate < Now Then myDate = Now
End Sub
</FONT></PRE></BLOCKQUOTE>
To run more than one line of code, you must use the multiple-line (or block) syntax. This syntax includes the <B>End If</B> statement, as shown in the following example:
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Sub AlertUser(value)
If value = 0 Then
AlertLabel.ForeColor = vbRed
AlertLabel.Font.Bold = True
AlertLabel.Font.Italic = True
End If
End Sub
</FONT></PRE></BLOCKQUOTE>
<H5>Running Certain Statements if a Condition is True and Running Others if a Condition is False </H5>
You can use an <B>If...Then...Else</B> statement to define two blocks of executable statements: one block to run if the condition is <B>True</B>, the other block to run if the condition is <B>False</B>.
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Sub AlertUser(value)
If value = 0 Then
AlertLabel.ForeColor = vbRed
AlertLabel.Font.Bold = True
AlertLabel.Font.Italic = True
Else
AlertLabel.Forecolor = vbBlack
AlertLabel.Font.Bold = False
AlertLabel.Font.Italic = False
End If
End Sub
</FONT></PRE></BLOCKQUOTE>
<H5>Deciding Between Several Alternatives</H5>
A variation on the <b>If...Then...Else</b> statement allows you to choose from several alternatives. Adding <b>ElseIf</b> clauses expands the functionality of the <b>If...Then...Else</b> statement so you can control program flow based on different possibilities. For example:
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Sub ReportValue(value)
If value = 0 Then
MsgBox value
ElseIf value = 1 Then
MsgBox value
ElseIf value = 2 then
Msgbox value
Else
Msgbox "Value out of range!"
End If
</FONT></PRE></BLOCKQUOTE>
You can add as many <b>ElseIf</b> clauses as you need to provide alternative choices. Extensive use of the <b>ElseIf</b> clauses often becomes cumbersome. A better way to choose between several alternatives is the <B>Select Case</B> statement.
</BLOCKQUOTE>
<H5>Making Decisions with Select Case</H5>
<BLOCKQUOTE>
The <b>Select Case</b> structure provides an alternative to <b>If...Then...ElseIf</b> for selectively executing one block of statements from among multiple blocks of statements. A <b>Select Case</b> statement provides capability similar to the <b>If...Then...Else statement</b>, but it makes code more efficient and readable.<p>
A <b>Select Case</b> structure works with a single test expression that is evaluated once, at the top of the structure. The result of the expression is then compared with the values for each <b>Case</b> in the structure. If there is a match, the block of statements associated with that <b>Case</b> is executed:<p>
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Select Case Document.Form1.CardType.Options(SelectedIndex).Text
Case "MasterCard"
DisplayMCLogo
ValidateMCAccount
Case "Visa"
DisplayVisaLogo
ValidateVisaAccount
Case "American Express"
DisplayAMEXCOLogo
ValidateAMEXCOAccount
Case Else
DisplayUnknownImage
PromptAgain
End Select
</FONT></PRE></BLOCKQUOTE>
Notice that the <b>Select Case</b> structure evaluates an expression once at the top of the structure. In contrast, the <b>If...Then...ElseIf</b> structure can evaluate a different expression for each <b>ElseIf</b> statement. You can replace an <b>If...Then...ElseIf </b>structure with a <b>Select Case</b> structure only if each <b>ElseIf</b> statement evaluates the same expression.
</BLOCKQUOTE>
<hr noshade size=1>
<p align=center><em><a href="../../common/colegal.htm">© 1997 by Microsoft Corporation. All rights reserved.</a></em></p>
</FONT>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -