What are the different types of conditional statements supported by C#?

Conditional statements in C# allow programs to execute different code blocks based on specified conditions. These statements evaluate boolean expressions and direct program flow accordingly, enabling dynamic decision-making in applications.

Types of Conditional Statements

Statement Description
if statement Executes a block of code when a boolean expression is true.
if...else statement Executes one block if the condition is true, another if false.
nested if statements Uses one if or else if statement inside another for complex conditions.
switch statement Tests a variable against multiple values and executes matching case.
nested switch statements Uses one switch statement inside another switch statement.

Syntax

Following is the basic syntax for different conditional statements −

// if statement
if (condition) {
   // statements
}

// if...else statement
if (condition) {
   // statements if true
} else {
   // statements if false
}

// switch statement
switch (variable) {
   case value1:
      // statements
      break;
   case value2:
      // statements
      break;
   default:
      // default statements
      break;
}

Using if Statement

Example

using System;

class Program {
   public static void Main() {
      int number = 10;
      
      if (number > 5) {
         Console.WriteLine("Number is greater than 5");
      }
      
      if (number % 2 == 0) {
         Console.WriteLine("Number is even");
      }
   }
}

The output of the above code is −

Number is greater than 5
Number is even

Using if...else Statement

Example

using System;

class Program {
   public static void Main() {
      int score = 75;
      
      if (score >= 60) {
         Console.WriteLine("Pass");
      } else {
         Console.WriteLine("Fail");
      }
      
      // Multiple conditions with else if
      if (score >= 90) {
         Console.WriteLine("Grade: A");
      } else if (score >= 80) {
         Console.WriteLine("Grade: B");
      } else if (score >= 70) {
         Console.WriteLine("Grade: C");
      } else {
         Console.WriteLine("Grade: D");
      }
   }
}

The output of the above code is −

Pass
Grade: C

Using switch Statement

Example

using System;

class Program {
   public static void Main() {
      char grade = 'B';
      
      switch (grade) {
         case 'A':
            Console.WriteLine("Excellent!");
            break;
         case 'B':
            Console.WriteLine("Good job!");
            break;
         case 'C':
            Console.WriteLine("Well done");
            break;
         case 'D':
            Console.WriteLine("You passed");
            break;
         case 'F':
            Console.WriteLine("Better try again");
            break;
         default:
            Console.WriteLine("Invalid grade");
            break;
      }
   }
}

The output of the above code is −

Good job!

Using Nested switch Statement

Example

using System;

class Program {
   public static void Main() {
      int a = 100;
      int b = 200;
      
      switch (a) {
         case 100:
            Console.WriteLine("This is part of outer switch");
            
            switch (b) {
               case 200:
                  Console.WriteLine("This is part of inner switch");
                  break;
               case 300:
                  Console.WriteLine("Inner case 300");
                  break;
               default:
                  Console.WriteLine("Inner default case");
                  break;
            }
            break;
         case 200:
            Console.WriteLine("Outer case 200");
            break;
         default:
            Console.WriteLine("Outer default case");
            break;
      }
   }
}

The output of the above code is −

This is part of outer switch
This is part of inner switch

Comparison of Conditional Statements

Statement Best Use Case Performance
if statement Simple boolean conditions Fast for few conditions
if...else if Multiple related conditions Good for complex logic
switch statement Testing single variable against many values Optimized for many cases

Conclusion

C# provides several conditional statements including if, if...else, nested if, switch, and nested switch statements. Choose if statements for boolean conditions and switch statements when testing a single variable against multiple discrete values. Proper use of these statements creates clean, readable, and efficient code flow control.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements