for
Username:  
Password:
  > Home
> User Guide
v Reference
    > Introduction
    > CROFT
    > BEE Variables
    > BEE Syntax
    v BEE Commands
       > Variable Operations
       > Conditional
       v Loop
          o foreach
          o for
          o while
          o continue
          o break
       > Module Calling
       > Remote Calling
       > Authentication
       > Data Access
       > Socket
       > Special Functions
    > Database Operation
    > Content Management
    > Objects and Classes
    > Interface with others
    o Glossary
> Portal Object
> Development Guide


Shortcuts
sys Class
debug Class
Intrinsic Conversions
>> Reference >> BEE Commands >> Loop >> for <=  =>      <  1  >  
for – loop through a series of numeric values

BEE Script:   for [maxiter=num] ([var] [from num] [to num] [step num])

                  statement;

or          for [maxiter=num] ([var] [from num] [to num] [step num])

                  { statement; ... }

 

BEE Tag:     <beefor var [maxiter=num] [from=num] [to=num] [step=num]>

                  tag

                  ...

            </beefor>

 

Note: While the numeric values (num) are usually integer, BEE can accept floating point values as well.

"For" loop is used to loop through a series of uniformly increasing or decreasing numeric values (with an equal interval (step) in between).

Example 1:

for (i from 3 to 10)

{

      display "Children of year {i} please come here<br> ";

}

 

Example 2:

display "Ready ... ";

for (i from 3 to 1) display "{i} - ";

display "Go!<br> ";

 

Example 3:

display "All even numbers between 100 and 110: ";

for (i from 100 to 110 step 2) display "{i} ";

 

 

In most cases, a "for" loop can be replaced by a "foreach" loop, using no Loop Variable but specifying the "maxiter" parameter as the number of iterations required.

Example: The following two loops display the same result

for (i from 1 to 3) display "{i} ";

foreach maxiter=3 { display "{result%foreach:iteration} "; }

 

 

Parameters

var is the Loop Variable.  The default Loop Variable is "for".  Explicitly specifying the Loop Variable helps to make the code reads better, and also is necessary in a nested loop to avoid crashing of the Loop Variables if the inner loop use the same Loop Variable as the outer one.

A "for" loop will be "inactive" if the ending condition is satisfied even before the first iteration.  For example, the parameter "from" is larger than "to" and "step" is positive, in such case the loop is taken as ended before it even starts.  The Loop Variable receives a value ONLY IF at least the first iteration of the loop is executed.

The Loop Variable is NOT a reference to the source variable being looped through.  You can modify the Loop Variable and use the new value within the loop, and the change has NO effect on the source variable.  Upon the next iteration, the Loop Variable will be assigned the next value it should receive.

The Loop Variable is an array.  If you specify an element as the Loop Variable, the element part will be ignored and the entire array will be used as the Loop Variable.

from is the value assigned to the Loop Variable in the first iteration (unless the "for" loop is in active, in which case the Loop Variable will not be altered at all.)  The "from" parameter is default to 0.

to is the value that specifies the end of the number series.  Before the Loop Variable is assigned the next number in the series, the number will be checked and if it goes beyond the "to" value (larger than "to" if "step" is positive or smaller than "to" if "step" is negative), the loop will exit without assigning the number to the Loop Variable.  The "to" parameter is default to 0.

step is the number used to generate the number series.  The first number of the series is the value of the "from" parameter.  Every number after the first one is obtained by adding the value of the "step" parameter to the previous number (the one contained in the Loop Variable for the last iteration).

If the "step" value is positive, the number is increasing in every iteration.  If the "step" value is negative, the number is decreasing.  The loop will be "inactive" (not executed) if the series is not expected to terminate (i.e. "from" is larger than "to" and "step" is positive, or "from" is smaller than "to" and "step" is negative.)

If the "step" value is zero or omitted, it will be taken as 1 if the "from" value is not larger than the "to" value, or -1 if otherwise.  By omitting the "step" parameter, you guarantee that the number series will be stepping from the "from" value to the "to" value by 1 each term towards to right direction to terminate.  (e.g. "from 1 to 3" will generate 1, 2, 3, and "from 3 to 1 will generate 3, 2, 1.)

maxiter is the maximum number of iterations the loop will execute.  It is default to 1000.  Zero means infinity.

Previous Page       Next Page

Accsoft Computer Technology Pty Ltd     ABN: 98 065 617 549
PO Box 892, Epping NSW 1710         Level 1, Epping Office Park, 242 Beecroft Rd, Epping NSW 2121, Australia
Tel: Sydney - (02)98691668     National - 1300-881668         Fax: (02)98691866
© Copyright 2003 Accsoft Computer Technology Pty Ltd