if Statement
If statement will execute a block of code if the given condition is true. The if statement checks a Boolean expression and executes the code based on if the expression is true< /p>
Syntax:
if(condition){
//code to be executed
}
Example 1:
using System;
public class Program
{
public static void Main()
{
int number = 10;
if (number % 2 == 0)
{
Console.WriteLine(number+ " is an even number");
}
}
}
Output
10 is an even number
Example 2:
using System;
public class Program
{
public static void Main()
{
int a = 20, b = 10;
if (a > b)
{
Console.WriteLine("a is greater than b");
}
if (a < b)
{
Console.WriteLine("a is less than b");
}
if (a == b)
{
Console.WriteLine("a is equal to b");
}
}
}
Output
a is greater than b