As in real life to make a decision and proceed further, we have similar things in programming to
execute certain blocks of code when a condition is fulfilled.
We have these conditional statements:
-> if
-> if-else
-> if-else-if
-> Nested if
-> Switch
-> Nested switch
IF Statement:
The IF Statement checks for a bool condition. If the condition is true then it
executes the block of code inside the IF block otherwise, it will not execute.
Syntax:
if (condition)
{
XX statement;
}
IF- else Statement:
The IF Statement checks for a bool condition. If the condition is true then it
executes the block of code inside the IF block otherwise it will execute the else block of code.
Syntax:
if (condition)
{
XX statement; //this executes if condition is true
}
else
{
XX statement;//this executes if condition is false
}
IF- else If Statement:
The If and else If Statements checks for bool conditions sequentially from
the top and executes only the block when the condition is satisfied and once any condition is
satisfied it skips the check for other conditions. If none of the If blocks are executed then the last
else block is executed.
Syntax:
if (conditionX)
{
XX statement; //this executes if conditionX is true
}
else if (conditionY)
{
XX statement; //this executes if conditionY is true
}
else if (conditionZ)
{
XX statement; //this executes if conditionZ is true
}
else
{
XX statement;//this executes if none of the above condition is satisfied
}
Nested If Statement:
The IF statement inside an IF statement is called the Nested IF statement.
This makes code more readable.
Syntax:
if (conditionX)
{
if (conditionX)
{
XX statement; //this executes if conditionX is true
}
}
Example 1:
using System;
namespace SampleProgram {
class MySample {
public static void Main(string[] args) {
int age = 15;
if (age > 18) {
Console.WriteLine(“Age greater than 18”);
}
else if (age <= 18 && age > 10) {
Console.WriteLine(“Age above 10 and less than or equal to 18”);
}
else {
Console.WriteLine(“Age less than or equal to 10”);
}
}
}
The output will be:
Age above 10 and less than or equal to 18
Switch Statement:
The Switch Statement checks for multiple conditions and when satisfied a
break is used to move out of the switch. If none of the conditions is true then it executes the
default case at the end.
Syntax:
switch (expression)
{
case conditon1:XX statement;
break;
case conditon2:YY statement;
break;
case conditon3:ZZ statement;
break;
default:XXY statement;
break;
}
Nested Switch Statement:
The Switch statement inside a switch statement is called Nested
Switch.
Syntax:
switch (expression)
{
case conditon1:XX statement;
break;
case conditon2:YY statement;
switch (expression)
{
case condition5:XY Statement;
break;
}
break;
case conditon3:ZZ statement;
break;
default:
}
Example 2:
using System;
namespace SampleProgram {
class MySample1 {
public static void Main(string[] args) {
string rating = ”Good”;
Switch(rating) {
Case“Average”: Console.WriteLine(“Rating is Average”);
break;
Case“Good”: Console.WriteLine(“Rating is Good”);
break;
Case“Excellent”: Console.WriteLine(“Rating is Excellent”);
break;
default:
Console.WriteLine(“Rating is Poor”);
break;
}
}
}
}
The output will be:
Rating is Good