if语句 – Lua中的链接逻辑操作

前端之家收集整理的这篇文章主要介绍了if语句 – Lua中的链接逻辑操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是在Lua中最有效的方法吗?谢谢!

if X >= 0 and Y >= 0 then
    if X1 <= 960 and Y1 <= 720 then
        someCode()
    else end
else end

解决方法

避免使用嵌套的if语句是个好主意,如果检查我会尝试单个.
最好的方法是分析功能,看看哪些功能更快.

-- Be paranoid and use lots of parenthesis:
if ( ( (X >= 0) and (Y >= 0) ) and ( (X1 <= 960) and (Y1 <= 720) ) ) then
    someCode()
end

这是相同的,但更容易阅读.好的代码不仅速度快,而且易于阅读.

local condition1 = ((X >= 0) and (Y >= 0))
local condition2 = ((X1 <= 960) and (Y1 <= 720))

if (condition1 and condition2) then
    someCode()
end

猜你在找的Lua相关文章