Repeats a group of statements a specified number of times.

 

First the expression start and end are evaluated.  When the NEXT statement is encountered, stepSize amount is added to the variable defined as the counter.  At this point, if counter is less than or equal to end value, the statements in the loop execute again.  If counter is greater than end, then the loop is exited and execution continues with the statement following the NEXT statement.

 

The expressions start, end and stepSize can be any expression or variable of any type.  However, unlike with the WHILE statement, if end is an expression, the FOR statement evaluates this expression only once at the start of the loop and stores this value for subsequent comparisons.  So you should not write code that relies on the end expression being evaluated every time through the loop.

 

Syntax:

FOR counter = start TO end [STEP stepSize ]
    [statements ]
NEXT

 

Parameter:

Description:

counter

Variable used as a loop counter.  This variable must be declared as an integer before using.  

start

Initial value of counter (can be a complex expression)

end

Final value of counter (can be a complex expression)

[STEP stepSize ]

Integer amount counter is changed each time through the loop. If not specified, step defaults to one, and the step value will increment.  Step value can be a negative that decrements to the value contained in the variable: end.

statements

One or more statements between FOR  and NEXT  that are executed the specified number of times.

 

Example:

'  -------------------------------------------------
VARIABLES: index Type: Integer
PRINT "Example 1:"
 
'  This For Next loop will increment index values
For index = 1 TO 5
  PRINT "index value = ", index
Next
 
'  -------------------------------------------------
VARIABLES: index Type: Integer
PRINT "Example 2:"
 
'  The For Next Loop decrements index values
For index = -1 TO -5 STEP -1
  PRINT "index value = ", index
Next
 
'  -------------------------------------------------
VARIABLES: index Type: Integer
PRINT "Example 3:"
 
'  The For Next Loop Increments index values
For index = -1 TO -5 STEP 1
  PRINT "index value = ", index
Next
 
'  -------------------------------------------------
VARIABLES: monthIndex, dayIndex Type: Integer
PRINT "Example 4:"
'  You can nest For...Next  loops by placing
'  one For...Next  Loop within another.
'  The following illustrates a nested For statement:
For monthIndex = 1 TO 2 STEP 1
  '  Display Month Name
  PRINT "Month: ", MonthName( monthIndex )
  '  Display the first 7 days of a Month
  For dayIndex = 1 TO 7 STEP 1
    PRINT " Day # = ", dayIndex, " Day Name = ", DayOfWeekName( dayIndex - 1 )
  Next
Next
'  -------------------------------------------------

Returns:

Example 1:

  index value =  1

  index value =  2

  index value =  3

  index value =  4

  index value =  5

 

Example 2:

  index value =  -1

  index value =  -2

  index value =  -3

  index value =  -4

  index value =  -5

 

Example 3:

  Month:  Jan

     Day # =  1  Day Name =  SUNDAY

     Day # =  2  Day Name =  MONDAY

     Day # =  3  Day Name =  TUESDAY

     Day # =  4  Day Name =  WEDNESDAY

     Day # =  5  Day Name =  THURSDAY

     Day # =  6  Day Name =  FRIDAY

     Day # =  7  Day Name =  SATURDAY

 

  Month:  Feb

     Day # =  1  Day Name =  SUNDAY

     Day # =  2  Day Name =  MONDAY

     Day # =  3  Day Name =  TUESDAY

     Day # =  4  Day Name =  WEDNESDAY

     Day # =  5  Day Name =  THURSDAY

     Day # =  6  Day Name =  FRIDAY

     Day # =  7  Day Name =  SATURDAY

 

Links:

 

See Also:

VARIABLES

 


Edit Time: 9/25/2017 09:09:58 AM


Topic ID#: 346

 

Created with Help & Manual 7 and styled with Premium Pack Version 2.80 © by EC Software