w3pop.com :: ÍøÂçѧԺ :: VBScript :: VBScript Ñ»·ÉùÃ÷
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À´Ö´Ðмòµ¥µÄÑ»·
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ÖÐÓÐËÄÖÖÑ»·ÉùÃ÷£º
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 |
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µÝÔöÒ»´Î
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 |
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 |
You can exit a For...Next statement with the Exit For keyword.
Äã¿ÉÒÔʹÓùؼü×ÖExit ForÀ´À뿪For....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) |
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ÉùÃ÷¡£µ±Ìõ¼þÎªÕæ»òÊÇÖ±µ½ÎªÕæÖ®Ç°´úÂë»áÒ»Ö±·´¸´Ö´ÐС£
You use the While keyword to check a condition in a Do...Loop statement.
ÔÚDo...LoopÉùÃ÷ÖÐʹÓÃWhile¹Ø¼ü×ÖÀ´¼ì²éÌõ¼þ
Do While i>10 |
If i equals 9, the code inside the loop above will never be executed.
µ±IµÈÓÚ9µÄʱºòÉÏÃæÑ»·ÖеĴúÂë¾Í²»»á±»Ö´ÐÐ
Do |
The code inside this loop will be executed at least one time, even if i is less than 10.
ÉÏÃæµÄ´úÂëÖÁÉÙΪִÐÐÒ»´Î£¬ÄÄÅÂiСÓÚ10
You use the Until keyword to check a condition in a Do...Loop statement.
ÔÚDo...LoopÉùÃ÷ÖÐʹÓÃUntil¹Ø¼üÀ´¼ì²éÌõ¼þ
Do Until i=10 |
If i equals 10, the code inside the loop will never be executed.
Èç¹ûiµÈÓÚ10ÄÇôѻ·ÖеĴúÂë²»»á±»Ö´ÐÐ
Do |
The code inside this loop will be executed at least one time, even if i is equal to 10.
ÉÏÃæÕâ¶Î´úÂëÑ»·ÀïµÄ´úÂëÖÁÉÙ»áÖ´ÐÐÒÀ´Î£¬ÄÄÅÂiµÈÓÚ10
You can exit a Do...Loop statement with the Exit Do keyword.
ʹÓÃExit Do¹Ø¼ü×־ͿÉÒÔÀ뿪Do...LoopÉùÃ÷
Do Until i=10 |
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