Conditional Statements in C#
Introduction
Conditional statements are used to perform different actions based on different conditions. In C#, we have several types of conditional statements, including if, else, else if, switch, and ternary operator.
If Statement
The if statement is used to execute a block of code only if a specified condition is true.
if (condition) {
// block of code to be executed if the condition is true
}
// block of code to be executed if the condition is true
}
Example:
int number = 10;
if (number > 5) {
Console.WriteLine("Number is greater than 5");
}
if (number > 5) {
Console.WriteLine("Number is greater than 5");
}
Output:
Number is greater than 5
Number is greater than 5
If...Else Statement
The if...else statement is used to execute one block of code if a condition is true and another block of code if the condition is false.
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Example:
int number = 3;
if (number > 5) {
Console.WriteLine("Number is greater than 5");
} else {
Console.WriteLine("Number is not greater than 5");
}
if (number > 5) {
Console.WriteLine("Number is greater than 5");
} else {
Console.WriteLine("Number is not greater than 5");
}
Output:
Number is not greater than 5
Number is not greater than 5
If...Else If...Else Statement
The if...else if...else statement is used to specify a new condition to test if the first condition is false.
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
Example:
int number = 7;
if (number > 10) {
Console.WriteLine("Number is greater than 10");
} else if (number > 5) {
Console.WriteLine("Number is greater than 5 but less than or equal to 10");
} else {
Console.WriteLine("Number is 5 or less");
}
if (number > 10) {
Console.WriteLine("Number is greater than 10");
} else if (number > 5) {
Console.WriteLine("Number is greater than 5 but less than or equal to 10");
} else {
Console.WriteLine("Number is 5 or less");
}
Output:
Number is greater than 5 but less than or equal to 10
Number is greater than 5 but less than or equal to 10
Switch Statement
The switch statement selects one of many code blocks to be executed.
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Example:
int day = 3;
switch (day) {
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
default:
Console.WriteLine("Other day");
}
switch (day) {
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
default:
Console.WriteLine("Other day");
}
Output:
Wednesday
Wednesday
Ternary Operator
The ternary operator is a shorthand for the if...else statement.
variable = (condition) ? expressionTrue : expressionFalse;
Example:
int number = 8;
string result = (number > 5) ? "Greater than 5" : "5 or less";
Console.WriteLine(result);
string result = (number > 5) ? "Greater than 5" : "5 or less";
Console.WriteLine(result);
Output:
Greater than 5
Greater than 5