C# If...Else Tutorial For Beginners With Examples

C# has provided conditional statements to allow decision making in a program.
These conditional statements allows a program to execute different sets of code based on certain conditions.

Different varieties of if-else construct is used to route the logic of a program in different paths. The following varieties of if-else are discussed below:
  • if statement
  • if...else statement
  • else...if statement
  • Nested if statements
  • Nested if...else statements
  • switch statement
  • Nested switch statements

C# if statement

The if statement is used for decision making in a C# program. The code inside an if block executes only if a condition is true.
In the below example the code inside the if block will get executed as variable y is greater thanx.
    int x = 2;
    int y = 3;

    if (y > x)
    {
        Console.WriteLine("Inside if block");
    }
        

C# if...else statement

Similar to if statement the if...else statement also helps in decision making in a C# program. In the case of if...else if the condition evaluates to false then the code inside else block is executed.
In the below example the code inside the else block will get executed as the statement x > y will evaluate to false.
    int x = 2;
    int y = 3;

    if (x > y)
    {
        Console.WriteLine("Inside if block");
    }
    else
    {
       Console.WriteLine("Inside else block");
    }
        
In a C# program instead of if...else statements ternary operators can also be used to take decisions.

C# else...if statements

While writing a program a need may arise to evaluate multiple conditions out of which only one may be true.
Let's say there are 'n' number of conditions and any one of these conditions may become true then in that case else...if statements can be used.
        
    int x = 1;
    int y = 2;
    int z = 3;
    int p = 1;            

    if (x == y)
        Console.WriteLine("Variable x is equal to y");
    else if (x == z)
        Console.WriteLine("Variable x is equal to z");
    else if (x == p)
        Console.WriteLine("Variable x is equal to p");
    else
        Console.WriteLine("Variable x is not equal to any other variable");        
        

C# Nested if statements

In a program when evaluating multiple conditions there may be a possibility that more than one condition may become true. In these scenarios nested if statements are useful.
An if statement can be nested within another if statement or an else statement.
Below is an example that shows nesting can add powerful decision making capabilities.
    int a = 2;
    int b = 3;
    int c = 1;

    if (c > b)
    {
        if (b > a)
        {
            Console.WriteLine("Variable a has minimum value");
        }
    }
    else
    {
        if (c < a)
        {
            Console.WriteLine("Variable c has minimum value");
        }        
    }
        
Logical operators like &&||! can also be used to create compound conditions.

C# Nested if...else statements

As discussed before when two different set of code needs to be executed based on a condition's evaluation then if...else statements are used.
However, based on the results of the initial evaluation if there is a need to evaluate multiple other conditions then nested if...else statements can be used.
An example on the nested if...else statements is shown below:
    int x = 1;            
    int z = 3;
    int p = 1;

    if (x == p)
    {
        if (p==z)
            Console.WriteLine("Variable x, p and z are equal");
        else
            Console.WriteLine("Only variable x and p are equal");
    }                
    else
    {
        if (x == z)
            Console.WriteLine("Variable x is equal to z");
        else
            Console.WriteLine("Variable x is is not equal to any other variable");
    }        
        
The switch statement and nested switch statement will be discussed in next chapter.