DevAcademia
C++C#CPythonJava
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction

Loading Cs tutorial…

Loading content
C# BasicsTopic 27 of 55
←PreviousPrevNextNext→

C# else if Statement - Complete Guide

Introduction to else if Statements

The else if statement in C# lets you check multiple conditions in sequence. It is used when the initial if condition is false but you still want to test other conditions before reaching the final else block.

else if statements help handle multiple scenarios without writing deeply nested if statements, improving both readability and maintainability.

Basic else if Statement Syntax

else if statements are placed between if and else blocks to test additional conditions:

Example
using System;

namespace ElseIfExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Basic else if structure
            int number = 7;
            
            if (number > 10)
            {
                Console.WriteLine("Number is greater than 10");
            }
            else if (number > 5)
            {
                Console.WriteLine("Number is greater than 5 but not greater than 10");
            }
            else
            {
                Console.WriteLine("Number is 5 or less");
            }
            
            // Multiple else if statements
            int score = 85;
            
            if (score >= 90)
            {
                Console.WriteLine("Grade: A");
            }
            else if (score >= 80)
            {
                Console.WriteLine("Grade: B");
            }
            else if (score >= 70)
            {
                Console.WriteLine("Grade: C");
            }
            else if (score >= 60)
            {
                Console.WriteLine("Grade: D");
            }
            else
            {
                Console.WriteLine("Grade: F");
            }
            
            // String comparisons with else if
            string day = "Wednesday";
            
            if (day == "Monday")
            {
                Console.WriteLine("Start of the work week");
            }
            else if (day == "Tuesday")
            {
                Console.WriteLine("Second day of the week");
            }
            else if (day == "Wednesday")
            {
                Console.WriteLine("Midweek");
            }
            else if (day == "Thursday")
            {
                Console.WriteLine("Almost Friday");
            }
            else if (day == "Friday")
            {
                Console.WriteLine("Weekend is near");
            }
            else
            {
                Console.WriteLine("It's the weekend!");
            }
            
            // Complex conditions with logical operators
            int temperature = 22;
            bool isSunny = true;
            
            if (temperature > 30 && isSunny)
            {
                Console.WriteLine("Very hot and sunny");
            }
            else if (temperature > 20 && isSunny)
            {
                Console.WriteLine("Warm and sunny");
            }
            else if (temperature > 20 && !isSunny)
            {
                Console.WriteLine("Warm but cloudy");
            }
            else if (temperature <= 20 && isSunny)
            {
                Console.WriteLine("Cool but sunny");
            }
            else
            {
                Console.WriteLine("Cool and cloudy");
            }
        }
    }
}
Output
Number is greater than 5 but not greater than 10
Grade: B
Midweek
Warm and sunny

Order of Evaluation in else if Chains

else if conditions are checked in order, and only the first true condition runs. The remaining conditions are skipped, even if they are also true.

Example
using System;

namespace EvaluationOrderExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int value = 15;
            Console.WriteLine("Testing evaluation order:");

            if (value > 20)
            {
                Console.WriteLine("Value is greater than 20");
            }
            else if (value > 10)
            {
                Console.WriteLine("Value is greater than 10 (this will execute)");
            }
            else if (value > 5)
            {
                Console.WriteLine("Value is greater than 5 (this won't execute)");
            }
            else
            {
                Console.WriteLine("Value is 5 or less");
            }

            int testScore = 75;
            Console.WriteLine("\nTesting condition order importance:");

            // Wrong order
            if (testScore >= 60)
            {
                Console.WriteLine("Grade: D or better (wrong order)");
            }
            else if (testScore >= 70)
            {
                Console.WriteLine("Grade: C or better (skipped)");
            }

            // Correct order
            if (testScore >= 90)
            {
                Console.WriteLine("Grade: A (correct order)");
            }
            else if (testScore >= 80)
            {
                Console.WriteLine("Grade: B (correct order)");
            }
            else if (testScore >= 70)
            {
                Console.WriteLine("Grade: C (correct order)");
            }
            else if (testScore >= 60)
            {
                Console.WriteLine("Grade: D (correct order)");
            }
            else
            {
                Console.WriteLine("Grade: F (correct order)");
            }

            int x = 25;
            int y = 15;
            if (x > 20)
            {
                Console.WriteLine("x is greater than 20");
                if (y > 10)
                {
                    Console.WriteLine("y is also greater than 10");
                }
            }

            int number = 8;
            if (number % 2 == 0)
            {
                Console.WriteLine("Number is even");
            }
            else if (number % 3 == 0)
            {
                Console.WriteLine("Number is divisible by 3");
            }
            else if (number > 5)
            {
                Console.WriteLine("Number is greater than 5");
            }
        }
    }
}
Output
Testing evaluation order:
Value is greater than 10 (this will execute)

Testing condition order importance:
Grade: D or better (wrong order)
Grade: C (correct order)
x is greater than 20
y is also greater than 10
Number is even
Test your knowledge: C# else if Statement - Complete Guide
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
C# BasicsTopic 27 of 55
←PreviousPrevNextNext→