8.8   Control Flow Operators

The operators introduced in this section execute a block of operators conditionally or repeatedly. Usually, these operators come in pairs: One operator marks the start of the block while the other marks the end. The code lines in-between are referred to as the body of a control flow structure.

When you enter a control flow operator to start a block, HDevelop also adds the corresponding closing operator by default to keep the program code balanced. In addition, the IC is placed between the control flow operators. This is fine for entering new code blocks. If you want to add control flow operators to existing code, you can also add the operators individually. Keep in mind, however, that a single control flow operator is treated as invalid code until its counterpart is entered as well.

In the following, <condition> is an expression that evaluates to an integer or boolean value. A condition is false if the expression evaluates to 0 (zero). Otherwise, it is true. HDevelop provides the following operators to control the program flow:

if ... endif

This control flow structure executes a block of code conditionally. The operator if takes a condition as its input parameter. If the condition is true, the body is executed. Otherwise the execution is continued at the operator call that follows the operator endif.

To enter both if and endif at once, select the operator if in the operator window and make sure the check box next to the operator is ticked.

  if (<condition>)
    ...
  endif

if ... else ... endif

Another simple control flow structure is the condition with alternative. If the condition is true, the block between if and else is executed. If the condition is false, the part between else and endif is executed.

To enter all three operators at once, select the operator ifelse in the operator window and make sure the check box next to the operator is ticked.

  if (<condition>)
    ...
  else
    ...
  endif

elseif

This operator is similar to the else-part of the previous control flow structure. However, it allows you to test for an additional condition. The block between elseif and endif is executed if <condition1> is false and <condition2> is true. elseif may be followed by an arbitrary number of additional elseif instructions. The last elseif may be followed by a single else instruction.

  if (<condition1>)
    ...
  elseif (<condition2>)
    ...
  endif

This is syntactically equivalent and thus a shortcut for the following code block:

  if (<condition1>)
    ...
  else
    if (<condition2>)
      ...
    endif
  endif

while ... endwhile

This is a looping control flow structure. As long as the condition is true, the body of the loop is executed. In order to enter the loop, the condition has to be true in the first place. The loop can be restarted and terminated immediately with the operator continue and break, respectively (see below).

To enter both while and endwhile at once, select the operator while in the operator window and make sure the check box next to the operator is ticked.

  while (<condition>)
   ...
  endwhile

repeat ... until

This loop is similar to the while loop with the exception that the condition is tested at the end of the loop. Thus, the body of a repeat ... until loop is executed at least once. Also in contrast to the while loop, the loop is repeated if the condition is false, i.e., until it is finally true.

To enter both repeat and until at once, select the operator until in the operator window and make sure the check box next to the operator is ticked.

  repeat
    ...
  until (<condition>)

for ... endfor

The for loop is controlled by a start and an end value and an increment value, step, that determines the number of loop steps. These values may also be expressions, which are evaluated immediately before the loop is entered. The expressions may be of type integer or of type real. If all input values are of type integer, the loop variable will also be of type integer. In all other cases the loop variable will be of type real.

Please note that the for loop is displayed differently in the program window than entered in the operator window. What you enter in the operator window as for(start,end,step,index) is displayed in the program window as:

  for <index> := <start> to <end> by <step>
    ...
  endfor

To enter both for and endfor at once, select the operator for in the operator window and make sure the check box next to the operator is ticked.

The start value is assigned to the index variable. The loop is executed as long as the following conditions are true: 1) The step value is positive, and the loop index is smaller than or equal to the end value. 2) The step value is negative, and the loop index is greater than or equal to the end value. After a loop cycle, the loop index is incremented by the step value and the conditions are evaluated again.

Thus, after executing the following lines,

  for i := 1 to 5 by 1
    j := i
  endfor

i is set to 6 and j is set to 5, while in

  for i := 5 to 1 by -1
    j := i
  endfor

i is set to 0, and j is set to 1.

The loop can be restarted and terminated immediately with the operator continue and break, respectively. (see below).

Please note, that in older versions of HDevelop (prior to HALCON 11), the expressions for start and termination value were evaluated only once when entering the loop. A modification of a variable that appeared within these expressions had no influence on the termination of the loop. The same applied to the modifications of the loop index. It also had no influence on the termination. The loop value was assigned to the correct value each time the for operator was executed. See the reference manual of the for operator for more information.

If the for loop is left too early (e.g., if you press Stop and set the PC) and the loop is entered again, the expressions will be evaluated, as if the loop were entered for the first time.

In the following example the sine from 0 up to 6π is computed and printed into the graphical window (file name: sine.hdev):

  old_x := 0
  old_y := 0
  dev_set_color ('red')
  dev_set_part(0, 0, 511, 511)
  for x := 1 to 511 by 1
      y := sin(x / 511.0 * 2 * 3.1416 * 3) * 255
      disp_line (WindowID, -old_y+256, old_x, -y+256, x)
      old_x := x
      old_y := y
  endfor

In this example the assumption is made that the window is of size 512 × 512. The drawing is always done from the most recently evaluated point to the current point.

continue

The operator continue forces the next loop cycle of a for, while, or repeat loop. The loop condition is tested, and the loop is executed depending on the result of the test.

In the following example, a selection of RGB color images is processed. Images with channel numbers other than three are skipped through the use of the operator continue. An alternative is to invert the condition and put the processing instructions between if and endif. But the form with continue tends to be much more readable when very complex processing with lots of lines of code is involved.

  i := |Images|
  while (i)
    Image := Images[i]
    count_channels (Image, Channels)
    if (Channels != 3)
      continue
    endif
    * extensive processing of color image follows
  endwhile

break

The operator break enables you to exit for, while, and repeat loops. The program is then continued at the next line after the end of the loop.

A typical use of the operator break is to terminate a for loop as soon as a certain condition becomes true, e.g., as in the following example:

  Number := |Regions|
  AllRegionsValid := 1
  * check whether all regions have an area <= 30
  for i := 1 to Number by 1
    ObjectSelected := Regions[i]
    area_center (ObjectSelected, Area, Row, Column)
    if (Area > 30)
      AllRegionsValid := 0
      break ()
    endif
  endfor

In the following example, the operator break is used to terminate an (infinite) while loop as soon as one clicks into the graphics window:

  while (1)
    grab_image (Image, FGHandle)
    dev_error_var (Error, 1)
    dev_set_check ('~give_error')
    get_mposition (WindowHandle, R, C, Button)
    dev_error_var (Error, 0)
    dev_set_check ('give_error')
    if ((Error = H_MSG_TRUE) and (Button != 0))
      break ()
    endif
  endwhile

switch ... case ... endswitch
The switch block allows you to control the program flow via a multiway branch. The branch targets are specified with case statements followed by an integer constant. Depending on an integer control value the program execution jumps to the matching case statements and continues to the next break statement or the closing endswitch statement. An optional default statement can be defined as the last jump label within a switch block. The program execution jumps to the default label if no preceding case statement matches the control expression.

  ...
  switch (Grade)
    case 1:
      Result := 'excellent'
      break
    case 2:
      Result := 'good'
      break
    case 3:
      Result := 'acceptable'
      break
    case 4:
    case 5:
      Result := 'unacceptable'
      break
    default:
      Result := 'undefined'
  endswitch
  ...

stop

The operator stop stops the program after the operator is executed. The program can be continued by pressing the Step Over or Run button.

exit

The exit operator terminates the HDevelop session.

return

The operator return returns from the current procedure call to the calling procedure. If return is called in the main procedure, the PC jumps to the end of the program, i.e., the program is finished.

try ... catch ... endtry

This control flow structure enables dynamic exception handling in HDevelop. The program block between the operators try and catch is watched for exceptions, i.e., runtime errors. If an exception occurs, diagnostic data about what caused the exception is stored in an exception tuple. The exception tuple is passed to the catch operator, and program execution continues from there. The program block between the operators catch and endtry is intended to analyze the exception data and react to it accordingly. If no exception occurs, this program block is never executed.

See section “Error Handling” on page 880, and the reference manual, e.g., the operator try for detailed information.

throw

The operator throw allows you to generate user-defined exceptions.