if文 if-else文
if文は、選択が必要な時に記述する文です。形式的な構文は次の通りです。
if (条件式) 処理文1 else 処理文2
式がtrueの場合、処理文1が実行され、処理文2は実行されません。式がfalseの場合、処理文2が実行されます。
if文のelse部分は省略可能です。ネストされた(入れ子の)if文でのelseは、最も近いelseを持たないif文とつながります。
サンプルコード
if (true)
if (true)
if (true) Print("all true");
else Print("3rd false");
else Print("2nd false");
else Print("1st false");
// 上記コードに改行とインデントを付けたコード
if (true) // if文1
if (true) // if文2
if (true) // if文3
Print("all true");
else // if文3のelse
Print("3rd false");
else // if文2のelse
Print("2nd false");
else // if文1のelse
Print("1st false");
elseの処理文がif文またはif-else文の単文の場合は、次のようにelse ifとしても同じになります。
if (false)
{
Print("1st true");
}
else
{
if (false)
{
Print("2nd true");
}
else
{
Print("all false");
}
}
// 上記コードをelse ifで書いたコード
if (false)
{
Print("1st true");
}
else if (false)
{
Print("2nd true");
}
else
{
Print("all false");
}

