C# uses if, if...else, and if...else if statements to execute different blocks of code based on Boolean conditions. Each condition must evaluate to either true or false.

Use a simple if statement when code should run only when a condition is true. Use if...else when one block should run for a true condition and another block for a false condition. Use an if...else if chain when several mutually exclusive conditions must be checked in order.

C# if Statement

An if statement evaluates a Boolean expression. When the expression evaluates to true, the statements inside the if block are executed. When it evaluates to false, the block is skipped.

C# if Statement Syntax

The syntax of C# If statement is:

</>
Copy
 if(boolean_expression) {
     /* statement(s) */
 }

The boolean_expression inside the parentheses must evaluate to the C# Boolean value true or false. If it evaluates to true, the statements inside the block are executed. If it evaluates to false, those statements are skipped.

Although braces are optional when an if block contains only one statement, using braces consistently makes the scope clear and reduces errors when more statements are added later.

C# if Statement with Independent Conditions

In the following example, three independent if statements evaluate three different conditions. Because they are separate statements, every condition is checked.

Program.cs

</>
Copy
using System;

namespace CSharpExamples
{
    class Program
    {
        static void Main(string[] args)
        {
            int a=1;
            int b=2;

            if(a==1){
                Console.WriteLine("a is 1.");    
            }

            if(a==b){
                Console.WriteLine("a is equal to b.");    
            }

            if(a!=b){
                Console.WriteLine("a is not equal to b.");    
            }
        }
    }
}

Output

PS D:\workspace\csharp\HelloWorld> dotnet run
a is 1.
a is not equal to b.

How the Independent C# if Conditions Are Evaluated

There are three independent if blocks here.

In the first if block, the condition a==1 evaluates to true. Therefore, the statements inside that if block are executed.

In the second if block, the condition a==b evaluates to false, so the statements inside that block are skipped.

In the third if block, the condition a!=b evaluates to true. Therefore, the statements inside that block are executed.

C# if…else Statement

An if...else statement selects between two alternatives. The if block runs when the condition is true; otherwise, the else block runs.

C# if…else Syntax

</>
Copy
if (condition)
{
    // Runs when condition is true
}
else
{
    // Runs when condition is false
}

C# if…else Example for Pass or Fail

The following example prints one message when the score is at least 40 and another message when it is below 40.

</>
Copy
using System;

class Program
{
    static void Main()
    {
        int score = 72;

        if (score >= 40)
        {
            Console.WriteLine("Pass");
        }
        else
        {
            Console.WriteLine("Fail");
        }
    }
}

Output

Pass

Only one of the two blocks can run. Because score >= 40 is true, the program prints Pass and skips the else block.

C# if…else if Statement

An if...else if chain checks several conditions from top to bottom. As soon as one condition evaluates to true, its block is executed and the remaining conditions in the chain are skipped.

Program.cs

</>
Copy
using System;

namespace CSharpExamples
{
    class Program
    {
        static void Main(string[] args)
        {
            int a=1;

            if(a==0){
                Console.WriteLine("a is 0.");    
            } else if(a==1){
                Console.WriteLine("a is 1.");    
            } else if(a==2){
                Console.WriteLine("a is 2.");    
            }
        }
    }
}

Output

PS D:\workspace\csharp\HelloWorld> dotnet run
a is 1.

How the C# if…else if Chain Selects a Branch

In the above example, the first condition a==0 is checked and evaluates to false. Control then moves to the next condition, a==1, which evaluates to true. Its block is executed, and the remaining else if condition is not checked.

The example has no final else block. Therefore, if none of the conditions were true, the entire chain would complete without printing anything.

C# if…else if…else Syntax

Add a final else block when one branch must run even if none of the earlier conditions match.

</>
Copy
if (condition1)
{
    // Runs when condition1 is true
}
else if (condition2)
{
    // Runs when condition1 is false and condition2 is true
}
else
{
    // Runs when all previous conditions are false
}

Independent if Statements vs an if…else if Chain in C#

Separate if statements and an if...else if chain do not behave the same way.

C# conditional structureHow conditions are evaluatedWhen to use it
Separate if statementsEvery condition is evaluated independently.Use when more than one block may need to run.
if...else if chainConditions are checked in order until the first true condition is found.Use when only one matching branch should run.
if...elseExactly one of two branches runs.Use for a two-way decision.

For example, a program may use separate if statements to report every property that applies to a number, such as whether it is positive, even, and less than 100. It should use an if...else if chain when assigning exactly one grade or category.

C# Conditions with Comparison and Logical Operators

C# conditional statements commonly use comparison operators to produce Boolean results.

OperatorMeaningExample
==Equal toa == b
!=Not equal toa != b
>Greater thanscore > 50
<Less thanage < 18
>=Greater than or equal tomarks >= 40
<=Less than or equal totemperature <= 0

Logical operators combine or reverse Boolean expressions.

  • && returns true when both conditions are true.
  • || returns true when at least one condition is true.
  • ! reverses a Boolean value.
</>
Copy
int age = 22;
bool hasTicket = true;

if (age >= 18 && hasTicket)
{
    Console.WriteLine("Entry allowed.");
}

The block runs only when both age >= 18 and hasTicket evaluate to true.

Nested if Statements in C#

A nested if statement places one conditional statement inside another. Use nesting when the second condition should be evaluated only after the first condition succeeds.

</>
Copy
using System;

class Program
{
    static void Main()
    {
        bool isLoggedIn = true;
        bool isAdmin = false;

        if (isLoggedIn)
        {
            Console.WriteLine("User is logged in.");

            if (isAdmin)
            {
                Console.WriteLine("Admin access granted.");
            }
        }
    }
}

Output

User is logged in.

The inner condition is checked because isLoggedIn is true. However, the inner block does not run because isAdmin is false.

C# Conditional Statement Mistakes to Avoid

  • Using = instead of ==: = assigns a value, while == compares two values for equality.
  • Writing True or False as C# literals: C# Boolean literals are lowercase: true and false.
  • Using separate if statements for mutually exclusive choices: use an if...else if chain when only one branch should execute.
  • Placing broad conditions before specific conditions: in an else if chain, an earlier broad condition may prevent a later specific condition from being reached.
  • Omitting braces in complex code: braces make block boundaries clear and help prevent unintended statements from falling outside a condition.

C# if, if…else, and if…else if FAQs

Must a C# if condition return a Boolean value?

Yes. The condition must evaluate to a value of type bool. Unlike some languages, C# does not treat arbitrary nonzero numbers or non-null objects as true conditions.

Can more than one block run in a C# if…else if chain?

No. The chain stops after the first true condition, so at most one branch runs. If none of the conditions are true, only the optional final else block runs.

Can a C# if statement be written without braces?

Yes, when the block contains a single statement. However, braces are generally clearer and safer, especially when code may be modified later.

What is the difference between two if statements and if followed by else if?

With two independent if statements, both conditions are evaluated and both blocks may run. With if followed by else if, the second condition is checked only when the first condition is false.

C# Conditional Statements Summary

Use if to run code only when a condition is true, if...else for a two-way decision, and if...else if when several mutually exclusive conditions must be checked in order. Separate if statements may execute multiple blocks, while an if...else if chain executes only its first matching branch.

In this C# Tutorial, we learned the syntax and usage of decision making statements: If, If-Else and If-Else-If.