制御構造体
制御構造if、while、repeatは通常の意味を持ち、おなじみの構文である。
statement ::= while exp do block end
statement ::= repeat block until exp
statement ::= if exp then block
{elseif exp then block}
[else block]
end
Luaにもfor文があり、2種類ある。
制御構造の条件式は任意の値を返すことができます。 false とnull は、両方とも false と見なされます。 null とfalse 以外のすべての値は、true と見なされます (特に、数値の 0 や空のストリングも true です)。
repeat–until ループでは、 内側のブロックは until キーワードのところではなく、条件の後でのみ終ります。 そのため、 条件は、ループ・ブロックの内側で宣言されたローカル変数を参照できます。
statement ::= return [explist]
statement ::= break
break は最も内側のループを終了させます。
return ステートメントと break ステートメントは、ブロックの最後の ステートメントとしてのみ記述できます。 If it is really necessary to 戻る or 休憩 in the middle of a block, then an explicit inner block can be used, as in the idioms do return end and do
break end, because now 戻る and 休憩 are the last statements in their (inner) blocks.