Skip to content

While

While控件用于控制循环,可以使用Break控件提前结束循环。

业务属性

属性含义说明输入限定示例值
enable是否启用该控件,未启用时不编译是否编译选择值启用
text与控件关联的文本设计页控件显示常量while
expression判断表达式Lua判断表达式Lua脚本var1>var2

判断表达式完全遵循Lua语言的语法,其中不等于逻辑与逻辑或逻辑非分别用~=andornot表示,在判断表达式中变量的表示无需使用#{}占位符,直接书写变量名即可。

当表达式值为假时结束循环。

lua
while ... do
--code
end
while ... do
--code
end

使用示例

使用LuaScript控件定义一个数组r1,用于存放输出参数:

lua
local r1={}
local r1={}
img_7.png

将入参body的属性currentValueendValue分别当作循环变量和循环结束值,判断表达式输入:

body.currentValue<=body.endValue

img_7_1.png

使用TableInsert控件往r1数组插入循环变量值:

body.currentValue

img_7_2.png

使用LuaScript控件对循环变量值自增 1

lua
body.currentValue=body.currentValue+1
body.currentValue=body.currentValue+1
img_7_3.png

使用Return控件输出结果r1

json
{"r1":"#{r1}"}
{"r1":"#{r1}"}
img_9.png

使用curl请求接口:

$ curl http://localhost:6636/api/while -d "{\"currentValue\":0,\"endValue\":5}"
$ curl http://localhost:6636/api/while -d "{\"currentValue\":0,\"endValue\":5}"

接口返回:

json
{
    "resCode": "0",
    "resMsg": "success",
    "data": {
        "r1": [0,1,2,3,4,5]
    }
}
{
    "resCode": "0",
    "resMsg": "success",
    "data": {
        "r1": [0,1,2,3,4,5]
    }
}

当循环变量值自增到6时,条件表达式判断为,循环结束。