目次
if文 / elseif / else / ネスト構造 if文の中にif文 / nilを==で比較 / nilをnotで比較
論理積(and) AかつB / 論理和(or) AまたはB / 論理否定(not) Aではない
or (デフォルト値を設定) / andとor / デフォルト値(テーブル) / フラグチェック / 範囲チェック / / 存在チェック(テーブル)
確認環境:Lua5.4
if文とは
| if 条件 then 処理 end |
- 条件がtrueの場合にのみ処理を実行します
複数条件(elseifとelseを書く場合)
| if 条件1 then 処理1 elseif 条件2 then 処理2 else 処理3 end |
- 最初にtrueになったブロックだけ実行されます
- それ以降のelseif / elseは評価されません
if文のサンプル
if文
a = 1
if a == 1 then
print("1") -- 1と出力される
end
elseif
a = 2
if a == 1 then
print("1")
elseif a == 2 then
print("2") -- 2と出力される
end
else
a = 2
if a == 1 then
print("1")
else
print("2") -- 2と出力される
end
ネスト構造 if文の中にif文
a = 5
if a > 0 then
if a < 10 then
print("1-9") -- 1-9
end
end
if文の中にif文を記述しています。ネストの構造です。入れ子ともいいます。
深いネストは可読性が悪くなるので注意します。
nilを==で比較
a = nil
if a == nil then
print("nil") -- nil
end
aの値がnilのときのみtrueになります。
nilをnotで比較
a = nil
-- a = false
if not a then
print("true") -- trueと出力される
end
aの値がnilまたはfalseのときtrueになります。
Luaの真偽値ルール
Luaは、falseとnilのみがfalseになります。
それ以外は、すべてtrueです。
| 値 | 判定 |
|---|---|
| false | false |
| nil | false |
| それ以外(0含む) | true |
0の場合
if 0 then
print("true") -- true
end
0はtrueになり文字が出力されます
空文字("")の場合
if "" then
print("true") -- true
end
空文字もtrueになり文字が出力されます
比較演算子
2つの値を比べて条件を満たしていればtrue,そうでない場合はfalseを返します。
| 比較演算子 | 説明 |
|---|---|
| a == b | aとbの値は等しい |
| a ~= b | aとbの値は等しくない |
| a > b | aはbより大きい (より大きい) |
| a >= b | aはbより大きい または 等しい (以上) |
| a < b | aはbより小さい (未満) |
| a <= b | aはbより小さい または 等しい (以下) |
論理演算子
論理積(and) AかつB
| if 左の条件 and 右の条件 |
- 左の条件と右の条件が両方ともtrueの場合、trueになります。それ以外はfalseです。
- 左の式がfalseかnilの場合、右の条件は判定(評価)されません。これを短絡評価(ショートサーキット評価 : Short-Circuit Evaluation)といいます。
a = 6
if a > 0 and a < 10 then
print("1-9") -- 1-9
end
左の条件と右の条件が共にtrueなので、trueになります。
論理和(or) AまたはB
| if 左の条件 or 右の条件 |
- 左の条件または右の条件のどちらかがtrueの場合、trueになります。
- 左の式がtrueの場合、右の条件は判定(評価)されません。これを短絡評価(ショートサーキット評価 : Short-Circuit Evaluation)といいます。
a = 1
if a == 0 or a < 10 then
print("OK") -- OKと出力される
end
左の条件はfalseですが右の条件がtrueなのでtrueになります。
論理否定(not) Aではない
| if not (条件) |
- 条件がfalseの場合にtrueになります。
- 条件がtrueの場合にfalseになります。
a = 1
if not(a == 2) then
print("OK") -- OKと出力される
end
if not(a == 1) then
print("OK")
else
print("NG") -- NGと出力される
end
3行目は値の比較でfalseになり、それをnotで否定するのでtrueになります。
7行目は値の比較でtrueになり、それをnotで否定するのでfalseになります。
よく使うパターン
Luaのand/orは、値を返す動きをします。
or (デフォルト値を設定)
local name = input or "guest"
inputの値がnil / falseの場合、nameの値はguestになります。
andとor
local result = (a > 0) and "positive" or "negative"
意味:aが0より大きければ"positive"、それ以外は"negative"を返す。
※andの返す値がfalse/nilになる場合は使えません
local result = true and false or "fallback"
-- resultは"fallback"になる(falseを期待しているのに)
デフォルト値
local timeout = config.timeout or 30
テーブルのconfig.timeoutがあればそれを使います。
ない場合は、30を使用します。
config = {
timeout = 10
}
フラグチェック
if not config.enabled then
return
end
テーブルのconfig.enabledがfalseまたはnilなら処理を中断します。
returnで後続の処理をさせずに終了するイメージです。
config = {
enabled = true -- 有効
}
範囲チェック
if value >= 0 and value <= 100 then
存在チェック
if obj and obj.name then
print(obj.name)
end
テーブルのobjが存在して、さらにnameもあればprint処理を行うという意味です。
obj = {
name = "Taro"
}
2つの存在チェックをしています。
1.obj が存在するか(nilでないか)→nilチェック
2.obj.nameが存在するか→プロパティの取得
if obj.nameのみの場合、objがnilだとエラーになってしまいます。
if obj and obj.nameの場合、objがnilの場合そこで止まるため安全です。
関連の記事
Lua for文 処理を繰り返すサンプル(break/continue)
Lua while文のサンプル(break/continue)
