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.
The above is the detailed content of Else If in C#. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

There are several ways to modify XML formats: manually editing with a text editor such as Notepad; automatically formatting with online or desktop XML formatting tools such as XMLbeautifier; define conversion rules using XML conversion tools such as XSLT; or parse and operate using programming languages such as Python. Be careful when modifying and back up the original files.
