⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vbs585.htm

📁 VBScript 是一种脚本语言
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><HTML>
<HEAD><TITLE>If...Then...Else Statement</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="Language Reference">

<META NAME="Keywords" CONTENT="If statement,Is operator,Else,Then,ElseIf,End If,TypeOf,program control,control structures,conditional execution,execution conditional,selecting from choices,choices selecting from,execution,choices"><META NAME="Description" CONTENT="If...Then...Else Statement"></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&#174; Visual Basic&#174; Scripting Edition</FONT><BR>
<FONT SIZE=5 COLOR=#660033><B>If...Then...Else Statement</B></FONT>

</TD>
<TD ALIGN=RIGHT>
<FONT SIZE=2>&nbsp;<A HREF="vbstoc.htm">Language&nbsp;Reference</A>&nbsp;<BR>
<A HREF="vbs16.htm">Version&nbsp;1</A>&nbsp;<P></FONT>
</TD></TR>
</TABLE> 
<FONT SIZE=2><p>
<HR noshade SIZE=1>

<H5>Description</H5>
<BLOCKQUOTE>Conditionally executes a group of statements, depending on the value of an expression.</BLOCKQUOTE>

<H5>Syntax</H5>

<BLOCKQUOTE><b>If</b> <i>condition </i><b>Then</b> <i>statements </i>[<b>Else</b> <i>elsestatements </i>]
<P>Or, you can use the block form syntax:
<P><b>If</b> <i>condition </i><b>Then</b><BR>
&nbsp;&nbsp;&nbsp;&nbsp;[<i>statements</i>]<BR>
[<b>ElseIf</b> <i>condition-n</i> <b>Then</b><BR>
&nbsp;&nbsp;&nbsp;&nbsp;[<i>elseifstatements</i>]] <b>. . .</b><BR>
[<b>Else</b><BR>&nbsp;&nbsp;&nbsp;&nbsp;[<i>elsestatements</i>]]<BR>
<b>End If</b>
<P>The <b>If...Then...Else</b> statement syntax has these parts:<P>

<TABLE WIDTH=87% BORDER=1 CELLPADDING=5 CELLSPACING=0>
<TR VALIGN=TOP BGCOLOR="#DDDDDD">
<TD><FONT SIZE=2><b>Part</b></FONT></TD>
<TD><FONT SIZE=2><b>Description</b></FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>condition</i></FONT></TD>
<TD><FONT SIZE=2>One or more of the following two types of expressions:<P>A 
<A HREF="vbs0.htm#defNumericExpression">numeric</A> or 
<A HREF="vbs0.htm#defStringExpression">string expression</A> that evaluates to <b>True</b> or <b>False</b>. If <i>condition</i> is 
<A HREF="vbs0.htm#defNull"><B>Null</B></A>, <i>condition</i> is treated as <b>False</b>.<P>
An expression of the form <b>TypeOf</b> <i>objectname</i> <b>Is</b> <i>objecttype</i>. The <i>objectname</i> is any object reference and <i>objecttype</i> is any valid object type. The expression is <b>True</b> if <i>objectname</i> is of the object type specified by <i>objecttype</i>; otherwise it is <b>False</b>.</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>statements</i></FONT></TD>
<TD><FONT SIZE=2>One or more statements separated by colons; executed if <i>condition</i> is <b>True</b>.</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>condition-n</i></FONT></TD>
<TD><FONT SIZE=2>Same as <i>condition</i>.</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>elseifstatements</i></FONT></TD>
<TD><FONT SIZE=2>One or more statements executed if the associated <i>condition-n</i> is <b>True</b>.</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2><i>elsestatements</i></FONT></TD>
<TD><FONT SIZE=2>One or more statements executed if no previous <i>condition</i> or <i>condition-n</i> expression is <b>True</b>.</FONT></TD></TR></TABLE></BLOCKQUOTE>

<H5>Remarks</H5>

<BLOCKQUOTE>You can use the single-line form (first syntax) for short, simple tests. However, the block form (second syntax) provides more structure and flexibility than the single-line form and is usually easier to read, maintain, and debug.
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0 WIDTH=87%><TR><TD COLSPAN=2 VALIGN=BOTTOM><hr noshade size=1></TD></TR><TR>
<TD VALIGN=TOP><FONT SIZE=2><b>Note</b>&nbsp;&nbsp;With the single-line syntax, it is possible to have multiple statements executed as the result of an <b>If...Then</b> decision, but they must all be on the same line and separated by colons, as in the following statement:
<BLOCKQUOTE>
<PRE><FONT FACE="Courier New" SIZE=3><FONT COLOR= "#FF0000">If</FONT> A &gt; 10 <FONT COLOR= "#FF0000">Then</FONT> A = A + 1 : B = B + A : C = C + B
</FONT></PRE>
</BLOCKQUOTE>
</FONT></TD></TR>
<TR><TD COLSPAN=2 VALIGN=TOP><hr noshade size=1></TD></TR></TABLE>
When executing a block <b>If</b> (second syntax), <i>condition </i>is tested. If <i>condition</i> is <b>True</b>, the statements following <b>Then</b> are executed. If <i>condition</i> is <b>False</b>, each <b>ElseIf</b> (if any) is evaluated in turn. When a <b>True</b> condition is found, the statements following the associated <b>Then</b> are executed. If none of the <b>ElseIf</b> statements are <b>True</b> (or there are no <b>ElseIf</b> clauses), the statements following <b>Else</b> are executed.  After executing the statements following <b>Then</b> or <b>Else</b>, execution continues with the statement following <b>End If</b>.<P>

The <b>Else</b> and <b>ElseIf</b> clauses are both optional. You can have as many <b>ElseIf</b> statements as you want in a block <b>If</b>, but none can appear after the <b>Else</b> clause. Block <b>If</b> statements can be nested; that is, contained within one another.<P>

What follows the <b>Then</b> keyword is examined to determine whether or not a statement is a block <b>If</b>. If anything other than a comment appears after <b>Then</b> on the same line, the statement is treated as a single-line <b>If</b> statement.<P>
A block <b>If</b> statement must be the first statement on a line. The block <b>If</b> must end with an <b>End If</b> statement.</BLOCKQUOTE>

<hr noshade size=1>
<p align=center><em><a href="../../common/colegal.htm">&copy; 1997 by Microsoft Corporation. All rights reserved.</a></em></p> 
</FONT> </FONT></BODY></HTML>




















































































⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -