00. 目录
01. 概述
02. 签名
03. 描述
04. 注意
05. 参数
06. 结果
07. 附录
for — 启动一个循环块,通常执行固定次数的迭代。
<code>for( : : start, end, step : index)</code>
在hdevelop中的语法:for index := start to end by step。
for语句启动一个通常是运行固定次数的迭代的循环分段。 for分段结束于相应的endfor语句。
迭代次数由start值,end值和step值共同决定。 所有这些参数都不一定是常量值,可以用表达式或变量初始化替代。 请注意,这些循环参数只被计算一次,即在for循环输入之前。 它们在循环之后不被重新计算,即在循环内对这些变量的任何修改都不会影响迭代次数。
传递的循环参数必须是整型或实型。 如果所有输入参数都是整数类型的,那么index变量也是整型。 在所有其他情况下,index变量将是real类型的。
在每次迭代开始时,将循环变量index与end参数进行比较。 如果step为正,则只要index变量小于或等于end参数,就会运行for循环。 如果step为负,则只要index变量大于或等于end参数,就会执行for循环。
注意:如果将step设置为real类型的值,则在index变量预期与上一个周期中的end值完全匹配的情况下,可能会因为四舍五入误差而忽略最后一个循环。 因此,在一些系统中,下面的循环没有按照预期的那样运行了四次(index变量设置为1.3,1.4,1.5和1.6),只运行了三次,因为在三次加法之后,索引变量由于四舍五入的误差而略大于1.6。
在循环内容的运行之后,即在到达相应的endfor语句或者continue语句之后,循环计数器index的当前值会加上step(在for循环的开始处被初始化)。 然后,如上所述重新判断循环条件。 根据结果,循环要么被重新运行,要么运行完毕。运行完毕时,将会继续运行相应的endfor语句之后的第一个语句。
循环中的存在break语句—没有被更多的内部分段所覆盖(that is not covered by a more internal block)—立即离开循环,并在相应的endfor语句之后继续运行。 与之相反的是,continue语句用于忽略当前循环中其余的循环内容,并继续执行索引变量并重新判断循环条件。
注意:在halcon 11中,for循环相对于index变量的行为发生了变化:在以前的版本中,循环体内的index变量的变化被for语句忽略。 在判断循环条件之前,index变量将被重置为一个内部计数器,计数器的计算公式如下:
index := start + count * step
count是已经运行的循环次数。
使用halcon 11之前的halcon版本保存的程序和程序(program and procedure)在加载时通过分析它们的for循环保持不变地运行:如果在循环体内修改了这样的for循环的index变量,则循环被设置为兼容模式并会进行相应的标注。所有以这种方式标记的循环都是使用旧的语义执行的,即在每个循环周期之前将index变量重置为内部计数器以保持旧的行为。在程序列表中,for语句由一个警告三角形和行尾的标签__use_internal_index__标记。另外,在算子窗口中也会显示警告。check box允许取消所选for循环的兼容模式,并使用新的语义执行。在全文编辑器中删除标签__use_internal_index__具有相同的效果。但是,为了保持程序行为,必须通过将循环体开头的index变量复制到局部变量来稍微调整for循环体。这个局部变量可以在循环体内随意使用和修改。
在任何情况下,建议避免在其内部修改for循环的index变量,因为代码变得难以调试,代码将不兼容halcon 11之前的halcon版本。
如果for循环停止,例如通过停止语句或按下停止按钮,并且如果用户手动放置pc(程序计数器),则只要pc还在for循环内,for循环在当前迭代中继续或者跳到endfor语句。 如果pc放置在在for语句中(或之前)并再次执行,则循环将重新初始化并在开始时重新启动。
原文描述:
syntax in hdevelop: for index := start to end by step
the for statement starts a loop block that is usually executed for a fixed number of iterations. the for block ends at the corresponding endfor statement.
the number of iterations is defined by the start value, the end value, and the increment value step. 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 for 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 index variable will also be of type integer. in all other cases the index variable will be of type real.
at the beginning of each iteration the loop variable index is compared to the end parameter. if the increment value step is positive, the for loop is executed as long as the index variable is less than or equal to the end parameter. if the increment value step is negative, the for loop is executed as long as the index variable is greater than or equal to the end parameter.
attention: if the increment value step 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 index variable is expected to match the end value exactly in the last cycle. hence, on some systems the following loop is not executed—as expected—for four times (with the index 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 endfor statement or a continue statement, the increment value (as initialized at the beginning of the for loop) is added to the current value of the loop counter index. 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 endfor statement.
a break statement within the loop—that is not covered by a more internal block—leaves the loop immediately and execution continues after the corresponding endfor statement. in contrast, the continue statement is used to ignore the rest of the loop body in the current cycle and continue execution with adapting the index variable and re-evaluating the loop condition.
attention: the behavior of the for loop with respect to the index variable has changed in halcon 11: in previous versions changes to the index variable within the loop body were ignored by the for statement. before evaluating the loop condition the index variable would be reset to an internal counter that was calculated by:
where count was the number of already executed cycles.
programs and procedures that were saved with a halcon version prior to halcon 11 are kept running unmodified by analyzing their for loops while loading: if the index variable of such a for loop is modified within the loop body, the loop is set into a compatibility mode and marked accordingly. all for loops that are marked in such a way are executed using the old semantics, i.e., the index variable is reset to the internal counter before each loop cycle to keep the old behavior. in the listing the for statement is marked by a warning triangle and the label use_internal_index at the end of the line. in addition, in the operator window a warning is displayed. a check box allows to cancel the compatibility mode for the selected for loop and perform it with the new semantics. deleting the label use_internal_index in the full text editor has the same effect. however, to keep the program behavior, the body of the for loop must slightly be adapted by copying the index variable at the begin of the loop body to a local variable. this local variable can be used and modified within the loop body at will.
in any case it is recommended to avoid modifying the index variable of the for loop within its body because the code becomes harder to debug and the code will not be compatible to halcon versions prior to halcon 11.
if the for loop is stopped, e.g., by a stop statement or by pressing the stop button, and if the pc is placed manually by the user, the for loop is continued at the current iteration as long as the pc remains within the for body or is set to the endfor statement. if the pc is set on the for statement (or before it) and executed again, the loop is reinitialized and restarts at the beginning.
无
start (input_control) number → (integer / real)
循环变量的起始值。
默认值: 1
end (input_control) number → (integer / real)
循环变量的结束值。
默认值: 5
step (input_control) number → (integer / real)
循环变量的增量值。
index (output_control) number → (integer / real)
循环变量
如果指定参数的值是正确的,for(作为算子)返回2(h_msg_true)。 否则,会引发异常并返回错误代码。
hdevelop例程
match_function_trans.hdev calculate transformation parameters between two functions fuzzy_measure_pin.hdev measure pins of an ic using fuzzy measuring for.hdev use a for loop to iterate over extracted blobs assign.hdev assign values to variables and tuple elements
程序示例
7.1 机器视觉博客汇总