共用方式為


if-else (C# Reference) 

The if statement selects a statement for execution based on the value of a Boolean expression. In the following example a Boolean flag flagCheck is set to true and then checked in the if statement. The output is: The flag is set to true.

bool flagCheck = true;
if (flagCheck == true)
{
    Console.WriteLine("The flag is set to true.");
}
else 
{
    Console.WriteLine("The flag is set to false.");
}

Remarks

If the expression in the parenthesis is evaluated to be true, then the Console.WriteLine("The boolean flag is set to ture."); statement is executed. After executing the if statement, control is transferred to the next statement. The else is not executed in this example.

If you wish to execute more than one statement, multiple statements can be conditionally executed by including them into blocks using {} as in the example above.

The statement(s) to be executed upon testing the condition can be of any kind, including another if statement nested into the original if statement. In nested if statements, the else clause belongs to the last if that does not have a corresponding else. For example:

if (x > 10)
    if (y > 20)
        Console.Write("Statement_1");
    else
        Console.Write("Statement_2");

In this example, Statement_2 will be displayed if the condition (y > 20) evaluates to false. However, if you want to associate Statement_2 with the condition (x >10), use braces:

if (x > 10) 
{
    if (y > 20) 
        Console.Write("Statement_1");
}
else
    Console.Write("Statement_2");

In this case, Statement_2 will be displayed if the condition (x > 10) evaluates to false

Example 1

In this example, you enter a character from the keyboard and the program checks if the input character is an alphabetic character. If so, it checks if it is lowercase or uppercase. In each case, the proper message is displayed.

// statements_if_else.cs
// if-else example
using System;
class IfTest
{
    static void Main()
    {
        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("Not an alphabetic character.");
        }
    }
}

Input

2

Sample Output

Enter a character: 2
The character is not an alphabetic character.

Additional sample might look as follows:

Run #2:

Enter a character: A
The character is uppercase.

Run #3:

Enter a character: h
The character is lowercase.

It is also possible to extend the if statement to handle multiple conditions using the following else-if arrangement:

if (Condition_1)
{
    // Statement_1;
}
else if (Condition_2)
{
    // Statement_2;
}
else if (Condition_3)
{
    // Statement_3;
}
else
{
    // Statement_n;
}

Example 2

This example checks if the input character is lowercase, uppercase, or a number. Otherwise, it is not an alphanumeric character. The program makes use of the else-if ladder.

// statements_if_else2.cs
// else-if
using System;
public class IfTest
{
    static void Main()
    {
        Console.Write("Enter a character: ");
        char c = (char)Console.Read();

        if (Char.IsUpper(c))
        {
            Console.WriteLine("Character is uppercase.");
        }
        else if (Char.IsLower(c))
        {
            Console.WriteLine("Character is lowercase.");
        }
        else if (Char.IsDigit(c))
        {
            Console.WriteLine("Character is a number.");
        }
        else
        {
            Console.WriteLine("Character is not alphanumeric.");
        }
    }
}

Input

E

Sample Output

Enter a character: E
The character is uppercase.

Additional sample runs might look as follows:

Run #2

Enter a character: e
The character is lowercase.

Run #3:

Enter a character: 4
The character is a number.

Run #4:

Enter a character: $
The character is not alphanumeric.

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 5.3.3.5 If statements

  • 8.7.1 The if statement

See Also

Reference

C# Keywords
?: Operator (C# Reference)
The if-else Statement
switch (C# Reference)

Concepts

C# Programming Guide

Other Resources

C# Reference