C-sharp if else statements are useful in decision making in C#. You can execute the block of codes based on the specified logical conditions is true or false. There can be different decisions in C# and different codes are executed based on these decisions.
The C-sharp if else statements are based on the following condition:-
if
if else
if else if else...
Let’s find out these C-sharp if else statements in detail one-by-one.
C# if Statement
C# if
statement executes the block of code based on the single condition is true. There is only a single block of code with a single condition to check within the if
statement.
Syntax
1 2 3 |
if (condition){ //Code to execute if the specified condition is true } |
The above syntax shows the variable ‘condition’ that can be true or false to check.
Let’s find out the statement with the example given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; namespace Demo { class Program { static void Main(string[] args) { int a = 13; int b = 5; if(a > b){ Console.WriteLine("a is greater than b"); } } } } |
Output
The above example first declares the two variables with their values. After that, it uses the if
statement with a single condition ‘a < b
‘. It only executes the block of code when the specified condition is true.
The result shows that the value of a is greater than the value of b.
C-sharp (C#) if else statements
The if else
statement executes the block of code inside the if
statement when the condition is true. If the given condition is not true, it executes the code placed inside the else
statement.
Syntax
1 2 3 4 5 |
if (condition){ //Code to execute if the specified condition is true }else{ //Code to execute if the specified condition is false } |
The syntax given above contains a single condition to check within the if
statement.
Let’s see a simple example to learn the use of if else
statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; namespace Demo { class Program { static void Main(string[] args) { int a = 3; int b = 5; if(a > b){ Console.WriteLine("a is greater than b"); }else{ Console.WriteLine("a is less than b"); } } } } |
Output
In the above example, there are two variables that have certain values. The if
statement contains the condition to check whether the value of a is greater than or not. If the value of a is not greater than b, it executes the code placed within the else
statement.
The result shows that the value of a is less than the value of b.
C-sharp (C#) if else if else Statements
There are different if
conditions in this statement. It checks which if
condition is true to execute one block of code. If all the condition is false, it executes the block of code given within the else
statement.
Syntax
1 2 3 4 5 6 7 |
if (condition1){ //Code to execute if condition1 is true }else if (condition2){ //Code to execute if condition2 is true }else{ //Code to execute if all conditions are false } |
The above syntax shows that there can be many if
conditions to check to execute the block of codes. If the condition is false, it runs the block of code placed inside the else
statement.
Let’s see a simple example to learn the use of the if else if else
statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; namespace Demo { class Program { static void Main(string[] args) { int a = 3; int b = 5; if(a > b){ Console.WriteLine("a is greater than b"); }else if(a < b){ Console.WriteLine("a is less than b"); }else{ Console.WriteLine("a is equals to b"); } } } } |
Output
When you check the above example, it contains the two conditional statements within different if
andelse if
statement. If any condition is true, it executes the block of code placed within it. However, when all the conditions are false, it executes the block of code within the else
statement.
The above result shows that the second if
condition is true to execute the block of code placed within it.
You May Also Like to Read
Reference