For/Next

From GCHSWiki
Jump to: navigation, search

When you want to repeat the statements in a loop a specific number of times, the For/Next loop is ideal. The For/Next loop uses the For and Next statements and a counter variable, called the loop index. The loop index is tested to determine the number of times the statements inside the loop will execute.

Dim intLoopIndex as Integer
Dim intMax as Integer

intMax = lsbSchools.Items.Count - 1

For intLoopIndex = 0 To intMax
    ' The statements inside of the loop are indented
    ' and referred to as the body of the loop.
Next intLoopIndex


intLoopIndex is used to count the number of times the loop is executed. intMax is the number of times the loop will execute. intMax is calculated by using the item.count function to determine the number of items in the list/combo box and then subtracting 1 (because computers always begin with 0 when counting.) intLoopIndex is set to 0 and increments each time it reads the Next intLoopIndex statement.