ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: VBScript :: VBScript Ìõ¼þÓï¾ä

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

VBScript
VBScript ½éÉÜ
ÈçºÎʹÓà VBScrip..
VBScript ·ÅÖÃ
VBScript ±äÁ¿
VBScript ³ÌÐò
VBScript Ìõ¼þÓï¾..
VBScript Ñ­»·ÉùÃ..
VBScript ÕªÒª
VBScript ʵÀý
VBScript º¯Êý
VBScript ¹Ø¼ü×Ö

VBScript Ìõ¼þÓï¾ä


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ä¯ÀÀ:2086 :: ::

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
    µ±Ìõ¼þÎªÕæÊ±Ö´ÐÐij´®´úÂë
  • 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ÖµÒ»Ò»¶ÔÓ¦ÆðÀ´£¬µ±±äÁ¿ÖµµÈÓÚÏàÓ¦µÄֵʱ¾ÍÖ´ÐжÔÓ¦µÄ´úÂë¡£

ÆÀÂÛ (1) 1 All