「Lua」if文にnilを比較するサンプル
環境
Lua 5.4.4
構文1
if 変数名 == nil then
処理コード
「==」を使って、nilを判定しています。
構文2
if not(変数名) then 処理コード
「not」を使って、nilを判定しています。
nilの場合、trueになります。
使用例
xx = nil
if xx == nil then
print("123")
print(xx)
end
if not (xx) then
print("456")
print(xx)
end
実行結果
C:\study\lua>lua 11.lua 123 nil 456 nil