00. Contents
01. General
The operators described in this section are conditional or repeated execution of a set of code blocks. Usually, these operators appear in pairs: one operator marks the beginning of the block and the other marks the end of the block. The middle line of code is called the body of the control flow structure.
When you start an HDP flow, you will also add a default HDP flow control operator to keep the corresponding code balanced. In addition, the cursor is placed between the control flow operators. This is good for entering new block s of code. If you want to add control flow operators to existing code, you can also add operators separately. Remember, however, that a single control flow operator is considered invalid code until its corresponding end operator is also entered.
Below is an expression that evaluates to an integer or Boolean value. If the expression evaluates to 0 (zero), the condition is false. Otherwise, the condition is true. HDevelop provides the following operators to control the program flow.
02. if
This control flow structure conditionally executes code blocks. The if operator takes a condition as its input parameter. If the condition is true, the corresponding code snippet is executed. Otherwise, execution continues after the operator endif.
if (<condition>) ... endif
03. if-else
if (<condition>) Code snippet 1 ... else Code snippet 2 ... endif
If the condition is true, execute code segment 1, otherwise execute code segment 2.
04. elseif
if (<condition1>) ... elseif (<condition2>) ... endif
Equivalent to
if (<condition1>) ... else if (<condition2>) ... endif endif
If condition 1 is true, execute code segment 1; otherwise, if condition 2 is true, execute code segment 2
05. while
This is a loop control flow structure. As long as the condition is true, the loop body is executed. In order to enter the loop, the condition must first be true. You can restart and terminate the loop using the operators continue and break, respectively (see below).
while (<condition>) ... endwhile
06. until
This loop is similar to a while loop except that it tests the condition at the end of the loop. Therefore, the body of the repeat... Until loop executes at least once. Similarly, contrary to the while loop, if the condition is false, the loop repeats, that is, until it finally turns true.
repeat ... until (<condition>)
07. for
The for loop is controlled by the start and end values and the incremental value step that determines the number of loop steps. These values can also be expressions that are evaluated immediately before entering the loop. Expressions can be integer or real. If all input values are of integer type, the loop variable will also be of integer type. In all other cases, the loop variable will be of real type.
Note that the for loop is displayed differently in the program window than it is entered in the operator window. The for(start,end,step,index) you entered in the grandchild window is displayed in the program window as:
for <index> := <start> to <end> by <step> ... endfor
Program example
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.
You can restart and terminate the loop through the operators continue and break, respectively.
Note that in older versions of HDevelop (before HALCON 11), the expressions for the start and end values were evaluated only once when entering the loop. The modification of variables appearing in these expressions has no effect on the termination of the loop. The same applies to the modification of circular index. It also has no effect on termination. Each time the for operator is executed, the loop value is assigned to the correct value. For more information, see the operator reference manual.
If the for loop leaves too early (for example, if you press stop and set PC) and enters the loop again, the expression is evaluated as if entering the loop for the first time.
In the following example, the sine from 0 to 6 π is calculated and printed into the graphics window (file name: sine.hdev):
* Compute the sine from 0 to 3*PI and print to the graphical window * 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
results of enforcement
In this example, assume that the size of the window is 512 × 512. The drawing is always carried out from the most recently evaluated point to the current point.
08. continue
The operator continue enforces the next loop of a for, while, or repeat loop. Test the cycle conditions and execute the cycle according to the test results.
In the following example, a set of RGB color images is processed. Skip images with channel numbers other than 3 by using the operator continue. Another way is to reverse the condition and put the processing instruction between if and endif. However, when it comes to very complex processing involving a large number of lines of code, forms with continue tend to be more readable.
i := |Images| while (i) Image := Images[i] count_channels (Image, Channels) if (Channels != 3) continue endif * extensive processing of color image follows endwhile
09. break
The operator break enables you to exit for, while, and repeat loops. The program then continues on the next line after the loop ends.
The typical usage of the operator break is to immediately terminate the for loop when a condition is true. For example, the following example is shown:
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 the (infinite) while loop whenever you click 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
10. switch
Switch allows you to control program flow through multiple branches. The branch target is specified with a case statement followed by an integer constant. According to the integer control value, the program execution jumps to the matching case statement and continues to the next break statement or the end switch statement. An optional default statement can be defined as the last jump label in the switch block. If no previous case statement matches the control expression, the program execution will jump to the default label.
... 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 ...
11. stop
This statement suspends the current program, and the program will stop executing the next statement after executing this statement, and will not continue to execute the program until the confirmation key is pressed through the keyboard.
12. exit
This statement stops the program and exits HDevelop software.
13. return
Operator return returns from the current procedure call to the calling procedure. If return is invoked in the main program, PC jumps to the end of the program, that is, the end of the program.
14. try-catch
This control flow structure enables dynamic exception handling in HDevelop. The block between the operators try and catch is monitored for exceptions, that is, runtime errors. If an exception occurs, diagnostic data about the cause of the exception is stored in the exception tuple. The exception tuple is passed to the catch operator, and the program continues from there. The program block between the operators catch and endtry is designed to analyze exception data and react to it accordingly. If no exception occurs, the block will never be executed.
15. throw
Throw is used to throw an exception during program execution. This statement is usually used with the try catch statement.
16. Appendix
16.1 machine vision blog summary
website: https://dengjin.blog.csdn.net/article/details/116837497