C# if-else Statements with Examples

How to Use the if-else Statements in C#

This tutorial describes how to use the if-else statement in C# programming language.

An if statement decides which block of code to execute based on a specified condition. If the specified condition evaluates to true, then code inside the if statement block runs. If the specified condition is false, code inside the else statement block runs.

Related C# tutorials:

The if-else Syntax

The syntax of the if statement that doesn't include the else statement is as follows:

if (condition)
{
    //code
}

In an if-statementcode runs if condition evaluates to true. If condition is false, control moves the next statement after the if-statement.

The syntax of the if statement that includes the else statement is as follows:

if (condition)
{
    //code1
}
else
{
    //code2
}

In an if-else statementcode1 runs if condition evaluates to true. If the condition is false, code2 runs.

The following syntax includes else if and else statements:

if (condition)
{
      //code1
}
else if(condition2)
{
      //code2
}
else
{
      //code3
}

If the condition1 evaluates to true, code1 runs. If the condition1 is false and condition2 evaluates to true, code2 runs. If both condition1 and condition evaluate to false, code3 runs.

Note that codecode1code2, or code3 can consist of a single statement or multiple statements.

The following is some examples of the condition of an if-statement:

if (number == 10)
{
}
if (number >= 10 || number <= 20)
{
}
if (fruit == "Apple")
{
}

Examples

Example 1

The following example shows how to use an if statement without an else statement block:

using System;
 
namespace IfElseExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "John";
            if (name == "John")
            {
                Console.WriteLine("Good morning!");
            }
            if (name == "James")
            {
                Console.WriteLine("Good afternoon!");
            }
        }
 
    }
}
 

Output:

Good morning!

In the first if-statement, the program outputs "Good morning!" because the condition name == "John" evaluates to true. However, in the second if -statement, the program outputs nothing because the condition name == "James" is false.

Example 2

The following example shows how to use an if-else statement:

using System;
 
namespace IfElseExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "James";
            if (name == "John")
            {
                Console.WriteLine("Good morning!");
            }
            else
            {
                Console.WriteLine("Good afternoon!");
            }
        }
 
    }
}

Output:

Good afternoon!

The program outputs "Good afternoon!" because the condition in the if-statement block evaluates to false, and the code inside the else statement block runs.

Example 3

You can also use the if-elseif-else and nested if-else statements, as the following example shows:

using System;
 
namespace IfElseExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 10;
            if (number <= 5)
            {
                Console.WriteLine("The number is less than or equato 5.");
            }
            else if (number >= 6 && number <= 9)
            {
                Console.WriteLine("The numbr is between 6 and 9.");
            }
            else if(number > 20)
            {
                Console.WriteLine("The number is greater than 20.");
            }
            else
            {
                if (number > 10)
                {
                    Console.WriteLine("The number is greater than 10 and less than 20.");
                }
                else
                {
                    Console.WriteLine("The number is " + number + ".");
                }
            }
        }
    }
}
 

Output:

The number is 10.

Learn more:

In this tutorial, you have learned how to use an if statementif-else statementif-elseif-else statement, and nested if-else statement.


See also:
C# List Contains with Examples
C# typeof with Examples
C# TryParse (int, double, float) with Examples
C# Data Types
C# Variables