Introduction to Else If in C#
C# also supports conditional statements. These statements basically used when someone wants to execute a set of statements and if a particular condition fails then another set of statements execute. So it is very useful when we have multiple sets of statements and we want to execute those according to the scenario or condition-based. This is mostly used for decision-making scenario.
Syntax:
if (some statement) {
}
else if (other statement) {
}
else {
(other statement)
}
Flowchart of Else If in C#
This is the flowchart of else if statement in C# as given below:
How does Else If in C# Work?
For example, we want to show the grades according to the marks obtained by the students.
- Students who have more than 80 percent have an A grade.
- Students who have more than 60 and less than 80 have B grade.
- Similarly, students who have more than 40 and less than 60 percent have C grade and students who have less than 40 have D grade.
- So in these types of scenarios(Decision making), we have used the If-else-if statements which helps the developer to conclude a result.
Examples to Implement Else If in C#
Below are the examples that show how we can implement else-if in C#.
Example #1
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace elseif
{
class Program
{
static void Main(string[] args)
{
int p = 15;
if (p == 20)
{
Console.WriteLine("Value of p is equal to 20");
}
else if (p> 20)
{
Console.WriteLine("Value of p is greater than 20");
}
else
{
Console.WriteLine("Value of p is less than 20");
}
Console.ReadLine();
}
}
}
Code Explanation: In the above example, if else-if statements are used based on the conditions. If the value of p is equal to 20, display the output showing that value is equal to 20, else if the value of p is greater than 20, display different output. If both are not satisfied then display that value is less than 20.
Output:
Example #2
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace elseif
{
class Program
{
static void Main(string[] args)
{
int a = 30, b = 20;
if (a > b)
{
Console.WriteLine("Value of a is greater than b");
}
else if (a < b)
{
Console.WriteLine("Value of a is less than b");
}
else
{
Console.WriteLine("Value of a is equal to b");
}
Console.ReadLine();
}
}
}
Code Explanation: In the above example, values of variables a and b are initialized. If the value of a is greater than b, display a is greater, else if the value of b is greater, display value of a less. The display value of a is equal to b in case both the above conditions are not true.
Output:
Example #3
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace elseif
{
class Program
{
static void Main(string[] args)
{
int x = -1; int y = 20; int z;
if (x < 0 && y < 0)
{
Console.WriteLine("Both x and y are negative.");
}
else if (x < 0 || y < 0)
{
if (y > 0 && y <= 20)
{
z = x + y;
Console.WriteLine("Sum: {0}", z);
}
Console.WriteLine("One of them is negative");
}
else
{
Console.WriteLine("Both x and y are positive.");
}
Console.ReadKey();
}
}
}
Code Explanation: In the above example, || and && operators are also used with statements. Else if statements can also have other statements in a loop called nested statements.
Output:
Example #4
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace elseif
{
class Program
{
static void Main(string[] args)
{
int marks = 65;
if (marks >= 80)
{
Console.WriteLine("Student has passed with higher first class");
}
else if (marks >= 60)
{
Console.WriteLine("Student has passed with first class");
}
else if (marks >= 40)
{
Console.WriteLine("Student has passed with second class");
}
else
{
Console.WriteLine("Student has failed");
}
Console.ReadLine();
}
}
}
Code Explanation: In the above example, multiple else if statements are used based on the marks obtained.
Output:
Conclusion
Conditional decisions are required when we want to execute a block of code only if a certain condition is true or when we want to execute certain steps depends upon some requirement then these conditional decisions are required. The conditional statement is used in C sharp for decision making.
Recommended Articles
This is a guide to Else If in C#. Here we discuss the syntax, flowchart, and working along with the examples and code implementation. You can also go through our suggested articles to learn more –