Skip to content

Control flow statements🔗

The statements introduced in this section execute a block of operators conditionally or repeatedly. Usually, these statements come in pairs: One statement 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.

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.

HDevelopEVO provides the following statements to control the program flow:

Conditions🔗

ifendif🔗

This control flow structure executes a block of code conditionally. The if statement 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 endif statement.

if (<condition>)
  ...
endif

ifelseendif🔗

Another 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.

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

elseif🔗

This statement 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 can be followed by an arbitrary number of additional elseif instructions. The last elseif can 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

switchcaseendswitch🔗

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
...

Loops🔗

forendfor🔗

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.

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

The start value is assigned to the index variable. The loop is executed as long as the following conditions are true:

  • The step value is positive, and the loop index is smaller than or equal to the end value.
  • 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.

Example

After executing the following lines, i is set to 6 and j is set to 5:

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

In contrast, i is set to 0, and j is set to 1 in the following:

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

The loop can be restarted and terminated immediately with the statements continue and break, respectively.

If the for loop is left too early (for example, if you press , and set the program counter) and the loop is entered again, the expressions will be evaluated, as if the loop was entered for the first time.

Example

In the following, the sine from 0 up to 6\(\pi\) is computed and printed into the graphical window (file name: sine.hscript):

dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowID)
dev_set_part (0, 0, 511, 511)
old_x := 0
old_y := 0
dev_set_color ('red')
for x := 1 to 511 by 4
    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

Here we assume that the window is of size 512 × 512. The drawing is always done from the most recently evaluated point to the current point.

whileendwhile🔗

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

while (<condition>)
  ...
endwhile

repeatuntil🔗

This loop is similar to the while loop, but here the condition is tested at the end of the loop. Thus, the body of a repeatuntil loop is executed at least once. In further contrast to the while loop, the loop is repeated if the condition is false, until it is finally true.

repeat
  ...
until (<condition>)

break🔗

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

A typical use of the break statement is to terminate a for loop as soon as a certain condition becomes true.

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

continue🔗

The continue statement 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.

Example

In the following, a selection of RGB color images is processed. Images with channel numbers other than three are skipped through the use of the continue statement. An alternative is to invert the condition and put the processing instructions between if and endif. However, using continue tends to be more readable when complex processing with many 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

Return and stop statements🔗

return🔗

The return statement returns from the current procedure call to the calling procedure. If return is called in the main procedure, the program counter jumps to the end of the program, and the program is finished.

stop🔗

The stop statement stops the program after the statement is executed. The program can be continued by pressing the or button.

Exception handling statements🔗

trycatchendtry🔗

The trycatchendtry control flow structure enables dynamic exception handling in HDevelopEVO. The program block between the try and catch statements is watched for exceptions (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 statement, and program execution continues from there.
The program block between the catch and endtry statements is intended to analyze the exception data and react to it accordingly. If no exception occurs, this program block is never executed.

throw🔗

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

More Info

For more information, see also the section about error handling.