w3pop.com :: ÍøÂçѧԺ :: 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ÖµÒ»Ò»¶ÔÓ¦ÆðÀ´£¬µ±±äÁ¿ÖµµÈÓÚÏàÓ¦µÄֵʱ¾ÍÖ´ÐжÔÓ¦µÄ´úÂë¡£