ÍøÂçѧԺ 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 ä¯ÀÀ:2203 :: ::

Examples
ʵÀý

For...next loop
This example demonstrates how to make a simple For....Next loop.
ÔõÑùʹÓÃFor...NextÀ´Ö´Ðмòµ¥µÄÑ­»·

Looping through headers
This example demonstrates how you can loop through the 6 headers in html.
ÔõÑùÔÚhtmlÖн«6ÖÖ¹æ¸ñµÄ±êÌâÑ­»·ÏÔʾһ±é

For...each loop
This example demonstrates how to make a simple For.....Each loop.
ÔõÑùʹÓÃFor...EachÀ´Ö´Ðмòµ¥µÄÑ­»·

Do...While loop
This example demonstrates how to make a simple Do...While loop.
ʹÓÃDo...WhileÀ´Ö´Ðмòµ¥µÄÑ­»·


Looping Statements
Ñ­»·ÉùÃ÷

Very often when you write code, you want to allow the same block of code to run a number of times. You can use looping statements in your code to do this.
µ±ÄãÏëÈÃÏàͬµÄ´úÂëÖØ¸´Ö´Ðжà´ÎµÄ»°Äã¿ÉÒÔʹÓÃÑ­»·ÉùÃ÷¡£

In VBScript we have four looping statements:
ÔÚVBScriptÖÐÓÐËÄÖÖÑ­»·ÉùÃ÷£º

  • For...Next statement - runs statements a specified number of times.
    ÈÃÉùÃ÷ÔËÐÐÖ¸¶¨´ÎÊý
  • For Each...Next statement - runs statements for each item in a collection or each element of an array
    Õë¶ÔijÊý×éÄÚµÄÿ¸öÔªËØ·´¸´ÔËÐÐÉùÃ÷
  • Do...Loop statement - loops while or until a condition is true
    ÔÚÌõ¼þÎªÕæÖ®Ç°Ñ­»·ÔËÐÐ
  • While...Wend statement - Do not use it - use the Do...Loop statement instead
    ±ðÓÃÕâ¸öÉùÃ÷£¬ËüÍêÈ«¿ÉÒÔÓÃDo...LoopÉùÃ÷À´È¡´ú

For...Next Ñ­»·

You can use a For...Next statement to run a block of code, when you know how many repetitions you want.
µ±ÄãÈ·¶¨×Ô¼ºÒªÖظ´ÔËÐжàÉٴδúÂëµÄʱºò¿ÉÒÔʹÓÃFor...NextÉùÃ÷¡£

You can use a counter variable that increases or decreases with each repetition of the loop, like this:
Äã¿ÉÒÔͨ¹ýÿѭ»·Ò»´Î±ãµÝÔö»òµÝ¼õijһ±äÁ¿µÄÊýÖµÀ´´ïµ½ÕâÖÖЧ¹û£¬ÏñÕâÑù£º

For i=1 to 10
some code
Next

The For statement specifies the counter variable (i) and its start and end values. The Next statement increases the counter variable (i) by one.
ForÉùÃ÷ÒѾ­Ö¸¶¨±äÁ¿iΪ¼ÆÊýÓõıäÁ¿²¢ÉèÖÃÁËËüµÄ³õʼֵºÍ×îÖÕÑ­»·½áÊøÊ±µÄÖµ¡£NextÉùÃ÷¿ÉÒÔÈüÆÊý±äÁ¿iµÝÔöÒ»´Î

¹Ø¼ü×Ö Step

Using the Step keyword, you can increase or decrease the counter variable by the value you specify.
µ±Ê¹Óõ½Step¹Ø¼ü×ÖÄã¾Í¿ÉÒÔÖ¸¶¨¼ÆÊý±äÁ¿Ã¿´Î¸Ä±äÊǵÝÔö»¹Êǵݼõ£¬²¢Ö¸¶¨Ëüÿ´ÎÔö¼õµÄ´óС¡£

In the example below, the counter variable (i) is increased by two each time the loop repeats.
ÏÂÃæµÄʵÀýÖУ¬¼ÆÊý±äÁ¿iÊÇÒÔÿ´ÎÑ­»·Ôö¼Ó2µÄËٶȽøÐеÝÔöµÄ¡£

For i=2 To 10 Step 2
some code
Next

To decrease the counter variable, you must use a negative Step value. You must specify an end value that is less than the start value.
Ҫʹ¼ÆÊý±äÁ¿²úÉúµÝ¼õµÄЧ¹û¾ÍµÃÈÃStepÖµ±äΪ¸ºÊý¡£µ«Í¬Ê±ÄãµÃ±£Ö¤Ö¸¶¨µÄ³õʼֵµÃ±È×îºóµÄÖµÒª´ó¡£

In the example below, the counter variable (i) is decreased by two each time the loop repeats.
ÏÂÃæµÄ¾ÙÀýÖмÆÊý±äÁ¿iÿ´ÎÑ­»·ºó¾Í»áµÝ¼õ2¡£

For i=10 To 2 Step -2
some code
Next

À뿪 For...Next

You can exit a For...Next statement with the Exit For keyword.
Äã¿ÉÒÔʹÓùؼü×ÖExit ForÀ´À뿪For....NextÉùÃ÷¡£


For Each...Next Ñ­»·

A For Each...Next loop repeats a block of code for each item in a collection, or for each element of an array.
µ±Òª¶ÔÒ»¸ö¼¯ºÏÖеÄÿ¸öÄÚÈÝ»òÊÇÊý×éÖеÄÿ¸öÔªËØ½øÐÐÑ­»·µÄ»°£¬¿ÉÒÔʹÓÃFor Each...NextÑ­»·

dim cars(2)
cars(0)="Volvo"

cars(1)="Saab"
cars(2)="BMW"

For Each x in cars
document.write(x & "<br />")
Next


Do...Loop

You can use Do...Loop statements to run a block of code when you do not know how many repetitions you want. The block of code is repeated while a condition is true or until a condition becomes true.
µ±Äã²»ÖªµÀÐèÒªÖØ¸´Ö´ÐжàÉٴδúÂëµÄʱºòÄã¿ÉÒÔʹÓÃDo....LoopÉùÃ÷¡£µ±Ìõ¼þÎªÕæ»òÊÇÖ±µ½ÎªÕæÖ®Ç°´úÂë»áÒ»Ö±·´¸´Ö´ÐС£

Repeating Code While a Condition is True
µ±Ìõ¼þÎªÕæÊ±·´¸´Ö´ÐдúÂë

You use the While keyword to check a condition in a Do...Loop statement.
ÔÚDo...LoopÉùÃ÷ÖÐʹÓÃWhile¹Ø¼ü×ÖÀ´¼ì²éÌõ¼þ

Do While i>10
some code
Loop

If i equals 9, the code inside the loop above will never be executed.
µ±IµÈÓÚ9µÄʱºòÉÏÃæÑ­»·ÖеĴúÂë¾Í²»»á±»Ö´ÐÐ

Do
some code
Loop While i>10

The code inside this loop will be executed at least one time, even if i is less than 10.
ÉÏÃæµÄ´úÂëÖÁÉÙΪִÐÐÒ»´Î£¬ÄÄÅÂiСÓÚ10

Repeating Code Until a Condition Becomes True
´úÂëÒ»Ö±ÖØ¸´£¬Ö±µ½Ìõ¼þÎªÕæ

You use the Until keyword to check a condition in a Do...Loop statement.
ÔÚDo...LoopÉùÃ÷ÖÐʹÓÃUntil¹Ø¼üÀ´¼ì²éÌõ¼þ

Do Until i=10
some code
Loop

If i equals 10, the code inside the loop will never be executed.
Èç¹ûiµÈÓÚ10ÄÇôѭ»·ÖеĴúÂë²»»á±»Ö´ÐÐ

Do
some code
Loop Until i=10

The code inside this loop will be executed at least one time, even if i is equal to 10.
ÉÏÃæÕâ¶Î´úÂëÑ­»·ÀïµÄ´úÂëÖÁÉÙ»áÖ´ÐÐÒÀ´Î£¬ÄÄÅÂiµÈÓÚ10

À뿪 Do...Loop

You can exit a Do...Loop statement with the Exit Do keyword.
ʹÓÃExit Do¹Ø¼ü×־ͿÉÒÔÀ뿪Do...LoopÉùÃ÷

Do Until i=10
i=i-1
If i<10 Then Exit Do
Loop

The code inside this loop will be executed as long as i is different from 10, and as long as i is greater than 10.
ÉÏÃæµÄÕâ´®´úÂë½öµ±i´óÓÚ10µÄʱºò²Å»áÖ´ÐÐÑ­»·¡£

ÆÀÂÛ (0) All