Summary: in this tutorial, you’ll learn how to use the C# if else if statement to check multiple conditions and execute a block if a condition is true.
Introduction to the C# if else if statement
The if
statement checks one condition and executes a block if the condition is true
.
And the if else
statement checks one condition and executes a block if the condition is true or another block otherwise.
Both the if
and if else
statements check only one condition.
Sometimes, you may want to check multiple conditions and execute a block if a condition is true
. To do that, you can use the if else if
statement.
Here’s the syntax of the if else if
statement:
if (condition1)
{
// block 1
}
else if (condition2)
{
// block 2
}
else if (condition3)
{
// block 3
}
else
{
// else block
}
Code language: C# (cs)
In this syntax:
- The
if else if
statement can have multipleelse if
clause where each clause has a condition. - The
if else if
statement checks thecondition1
,condition2
, … from top to bottom sequentially. If a condition istrue
, the corresponding block executes. The statement will stop evaluating the remaining conditions. - If no condition is
true
, the block in theelse
clause executes. Theelse
clause is optional.
The following flowchart illustrates how the if else if
statement works:
C# if else if examples
Let’s take some examples of using the if else if
statement.
1) Simple C# if else if statement example
The following example shows how to use the if else if
statement to display the day name based on a day number entered by users:
string dayName;
if (day == 1)
{
dayName = "Sunday";
}
else if (day == 2)
{
dayName = "Monday";
}
else if (day == 3)
{
dayName = "Tuesday";
}
else if (day == 4)
{
dayName = "Wednesday";
}
else if (day == 5)
{
dayName = "Thursday";
}
else if (day == 6)
{
dayName = "Friday";
}
else if (day == 7)
{
dayName = "Saturday";
}
else
{
dayName = "Unknown";
}
Code language: C# (cs)
Output:
Monday
Code language: plaintext (plaintext)
How it works.
First, prompt users to input a day number between 1 and 7:
Console.WriteLine("Please enter a day number (1-7):");
int day = Convert.ToInt32(Console.ReadLine());
Code language: C# (cs)
Second, use the if else if
statement to assign the day name to the dayName
variable based on the day number:
string dayName;
if (day == 1)
{
dayName = "Sunday";
}
else if (day == 2)
{
dayName = "Monday";
}
else if (day == 3)
{
dayName = "Tuesday";
}
else if (day == 4)
{
dayName = "Wednesday";
}
else if (day == 5)
{
dayName = "Thursday";
}
else if (day == 6)
{
dayName = "Friday";
}
else if (day == 7)
{
dayName = "Saturday";
}
else
{
dayName = "Unknown";
}
Code language: C# (cs)
Finally, output the day name to the console:
Console.WriteLine($"It's {dayName}.");
Code language: C# (cs)
If you enter a day number as 5, you’ll get the following output:
Please enter a day number (1-7):
5
It's Thursday.
Code language: plaintext (plaintext)
2) Using the C# if else if statement to develop a body mass index calculation program
The body mass index (BMI) is a person’s weight in kilograms divided by the square of height in meters. The BMI classifies a person’s weight category as underweight, healthy weight, overweight, and obesity.
The following program calculates the body mass index (BMI):
Console.WriteLine("Body Mass Index (BMI) Calculation");
Console.WriteLine("Enter a weight (kg):");
var weight = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter a height (m):");
var height = Convert.ToDouble(Console.ReadLine());
double bmi = weight / (height * height);
string weightStatus;
if (bmi < 18.5)
{
weightStatus = "Underweight";
}
else if (bmi >= 18.5 && bmi <= 24.9)
{
weightStatus = "Healthy Weight";
}
else if (bmi >= 25 && bmi <= 29.9)
{
weightStatus = "Overweight";
}
else
{
weightStatus = "Obesity";
}
Console.WriteLine($"BMI: {bmi:0.#}");
Console.WriteLine($"Weight status:{ weightStatus}");
Code language: C# (cs)
How it works.
First, display the program heading:
Console.WriteLine("Body Mass Index (BMI) Calculation");
Code language: C# (cs)
Next, prompt users to enter weight in kilograms and height in meters:
Console.WriteLine("Enter a weight (kg):");
var weight = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter a height (m):");
var height = Convert.ToDouble(Console.ReadLine());
Code language: C# (cs)
Then, calculate the body mass index:
double bmi = weight / (height * height);
Code language: C# (cs)
After that, assign the weight status based on the BMI using the if else if
statement:
string weightStatus;
if (bmi < 18.5)
{
weightStatus = "Underweight";
}
else if (bmi <= 18.5 && bmi >= 24.9)
{
weightStatus = "Healthy Weight";
}
else if (bmi <= 25 && bmi >= 29.9)
{
weightStatus = "Overweight";
}
else
{
weightStatus = "Obesity";
}
Code language: C# (cs)
Finally, show the BMI result:
Console.WriteLine($"BMI: {bmi:0.#}");
Console.WriteLine($"Weight status:{ weightStatus}");
Code language: C# (cs)
Summary
- Use the C#
if else if
statement to check multiple conditions and execute a block if a condition is true.