使用右上角的选项卡切换到不同的编程语言。

使用右上角的选项卡切换到不同的编程语言。

使用右上角的选项卡切换到不同的编程语言。

使用右上角的选项卡切换到不同的编程语言。

forforForForfor For循环(算子)

名称

forforForForfor — 启动一个循环块,通常执行固定次数的迭代。

签名

for( : : Start, End, Step : Index)

Herror for(const Hlong Start, const Hlong End, const Hlong Step, Hlong* Index)

Herror T_for(const Htuple Start, const Htuple End, const Htuple Step, Htuple* Index)

void For(const HTuple& Start, const HTuple& End, const HTuple& Step, HTuple* Index)

static void HOperatorSet.For(HTuple start, HTuple end, HTuple step, out HTuple index)

def for(start: Union[float, int], end: Union[float, int], step: Union[float, int]) -> Union[float, int]

描述

HDevelop 中的语法:for Index := Start to End by Step

forforForForForfor 语句启动一个循环块,该循环块通常执行固定次数的迭代。forforForForForfor 块在对应的 endforendforEndforEndforEndforendfor 语句处结束。

迭代次数由 StartStartStartStartstartstart 值、EndEndEndEndendend 值和增量值 StepStepStepStepstepstep 共同决定。所有这些参数均可使用表达式或变量初始化,而非固定数值。请注意,这些循环参数仅在进入 forforForForForfor 循环前立即评估一次。循环迭代后不会重新评估,即循环体内的变量修改不会影响迭代次数。

传递的循环参数必须为 integerreal 类型。若所有输入参数均为 integer 类型,则 IndexIndexIndexIndexindexindex 变量也将为 integer 类型。在其他所有情况下,IndexIndexIndexIndexindexindex 变量将为 real 类型。

每次迭代开始时,循环变量 IndexIndexIndexIndexindexindex 都会与 EndEndEndEndendend 参数进行比较。若递增值 StepStepStepStepstepstep 为正,则只要 IndexIndexIndexIndexindexindex 变量小于或等于 EndEndEndEndendend 参数,forforForForForfor 循环就会持续执行;若递增值 StepStepStepStepstepstep 为负,则只要 IndexIndexIndexIndexindexindex 变量大于或等于 EndEndEndEndendend 参数,forforForForForfor 循环就会持续执行。

注意:若增量 StepStepStepStepstepstep 被设为 real类型,当 IndexIndexIndexIndexindexindex 变量在最后一次循环中需精确匹配 EndEndEndEndendend 值时,可能因舍入误差导致最后一次循环被省略。因此在某些系统中,下述循环不会如预期执行四次(当 IndexIndexIndexIndexindexindex 变量依次设为 1.3、1.4、1.5 和1.6),而仅执行三次——因为经过三次累加后,指数变量因舍入误差会略大于 1.6。 I:=[] for Index := 1.3 to 1.6 by 0.1 I := [I,Index] endfor

在循环体执行完毕后(即到达对应的 endforendforEndforEndforEndforendfor 语句或 continuecontinueContinueContinueContinuecontinue 语句时),会将增量值(在 forforForForForfor 循环开始时初始化)加到循环计数器 IndexIndexIndexIndexindexindex 的当前值上。随后,循环条件将如上所述重新评估。根据评估结果,循环要么重新执行,要么终止——此时执行流程将跳转至对应 endforendforEndforEndforEndforendfor 语句后的第一条语句继续运行。

循环内部的 breakbreakBreakBreakBreakbreak 语句——若未被更内层的代码块覆盖——将立即退出循环,执行流程跳转至对应的 endforendforEndforEndforEndforendfor 语句之后。而 continuecontinueContinueContinueContinuecontinue 语句的作用则是跳过当前循环周期的剩余循环体,通过更新 IndexIndexIndexIndexindexindex 变量并重新评估循环条件来继续执行。

注意:建议避免在 forforForForForfor 循环的循环体内修改 IndexIndexIndexIndexindexindex 变量。

forforForForForfor 循环被终止(例如通过 stopstopStopStopStopstop 语句或按下停止按钮),且程序计数器(PC)由用户手动设置,则只要程序计数器仍位于 forforForForForfor 循环体内或被设置为 endforendforEndforEndforEndforendfor 语句,该 forforForForForfor 将从当前迭代点继续执行。若程序计数器设置在 forforForForForfor 语句(或其之前)并再次执行,则循环将重新初始化并从开头开始。

参数

StartStartStartStartstartstart (输入控制)  number HTupleUnion[float, int]HTupleHtuple (integer / real) (int / long / double) (Hlong / double) (Hlong / double)

循环变量的起始值。

默认值: 1

EndEndEndEndendend (输入控制)  number HTupleUnion[float, int]HTupleHtuple (integer / real) (int / long / double) (Hlong / double) (Hlong / double)

循环变量的终止值。

默认值: 5

StepStepStepStepstepstep (输入控制)  number HTupleUnion[float, int]HTupleHtuple (integer / real) (int / long / double) (Hlong / double) (Hlong / double)

循环变量的递增值。

默认值: 1

IndexIndexIndexIndexindexindex (输出控制)  number HTupleUnion[float, int]HTupleHtuple (integer / real) (int / long / double) (Hlong / double) (Hlong / double)

循环变量。

示例(HDevelop)

read_image (Image, 'fabrik')
threshold (Image, Region, 128, 255)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 150, 99999)
area_center (SelectedRegions, Area, Row, Column)
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_display (Image)
dev_display (SelectedRegions)
dev_set_color ('white')
for Index := 0 to |Area| - 1 by 1
  set_tposition (WindowHandle, Row[Index], Column[Index])
  write_string (WindowHandle, 'Area=' + Area[Index])
endfor

结果

如果指定参数的值正确,则 forforForForForfor(作为运算符)返回 2 ( H_MSG_TRUE )。否则,将抛出异常并返回错误代码。

替代

whilewhileWhileWhileWhilewhile, untiluntilUntilUntilUntiluntil

另见

repeatrepeatRepeatRepeatRepeatrepeat, breakbreakBreakBreakBreakbreak, continuecontinueContinueContinueContinuecontinue, endforendforEndforEndforEndforendfor

模块

基础