In this article I am going to discuss about C# If-else Statement in detail but before that if you had not read my previous article of C# Control Flow Statements then must read it because If-Else statement is part of control flow statements. C# If-Else Statement is one of the control flow statements which is used to execute particular condition if the condition is true otherwise it returns false.
Introduction to C# If-Else Statement
This If-Else Statement belongs to the Selection statements or branching statement category which is one of the type of control flow statements. This is also called Decision making statement because decision making in programming language is very similar to taking decisions in real life. For example, you might have one situation where you will decide whether you to go to the park or you want to go cinema to watch a movie. And the condition is that, if it is raining, I will go to the cinema and if it is not raining then I will go to the park. So, the condition is rain, and based on the rain, you will decide what you need to do.
The decision-making statements in C# allow us to make a decision, based upon the result of a condition. If the condition is satisfying, we may need to execute some piece of code and if the condition is failed, we might need to execute some other piece of code. Like:
string value="Its raining";
if(value=="Its raining"){
Console.WriteLine("I will go to the cinema");
}
else{
Console.WriteLine("I will go to the park");
}
C# Program to Explain If-Else Statement
using System;
namespace IfElseDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a Number: ");
int number = Convert.ToInt32(Console.ReadLine());
if (number % 2 == 0)
{
Console.WriteLine("{0} is an Even Number",number);
}
else
{
Console.WriteLine("{0} is an Odd Number",number);
}
Console.ReadKey();
}
}
}
Output:
Enter a Number:
67
67 is an Odd Number
Is it Mandatory to Use Else Block?
No, It is not mandatory to use else block because it is optional. Only If statement is required in writing program but if you want to provide some extra information when the condition is failed then on that case you can use else block also. For example:
using System;
namespace IfStatement
{
class Program
{
static void Main(string[] args)
{
int number;
Console.WriteLine("Enter a Number: ");
number = Convert.ToInt32(Console.ReadLine());
if (number > 10)
{
Console.WriteLine("{0} is greater than 10 ",number);
Console.WriteLine("End of if block");
}
Console.WriteLine("End of Main Method");
Console.ReadKey();
}
}
}
Output:
Enter a Number:
70
70 is greater than 10
End of if block
End of Main Method
C# If-Else Statement Without Curly Braces
In the declaration of if block or else block if we do not specify statements using curly braces {}, then only the first statement will be considered as the if block or else block statement. Let us understand this with an example. Please have a look at the below code. For Example :
using System;
namespace IfElseWithoutCurlyBraces
{
class Program
{
static void Main(string[] args)
{
int number = 10;
if (number == 10)
Console.WriteLine("Hi"); //This Statement belongs to IF Block
else
Console.WriteLine("Hello");//This Statement belongs to ELSE Block
Console.WriteLine("Bye");
Console.ReadKey();
}
}
}
Output:
Hi
Bye
Nested If-Else Statement in C# Language
In C#, Nested if-else block means defining if-else block inside another if block. We can also define the if block inside the else block. It totally Depend on our logical requirements, we can use nested if block either inside the if block or inside the else block. When we write this type of code then it is called Nested If-Else Statement. For Example :
using System;
namespace NestedIfElseDemo
{
class Program
{
static void Main(string[] args)
{
int a = 15, b = 25, c = 10;
int LargestNumber = 0;
if (a > b)
{
Console.WriteLine("Outer IF Block");
if (a > c)
{
Console.WriteLine("Outer IF - Inner IF Block");
LargestNumber = a;
}
else
{
Console.WriteLine("Outer IF - Inner ELSE Block");
LargestNumber = c;
}
}
else
{
Console.WriteLine("Outer ELSE Block");
if (b > c)
{
Console.WriteLine("Outer ELSE - Inner IF Block");
LargestNumber = b;
}
else
{
Console.WriteLine("Outer ELSE - Inner ELSE Block");
LargestNumber = c;
}
}
Console.WriteLine("The Largest Number is: {0}",LargestNumber);
Console.ReadKey();
}
}
}
Output:
Outer ELSE Block
Outer ELSE - Inner IF Block
The Largest Number is: 25
Conclusion
In this C# tutorials article we have discussed about the If-Else Statements, Nested If-Else Statement and else block is mandatory or not while using If statement with proper examples. In my next article I am going to discuss about If-Else-If Statement in detail.