Share via


if-else (C# Reference)

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

bool result = true;

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

If the expression in the parenthesis is evaluated to be true, then the Console.WriteLine("The variable is set to true."); 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:


int x = 12;
int y = 18;

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

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.


    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;
            }

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.

    public class IfTest3
{
    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.");
        }
    }
}
    /*
        Sample 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 C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See Also

Reference

C# Keywords

?: Operator (C# Reference)

if-else Statement (C++)

switch (C# Reference)

Concepts

C# Programming Guide

Other Resources

C# Reference