Show/Hide Toolbars

XSharp

Purpose

Execute a block of statements a specified number of times.

Syntax

FOR [<idCounter> := <nStart> | VAR <idCounter> := <nStart> | LOCAL <idCounter> := <nStart> AS <idType> ] [TO | UPTO | DOWNTO] <nEnd> [STEP <nDelta>]
  <Statements>...
  [EXIT]
  <Statements>...
  [LOOP]
NEXT

Note

In the FoxPro and Xbase++ dialect ENDFOR is allowed as alternative for NEXT

Arguments

<idCounter>The name of the loop control or counter variable.  IF a LOCAL or VAR clause is included then the local is created for the duration of the loop. With the VAR clause the datatype is inferred from the usage.

 

AS <idType>Specifies the data type.  If omitted, then depending on the compiler options the type will be either USUAL or determined by the compiler.

 

<nStart>The initial value assigned to <idCounter>.  If the loop is counting up, <nStart> must be less than <nEnd>.  If the loop is counting down, <nStart> must be greater than <nEnd>.

 

TO <nEnd>The final value of <idCounter>.  The TO clause can be used for counting up or down, depending on whether the STEP clause gives a positive or negative value for <nDelta>.  Note, however, that your code will be more efficient if you avoid the TO clause and specify UPTO or DOWNTO instead.

 

UPTO <nEnd>The final value of <idCounter>.  The UPTO clause is used for counting up.

 

DOWNTO <nEnd>The final value of <idCounter>.  The DOWNTO clause is used for counting down.

 

STEP <nDelta>The amount <idCounter> is changed for each iteration of the loop.  If used with the TO clause, <nDelta> can be either positive or negative.  With UPTO and DOWNTO, <nDelta> should be positive.  If the STEP clause is not specified, <idCounter> is incremented (or decremented in the case of DOWNTO) by one for each iteration of the loop.

 

EXITUnconditionally branches control from within a FOR, FOREACH , REPEAT or DO WHILE statement to the statement immediately following the corresponding ENDDO or NEXT statement.

 

LOOPBranches control to the most recently executed FOR, FOREACH , REPEAT or DO WHILE statement.

Description

The control structure loops from the initial value of <idCounter> to the boundary specified by <nEnd>, moving through the range of values of the control variable for an increment specified by <nDelta>.  All expressions in the FOR statement are reevaluated for each iteration of the loop.  The <nStart> and <nEnd> values, therefore, can be changed as the control structure operates.

 

A FOR loop operates until <idCounter> is greater than or less than <nEnd> (depending on whether you are counting up or down) or an EXIT statement is encountered.  Control then branches to the statement following the corresponding NEXT statement.  If a LOOP statement is encountered, control branches back to the current FOR statement.

Control structures can be nested to any depth.  The only requirement is that each control structure be properly nested.

 

Tip: Although FOR loops are useful for traversing arrays (as demonstrated in the examples below), your code will be more efficient if there is a corresponding array function designed to do what you want.

Examples

This example traverses an array in ascending order:

 

nLenArray := ALen(aArray)
FOR i := 1 UPTO nLenArray
 <Statements>...
NEXT

 

To traverse an array in descending order:

 

nLenArray := ALen(aArray)
FOR i := nLenArray DOWNTO 1
 <Statements>...
NEXT

See Also

AEval(), BEGIN SEQUENCE, DO CASE, DO WHILE, IF, EXIT, LOOP