DIAdem Help

For...Next

  • Updated2024-09-12
  • 2 minute(s) read

For...Next

The For...Next counter loop repeats a group of statements until the loop counter reaches a specified value.

For Counter = Begin To End [Step StepValue]
  [Statements]
Next
CounterNumerical variable that DIAdem uses to count loops.
BeginStart value for Counter.
EndEnd value for Counter.
StepValueThe step width with which DIAdem changes the Counter each time DIAdem executes a loop. The standard value for StepValue is 1, if you do not specify the value.
StatementsOne or more statements between For and Next that DIAdem repeats a specified number of times.

Use For...Next loops if the number of repetitions of a statement group is specified. Use the Exit For statement to abort such loops.

If the value of the Counter argument is positive, and Counter<=End, DIAdem executes the statements defined inside the loop. If the value for Step is negative, the condition Counter>=End must be fulfilled.

Each time the loop executes, DIAdem adds StepValue to the Counter loop counter. DIAdem checks the condition and then executes the statements in the loop again or ends the loop and continues with the statement after Next.

You can nest For...Next loops. For every loop, use a distinct variable name as the Counter.

In the following example, the iLoop count variable automatically increments by one each time DIAdem executes the loop. DIAdem executes the loop three times. 

Dim iLoop
For iLoop = 2 To 4
  Call MsgBox("Number = " & iLoop)
Next

In the following example, the iLoop count variable automatically decreases by one each time DIAdem executes the loop. DIAdem executes the loop five times.

Dim iLoop
For iLoop = 10 To 2 Step -2
  Call MsgBox("Number = " & iLoop)
Next

Related Topics

Do While...Loop | Do...Loop Until | Exit | For...Next | For Each...Next | If...Then...Else | Select Case...Case...End Select | While...Wend | With