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