if-else (C# リファレンス)

実行するステートメントが Boolean の式の値に基づいて if のステートメントを指定します。次の例では、Boolean 変数 result が true に設定され、if ステートメントで検証されます。出力は The condition is true になります。

bool condition = true;

if (condition)
{
    Console.WriteLine("The variable is set to true.");
}
else
{
    Console.WriteLine("The variable is set to false.");
}

コンソール アプリケーションの Main のメソッドに入ることによって、このトピックの例を実行できます。

、C の if のステートメントは、次の例に示すように、2 種類の形式があります。

// if-else statement
if (condition)
{
    then-statement;
}
else
{
    else-statement;
}
// Next statement in the program.


// if statement without an else
if (condition)
{
    then-statement;
}
// Next statement in the program.

if-else のステートメントでは、condition が true の場合 then-statement が実行されます。condition が false の場合、else-statement が実行されます。condition が同時に true と false であることはできないので、if-else のステートメントの then-statement と else-statement は、両方の実装はできません。then-statement か else-statement を実行すると、コントロールは if のステートメントの後に次のステートメントに移動します。

else のステートメントが含まれていない if のステートメントでは condition が true の場合、then-statement が実行されます。condition が false の場合、コントロールは if のステートメントの後に次のステートメントに移動します。

中かっこ ({}) で囲んだ then-statement と else-statement または複数のステートメントは、単一のステートメントで構成できます。単一のステートメントの場合、中かっこはオプションお勧めします。

then-statement と else-statement のステートメント if 元のステートメントの中に入れ子になった if にステートメントを含む型である場合もあります。if の入れ子になったステートメントで、対応する elseがない else 各句は、最後の if に属しています。次の例では、Result1 は m > 10 および n > 20 の両方が true と評価された場合に表示されます。m > 10 が true の場合は、n > 20 が false の場合、Result2 が表示されます。

// Try with m = 12 and then with m = 8.
int m = 12;
int n = 18;

if (m > 10)
    if (n > 20)
    {
        Console.WriteLine("Result1");
    }
    else
    {
        Console.WriteLine("Result2");
    }

代わり (m > 10) が false の場合に Result2 に表示する場合は、名前付けを次の例として if の入れ子になったステートメントの開始と終了を確立するために中かっこを使用して関連付けをことを指定できます。

// Try with m = 12 and then with m = 8.
if (m > 10)
{
    if (n > 20)
        Console.WriteLine("Result1");
}
else
{
    Console.WriteLine("Result2");
}

Result2 は (m > 10) 条件が false と評価された場合に表示されます。

使用例

次の例では、キーボードから文字を入力し、プログラムは入力文字がアルファベットかどうかを確認するには if の入れ子になったステートメントを使用します。入力文字が大文字または小文字であるかどうかを入力文字がアルファベット、プログラムのチェックの場合は。メッセージは、各ケースに表示されます。

Console.Write("Enter a character: ");
char c = (char)Console.Read();
if (Char.IsLetter(c))
{
    if (Char.IsLower(c))
    {
        Console.WriteLine("The character is lowercase.");
    }
    else
    {
        Console.WriteLine("The character is uppercase.");
    }
}
else
{
    Console.WriteLine("The character isn't an alphabetic character.");
}

//Sample Output:

//Enter a character: 2
//The character isn't an alphabetic character.

//Enter a character: A
//The character is uppercase.

//Enter a character: h
//The character is lowercase.

次の部分的なコードに示すように、他のブロック内の if のステートメントに入れ子にできます。2 つが他のブロックと 1 個の中の例の入れ子の if のステートメントは、ブロック。コメントは、各ブロックで条件が true か false かを指定します。

// Change the values of these variables to test the results.
bool Condition1 = true;
bool Condition2 = true;
bool Condition3 = true;
bool Condition4 = true;

if (Condition1)
{
    // Condition1 is true.
}
else if (Condition2)
{
    // Condition1 is false and Condition2 is true.
}
else if (Condition3)
{
    if (Condition4)
    {
        // Condition1 and Condition2 are false. Condition3 and Condition4 are true.
    }
    else
    {
        // Condition1, Condition2, and Condition4 are false. Condition3 is true.
    }
}
else
{
    // Condition1, Condition2, and Condition3 are false.
}

次の例では、入力文字が大文字、小文字、数字であるかどうかを判定します。3 種類の条件がすべて false の場合、文字は英数字ではありません。例では、各ケースのメッセージを表示します。

Console.Write("Enter a character: ");
char ch = (char)Console.Read();

if (Char.IsUpper(ch))
{
    Console.WriteLine("The character is an uppercase letter.");
}
else if (Char.IsLower(ch))
{
    Console.WriteLine("The character is a lowercase letter.");
}
else if (Char.IsDigit(ch))
{
    Console.WriteLine("The character is a number.");
}
else
{
    Console.WriteLine("The character is not alphanumeric.");
}

//Sample Input and Output:
//Enter a character: E
//The character is an uppercase letter.

//Enter a character: e
//The character is a lowercase letter.

//Enter a character: 4
//The character is a number.

//Enter a character: =
//The character is not alphanumeric.

他のブロックまたはブロックとブロックのステートメントが、有効なステートメントで使用できるように条件に対して有効なブール式を使用できます。&&&||などの論理演算子を使用できます。|そして ! と複合条件を作ります。次のコード例を示します。

// NOT
bool result = true;
if (!result)
{
    Console.WriteLine("The condition is true (result is false).");
}
else
{
    Console.WriteLine("The condition is false (result is true).");
}

// Short-circuit AND
int m = 9;
int n = 7;
int p = 5;
if (m >= n && m >= p)
{
    Console.WriteLine("Nothing is larger than m.");
}

// AND and NOT
if (m >= n && !(p > m))
{
    Console.WriteLine("Nothing is larger than m.");
}

// Short-circuit OR
if (m > n || m > p)
{
    Console.WriteLine("m isn't the smallest.");
}

// NOT and OR
m = 4;
if (!(m >= n || m >= p))
{
    Console.WriteLine("Now m is the smallest.");
}
// Output:
// The condition is false (result is true).
// Nothing is larger than m.
// Nothing is larger than m.
// m isn't the smallest.
// Now m is the smallest.

C# 言語仕様

詳細については、「C# 言語仕様」を参照してください。言語仕様は、C# の構文と使用法に関する信頼性のある情報源です。

参照

関連項目

C# のキーワード

?: 演算子 (C# リファレンス)

他のステートメント (C++)

switch (C# リファレンス)

概念

C# プログラミング ガイド

その他の技術情報

C# リファレンス