For

Controls the looping of a block of code based on an incrementing index.

Syntax:

For var-name = initial-value to last-value [ by increment]

Parameters:

(s) var-name a variable name to be used for the loop index.

(f) initial-value an expression used to set the initial value of the loop index.

(f) last-value an expression that defines last value to be used in the loop. When the initial value is incremented past the last value, the loop terminates.

(f) increment an expression that defines the amount to increment the loop index on each pass through the loop. The default is one. The increment may be negative.

 

Use the For statement to execute a block of code a fixed number of times. When the For statement is executed, it initializes the specified variable var-name to the initial-value This variable is called the loop index. It then tests the loop index with the last value. If the increment is positive and the loop index is greater than the last value, or if the increment is negative and the loop index is less than the last value, then the loop terminates and control is passed to the statement after the Next statement.

 

Otherwise the statements below the For are executed until the Next statement is reached. When the Next statement is reached, control returns to the For statement so that the loop index may be incremented and the test for last value repeated.

Example:


; Compute sum of numbers between 1 and selected number
a=AskLine("Sums", "Please enter a number", 5, 0)
f=0
For j = 1 To a
   f = f + j
Next
Message("Sum [ 1 to %a% ] is", f)
;Compute factorials
a=AskLine("Factorials", "Please enter a number", 5, 0)
f=1
For j = a To 2 By -1
   f=f*j
Next
Message("%a% Factorial is", f)
See Also:

Break, Continue, If, Select, Switch, While