控制结构

扩展函数语言提供了控制程序执行的常用语句。

  • 如果然后,否则
  • 虽然做并重复,直到
  • 数字对于
  • 打破
  • 转到

如果然后,否则

if 语句根据一个或多个条件选择要执行的语句块。有三种形式:

如果然后形成

if expression then
     statements to execute if expression is not false or nil
end
<!--NeedCopy-->

如果然后,形式

if expression then
     statements to execute if expression is not false or nil
else
     statements to execute if expression is false or nil
end
<!--NeedCopy-->

如果然后别的表格

if expression1 then
     statements to execute if expression1 is not false or nil
     elseif expression2 then
          statements to execute if expression2 is not false or nil
. . .
else
     statements to execute if all expressions are false or nil
end
<!--NeedCopy-->

示例:

if headers[name] then

     local next_value_index = #(headers[name]) + 1
     headers[name][next_value_index] = value

else

     headers[name] = {name .. ":" .. value}

end
<!--NeedCopy-->

注意

  • 表达式不像 C 和 Java 中的情况那样括在括号中。
  • 没有等效于 C/Java 开关语句。您必须使用一系列 if seif 语句来执行等价操作。

虽然做并重复,直到

while 语句和 重复 语句提供由表达式控制的循环。

while expression do
     statements to execute while expression is not false or nil
end

repeat

     statements to execute until expression is not false or nil

until expression
<!--NeedCopy-->

示例,而:

local a = {1, 2, 3, 4}
local sum, i = 0, 1 -- multiple assignment initializing sum and i
while i <= #a do -- check if at the end of the array
     sum = sum + a[i] -- add array element with index i to sum
     i = i + 1 -- move to the next element
end
<!--NeedCopy-->

重复示例:

sum, i = 0, 1 -- multiple assignment initializing sum and i
repeat
     sum = sum + a[i] -- add array element with index i to sum
     i = i + 1 -- move to the next element
until i > #a -- check if past the end of the array
<!--NeedCopy-->

当然,可以编写一个不终止的循环,例如,如果在这些示例中忽略了 i = i + 1 语句。执行此类功能时,Citrix ADC 将检测到该功能未在合理的时间内完成,并且会在运行时出现错误时终止该功能:

Cpu limit reached. Terminating extension execution in [[string "function extension function..."]]: line line-number.

将在 /var/log /ns.log中报告。

数字对于

有两种类型的 for 循环。第一个是的数字,它类似于 C 和 Java 中的 for 语句的通常使用。数字的 if 语句初始化一个变量,测试该变量是否已传递最终值,如果没有执行一组语句,则增加该变量,然后重复。数值 for 循环的语法是:

for variable = initial, final, increment do

     statements in the loop body

end
<!--NeedCopy-->

其中初始,final 和增量都是产生(或可以转换为)数字的表达式。变量被认为是 for 循环语句块的本地; 它不能在循环之外使用。增量可以省略; 默认值为 1。这些表达式在循环开始时被评估一次。如果增量为正数,终止条件为变量 > final;如果增量为负数,则终止条件为变量 < final。如果增量为 0,循环将立即终止。

示例(相当于上一节中的 while 和重复循环):

sum = 0
for i = 1, #a do -- increment defaults to 1
     sum = sum + a[i]
end
<!--NeedCopy-->

for 循环的第二种类型是通用的,它可以用于更灵活的循环类型。它涉及函数的使用,所以将在函数引入后讨论。

打破

break 语句在一段时间内使用,重复或 for 循环。它将终止循环并在循环后的第一个语句中恢复执行。示例(也等同于前面的 while、重复和 for 循环):

sum, i = 0, 1
while true do
     if i > #a then
          break
     end
     sum = sum + a[i]
     i = i + 1
end
<!--NeedCopy-->

转到

goto 语句可用于向前或向后跳转到标签。标签是一个标识符,其语法是:: label::。goto 语句是 goto 标签。示例(再次等同于前面的循环):

sum, i = 0, 1
::start_loop::
     if i > #a then
          goto end_loop -- forward jump
     end
     sum = sum + a[i]
     i = i + 1
     goto start_loop -- backwards jump
::end_loop::
. . .
<!--NeedCopy-->

在编程中使用 gotos 一直存在着长期的争议。一般来说,您应该尝试使用其他控制结构来使您的函数更具可读性和可靠性。但偶尔明智地使用 gotos 可能会导致更好的程序。特别是,gotos 可能在处理错误时有用。

控制结构