Lua编程语言的if语句语法是:
if(boolean_expression) then --[ statement(s) will execute if the boolean expression is true --] end
如果布尔表达式的计算结果为代码的if语句为true,那么块将被执行。如果if语句的末尾(右大括号后)布尔表达式计算为false,那么第一组代码将被执行。
Lua程序设计语言假定布尔true和非零值的任意组合作为true,以及它是否是布尔假或零,则假定为false值。但应当注意的是,在Lua零值也会被视为true。
--[ local variable definition --] a = 10; --[ check the boolean condition using if statement --] if( a < 20 ) then --[ if condition is true then print the following --] print("a is less than 20" ); end print("value of a is :", a);
当建立和运行上面代码,它会产生以下结果。
a is less than 20 value of a is : 10