C# if

Summary: in this tutorial, you’ll learn how to use the C# if statement to execute a code block based on a condition.

Introduction to the C# if statement

The if statement evaluates a condition and executes one or more statements if the result is true. Otherwise, the if statement passes the control to the statement after it.

The following illustrates the syntax of the if statement:

if (condition)
    statement;Code language: C# (cs)

In this syntax, if the condition evaluates to true, then if statement executes the statement.

If you want to execute multiple statements, you need to use a block like this:

if (expression) {
    // statements
}Code language: C# (cs)

However, it’s a good practice to always use a block with the if statement even though it has a simple statement.

The following flowchart illustrates how the C# if statement works:

C# if statement examples

Let’s take some examples of using the if statement.

1) A simple C# if statement example

The following example uses the if statement to show a message when the condition is sunny:

string condition = "sunny";

if (condition == "sunny")
{
    Console.WriteLine("Let's go outside.");
}Code language: C# (cs)

Output:

Let's go outside.Code language: C# (cs)

How it works.

  • First, declare a string variable condition with the initial value "sunny".
  • Second, check if the condition variable equals "sunny" and display the message "Let's go outside".

2) C# if statement with condition evaluates to false

The following example doesn’t output anything because the condition in the if statement evaluates to false:

string condition = "sunny";

if (condition == "rainy")
{
    Console.WriteLine("Stay home");
}Code language: C# (cs)

3) C# if statement example with a complex condition

In practice, the condition is more complex, which consists of multiple expressions with operators like this:

string condition = "sunny";
bool free = true;

if (condition == "sunny" && free)
{
    Console.WriteLine("Let's go outside.");
}Code language: C# (cs)

Output:

Let's go outside.Code language: C# (cs)

Nested C# if statement

C# allows you to nest if statements inside an if statement. The following example illustrates how to nest if statements inside another if statement:

string condition = "rainy";
bool free = true;

if (free)
{
    if (condition== "sunny")
    {
        Console.WriteLine("Let's go outside.");
    }

    if (condition == "rainy")
    {
        Console.WriteLine("Just stay home.");
    }
}Code language: C# (cs)

Output:

Just stay home.Code language: C# (cs)

How it works.

  • First, declare the condition and free variables and initialize their values to "sunny" and true respectively.
  • Second, check if the free is true in the if statement. Since the free is true, the if statement executes the statement inside its block.
  • Third, check if the condition is "sunny" in the first nested if statement. Because the condition is "rainy", the first nested if statement does nothing.
  • Finally, check if the condition is "rainy" in the second nested if statment. Since the condition is "rainy", the if statement outputs the message "Just stay home." to the console.

In practice, you should avoid the nesting of the if statements as much as possible. Otherwise, the code will become difficult to read.

For example, you can flatten the example above by using two if statements as follows:

string condition = "rainy";
bool free = true;

if (free && condition == "sunny")
{
    Console.WriteLine("Let's go outside.");
}

if (free && condition == "rainy")
{
    Console.WriteLine("Just stay home.");
}Code language: C# (cs)

Summary

  • Use the C# if statement to execute one or more statements when a condition is true.
  • Avoid nesting if statements as much as possible to make the code more readable.
Was this tutorial helpful ?