In this article I am going to discuss about the C# Else-If Statement in detail but before that if you had not read my previous article of C# If-Else Statement then must read it. This article covers about C# Else-If Statement and why it is important and how we can use else if statement in our C# Program. If you have not read my previous article then I can tell you Else-If Statement is one of the types of Branching Statements that is used for decision making.
Introduction to C# Else-If Statement
In C#, Else-if Statement is used to specify mulitple conditions if the first condition not met or failed. This type of statement is also important when we have to deal with big programs or having multiple conditions. Let see an example to how we use else if statement in our program.
string name="WebTutorialStack.com";
if (name=="WebTutorialStack.com")
{
// block of code to be executed if condition1 is True
}
else if (name=="Make a career with web development")
{
// block of code to be executed if the condition1 is false and condition2 is True
}
else
{
// block of code to be executed if the condition1 is false and condition2 is False
}
In above example we have declared one string with name variable and using the if statement that if name is equal to “WebTutorialStack.com” then execute this code otherwise execute the else statement but here before else statement we are checking one more condition using else if statement that name is equal to “Make a career with web development” or not. If yes then execute this statement otherwise else block will executes. Similarly we can create any number of else if condition as per our requirement.
Why C# Else-If Statement is Important?
In any programming language sometimes we have to write program which had multiple conditions and that is not possible with if-else statement only. Therefore C# Else-If Statement is comes into existence to tackle this problem with providing one extra block for your conditions. If we have multiple conditions then we can wrap our conditions in else if block for code readability and optimization of code.
C# Program to Explain Else-If Statements
using System;
namespace ElseIfStatementDemo
{
class Program
{
static void Main(string[] args)
{
int time = 22;
if (time < 12)
{
Console.WriteLine("Good morning.");
}
else if (time < 19)
{
Console.WriteLine("Good evening.");
}
else
{
Console.WriteLine("Good night.");
}
}
}
}
Output:
Good night
Is Else block is Mandatory in Else-If Statement
The answer is no, because else block is totally optional in else-if statement. Without else block our program also run without throwing any error. For example:
using System;
public class Program
{
public static void Main()
{
int i = 10, j = 20;
if (i == j)
{
Console.WriteLine("i is equal to j");
}
else if (i > j)
{
Console.WriteLine("i is greater than j");
}
else if (i < j)
{
Console.WriteLine("i is less than j");
}
}
}
Output:
i is less than j
Conclusion
In this C# tutorials article we have discussed about else-if statements, how to use else-if statements and why else-if statements are important for complex programs. Hope you get the clear understanding of when we have to use the else-if statements in our program. In my next article I am going to discuss about the last branching statement which is switch statement in detail.