Friday, May 22, 2009

For....Next Loop In VB.


For...Next Loop.


Use the For...Next statement to run a block of code a specified number of times.

The For statement specifies the counter variable (i), and its start and end values. The Next statement increases the counter variable (i) by one.

Example.


<html>
<body>

<script type="text/vbscript">
for i = 0 to 5
document.write("The number is " & i & "<br />")
next
</script>

</body>
</html>

The Step Keyword.


With the Step keyword, you can increase or decrease the counter variable by the value you specify.

In the example below, the counter variable (i) is INCREASED by two, each time the loop repeats.

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.

In the example below, the counter variable (i) is DECREASED by two, each time the loop repeats.

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

Exit a For...Next.


You can exit a For...Next statement with the Exit For keyword.

No response to “For....Next Loop In VB.”

Post a Comment