If Else Statement in C#
If Else Statement in C#:
In C#, we have an if statement which is executed if the condition is true otherwise it does not execute. Let us say, we want to execute something if the condition is false. For this purpose, we use the else statement. Else statement is used with an if statement to execute some block of code if the given condition is false. Else statement can contain single or multiple statements inside the curly brackets. We can skip the curly brackets if the else statement only contains a single statement. The statements inside the else block may contain another if-else statement as well.
Syntax:
if(condition)
{
// statements execute if condition is true
}
else
{
// statements execute if condition is false
}
Below you can see the flow chart representation of the if else statement - Let us understand the if else statement with an example -
using System;
class Program
{
static public void Main()
{
string x = "Parth";
string y = "ParthJolly";
if (x == y)
{
Console.WriteLine(“Both are same”);
}
else
{
Console.WriteLine(“Both are different”);
}
}
}
Output: Both are different
