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.
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 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
.
if
… else
… endif
🔗
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.
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
can be followed by an arbitrary number of additional elseif
instructions.
The last elseif
can be followed by a single else
instruction.
This is syntactically equivalent and thus a shortcut for the following code block:
while
… endwhile
🔗
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 operators continue
and break
, respectively.
repeat
… until
🔗
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 repeat
… until
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.
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
.
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.
Thus, after executing the following lines, i
is set to 6 and j
is set to 5:
In contrast, i
is set to 0, and j
is set to 1 in the following:
The loop can be restarted and terminated immediately with the operators 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.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
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.
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.
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 operator continue
.
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.
break
🔗
The operator break 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 operator break
is to terminate a for
loop as soon as a certain condition becomes true.
Example
In the following example, the operator break
is used to terminate an (infinite) while
loop as soon as one clicks into the graphics window.
Example
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 or button.
exit
🔗
The exit
operator terminates the HDevelopEVO session.
return
🔗
The operator return
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.
try
… catch
… endtry
🔗
This control flow structure enables dynamic exception handling in HDevelopEVO.
The program block between the operators try
and catch
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
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 also the section about error handling for detailed information.
throw
🔗
The operator throw
allows you to generate user-defined exceptions.