Counting and Looping

The Do...Loop Statements

 

Loops are used to repeat an action. There are various variations of the Do loops.

The formula of the Do While loop is:

Do While Condition
    Statement(s)
Loop

This expression will execute the Statement or statements AS LONG AS the Condition is true, as many times as the Condition will be visited and found true. The program will first test the Condition. If the Condition is true, the program will execute the Statement or Statements and go back to the Do While statement and test the condition again. If the Condition is false, the program will skip the Do While statement and not execute any.

Since the Do While statement tests the Condition first before executing the Statement, sometimes you will want the program to execute the Statement first, then go back and test the Condition. VBScript offers a reverse to the formula, which is:

Do
  Statement(s)
Loop While Condition

In this case, VBScript will execute the Statement or Statements first, then it will test the Condition. If the Condition is true, the program will execute the Statement again. The program will continue this examination-execution as long as the Condition is true. The big difference here is that even if the Condition is false, the program will have executed the Condition at least once.

An alternative to the Do While loop is the Do Until loop. Its formula is:

Do Until Condition
    Statement(s)
Loop

This loop will first examine the Condition, instead of examining whether the Condition is true, it will test whether the Condition is false.

The other side of the Do Until loop will execute the Statement first, then it would examine the Condition. The formula is:

Do
    Statement(s)
Loop Until Condition

 

Decision Makers: The For...Next Statement

 

If you don't know how many times a statement needs to be executed, you can use one of the Do loops. But whenever you want to control how many times a statement should be executed, the For...Next loop offers a better alternative. The formula is:

For Counter = Start To End
  Statement(s)
Next

Used for counting, the For loop begins counting at the Start point. Then it examines whether the current value (after starting to count) is greater than End; if that's the case, the program exits the loop. It then executes the Statement or Statements. Next, it increments the value of Counter by 1 and examines the condition again. This process goes on until Counter = End.

The formula above will increment the counting by 1 at the end of each statement. If you want to control how the incrementing processes, you can set your own, using the Step option. Here is the formula:

For Counter = Start To End Step Increment
  Statement(s)
Next Counter

You can set the incrementing value to your choice. If the value of Increment is positive, the Counter will be added its value. This means you can give it a negative value, in which case the Counter will be subtracted the set value.

 

Decision Makers: The For Each...Next Statement

 

Since the For Next loop is used to execute a group of statements based on the current result of the loop counting from Start to End, an alternative is to state various steps in the loop and execute a group of statements for each one of the elements in the group. This is mostly used when dealing with a collection of items.

The formula is:

For Each Element In Group
    Statement(s)
Next Element

The loop will execute the Statement or Statement(s) for each Element in the Group.

 

Previous Copyright © 2002-2010 FunctionX, Inc.