Home  >  Article  >  Backend Development  >  Else If in C#

Else If in C#

WBOY
WBOYOriginal
2024-09-03 15:09:34730browse

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:

Else If in C#

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:

Else If in C#

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:

Else If in C#

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:

Else If in C#

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:

Else If in C#

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.

The above is the detailed content of Else If in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# if StatementNext article:C# if Statement