For Loop in VBScripting

by seetaram on August 28, 2011

In vbscript to execute set of statements FOR LOOP can be utilized.  There are two variants of this are present.  Let us start with the first one.

1. For . . . Next

Syntax:

For {integer variable}={initial value} to {Max value for the variable} Step {incremental value e.g. –2, 1, 2, etc}

. . . . . . .

Set of Statements

. . . . . . .

Next

Example:

mult=5
multtable="Multiplication Table for "&mult
multtable=multtable&vbnewline
    For i=1 to 10
        ans=mult*i
            multtable=multtable&mult&"X"&i&"="&ans&vbnewline
    Next

msgbox multtable

 

This variant of For Loop can be used where we know the increment or decrement value and also the max or minimum value until the code inside the loop can be executed.

Above is a multiplication table example in which I have used four variables mult, multtable, ans, and i.  Along with this I have used one more vbscript constant “vbnewline”.  This constant is used to move the control to the next line while displaying the output.

To understand the workflow it is better to use a debugger / editor such as QTP or VBSEdit.  Apply a toggle breakpoint in the (Right click-> Toggle Breakpoint OR insert breakpoint) first line of the script.  Start the debugging with F11 / F10, to Step into the code F11 should be used and F10 to Step Over.

Add the variables to watch list and see how the variables value gets changed in every step.

Iteration 1:

Initial value of multtable=”Multiplication Table for 5” and it is concatenated with value of mult (i.e. 5)

Inside For Loop:

i=1

ans=mult*i = 5 * 1 = 5

multtable=initial value of multtable is concatenated with & {5} & {“X” – to display multiplication symbol} & {i =1} &{ans=5}& {vbnewline constant – vbnewline}

Iteration 2:

Now the value of i is incremented by 1 that is the default value.  This step value can be provided to the For Loop as “Step {value}

i=2

. . . Continue as above . . .

Try to understand how the for loop works.

Now make some changes to the above code snippet as below:

mult=5
multtable="Multiplication Table for "&mult
multtable=multtable&vbnewline
    For i=10 to 1 step -2
        ans=mult*i
            multtable=multtable&mult&"X"&i&"="&ans&vbnewline
    Next

msgbox multtable

The only change I made is added a step {incrementing / decrementing value} to the loop now it decreases the value of i by –2.  Both the examples clearly explain how the For Loop can be used in vbscripting with incrementing as well as decrementing values.

Exit For

After executing some statements inside the For Loop if there is a need to get out of the loop depending on a condition then we could go ahead and use Exit For.

For i=0 to 10
    msgbox "I am in loop "&i
   If i=5 Then
       Exit For
   End If
Next

The script is pretty simple.  In the iteration where “i” takes the value of “5” the condition becomes true and the Exit For gets executed, loop breaks, and control comes out of the loop.

Leave a Comment

Previous post:

Next post:

Do you want to get updates on the recent articles written? Please subscribe to RSS feed or Email