If Else Statement
In C# if-else statement also tests the condition. It executes the if block if the condition is true otherwise else block, is executed. the else statement to specify a block of code to be executed if the condition is False. the if-else statement checks a Boolean expression and executes the code based on if the expression is true or false. The if part of the code executes when the value of the expression is true. The else part of the code is executed when the value of the expression is false.
Syntax
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
}
Example 1:
using System;
public class Program {
public static void Main() {
int age = 19;
if (age < 18) {
Console.WriteLine("Your age is less then 18");
}
else {
Console.WriteLine("Your age is greater then 18.");
}
}
}
The output will be:
Your age is greater then 18.