Operator Reference

Use the tabs on the upper right to switch to a different programming language.

Use the tabs on the upper right to switch to a different programming language.

Use the tabs on the upper right to switch to a different programming language.

Use the tabs on the upper right to switch to a different programming language.

forforForForfor (Operator)

forforForForfor — Starts a loop block that is usually executed for a fixed number of iterations.

Signature

for( : : Start, End, Step : Index)

Herror for(const Hlong Start, const Hlong End, const Hlong Step, Hlong* Index)

Herror T_for(const Htuple Start, const Htuple End, const Htuple Step, Htuple* Index)

void For(const HTuple& Start, const HTuple& End, const HTuple& Step, HTuple* Index)

static void HOperatorSet.For(HTuple start, HTuple end, HTuple step, out HTuple index)

def for(start: Union[float, int], end: Union[float, int], step: Union[float, int]) -> Union[float, int]

Description

Syntax in HDevelop: for Index := Start to End by Step

The forforForForfor statement starts a loop block that is usually executed for a fixed number of iterations. The forforForForfor block ends at the corresponding endforendforEndforEndforendfor statement.

The number of iterations is defined by the StartStartStartstartstart value, the EndEndEndendend value, and the increment value StepStepStepstepstep. All of these parameters can be initialized with expressions or variables instead of constant values. Please note that these loop parameters are evaluated only once, namely, immediately before the forforForForfor loop is entered. They are not re-evaluated after the loop cycles, i.e., any modifications of these variables within the loop body will have no influence on the number of iterations.

The passed loop parameters must be either of type integer or real. If all input parameters are of type integer, the IndexIndexIndexindexindex variable will also be of type integer. In all other cases the IndexIndexIndexindexindex variable will be of type real.

At the beginning of each iteration the loop variable IndexIndexIndexindexindex is compared to the EndEndEndendend parameter. If the increment value StepStepStepstepstep is positive, the forforForForfor loop is executed as long as the IndexIndexIndexindexindex variable is less than or equal to the EndEndEndendend parameter. If the increment value StepStepStepstepstep is negative, the forforForForfor loop is executed as long as the IndexIndexIndexindexindex variable is greater than or equal to the EndEndEndendend parameter.

Attention: If the increment value StepStepStepstepstep is set to a value of type real, it may happen that the last loop cycle is omitted owing to rounding errors in case the IndexIndexIndexindexindex variable is expected to match the EndEndEndendend value exactly in the last cycle. Hence, on some systems the following loop is not executed---as expected---for four times (with the IndexIndexIndexindexindex variable set to 1.3, 1.4, 1.5, and 1.6), but only three times because after three additions the index variable is slightly greater than 1.6 due to rounding errors.

  I:=[]
  for Index := 1.3 to 1.6 by 0.1
    I := [I,Index]
  endfor
After the execution of the loop body, i.e., upon reaching the corresponding endforendforEndforEndforendfor statement or a continuecontinueContinueContinuecontinue statement, the increment value (as initialized at the beginning of the forforForForfor loop) is added to the current value of the loop counter IndexIndexIndexindexindex. Then, the loop condition is re-evaluated as described above. Depending on the result the loop is either executed again or finished in which case execution continues with the first statement after the corresponding endforendforEndforEndforendfor statement.

A breakbreakBreakBreakbreak statement within the loop---that is not covered by a more internal block---leaves the loop immediately and execution continues after the corresponding endforendforEndforEndforendfor statement. In contrast, the continuecontinueContinueContinuecontinue statement is used to ignore the rest of the loop body in the current cycle and continue execution with adapting the IndexIndexIndexindexindex variable and re-evaluating the loop condition.

Attention: It is recommended to avoid modifying the IndexIndexIndexindexindex variable of the forforForForfor loop within its body.

If the forforForForfor loop is stopped, e.g., by a stopstopStopStopstop statement or by pressing the Stop button, and if the PC is placed manually by the user, the forforForForfor loop is continued at the current iteration as long as the PC remains within the forforForForfor body or is set to the endforendforEndforEndforendfor statement. If the PC is set on the forforForForfor statement (or before it) and executed again, the loop is reinitialized and restarts at the beginning.

Parameters

StartStartStartstartstart (input_control)  number HTupleUnion[float, int]HTupleHtuple (integer / real) (int / long / double) (Hlong / double) (Hlong / double)

Start value of the loop variable.

Default: 1

EndEndEndendend (input_control)  number HTupleUnion[float, int]HTupleHtuple (integer / real) (int / long / double) (Hlong / double) (Hlong / double)

End value of the loop variable.

Default: 5

StepStepStepstepstep (input_control)  number HTupleUnion[float, int]HTupleHtuple (integer / real) (int / long / double) (Hlong / double) (Hlong / double)

Increment value of the loop variable.

Default: 1

IndexIndexIndexindexindex (output_control)  number HTupleUnion[float, int]HTupleHtuple (integer / real) (int / long / double) (Hlong / double) (Hlong / double)

Loop variable.

Example (HDevelop)

read_image (Image, 'fabrik')
threshold (Image, Region, 128, 255)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 150, 99999)
area_center (SelectedRegions, Area, Row, Column)
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_display (Image)
dev_display (SelectedRegions)
dev_set_color ('white')
for Index := 0 to |Area| - 1 by 1
  set_tposition (WindowHandle, Row[Index], Column[Index])
  write_string (WindowHandle, 'Area=' + Area[Index])
endfor

Result

If the values of the specified parameters are correct, forforForForfor (as an operator) returns 2 ( H_MSG_TRUE) . Otherwise, an exception is raised and an error code is returned.

Alternatives

whilewhileWhileWhilewhile, untiluntilUntilUntiluntil

See also

repeatrepeatRepeatRepeatrepeat, breakbreakBreakBreakbreak, continuecontinueContinueContinuecontinue, endforendforEndforEndforendfor

Module

Foundation