In C#, if...else statements are used for conditional execution.
They allow you to execute certain code blocks based on the
evaluation of a condition.
There are several variations of if...else statements, including if,
if...else,
if...else if...else, and nested if...else.
using System;
class Program
{
static void Main(string[] args)
{
int a = 25;
int b = 50;
// Less than
Console.WriteLine($"Is {a} less than {b}? " + (a < b)); // Output: True
// Less than or equal to
Console.WriteLine($"Is {a} less than or equal to {b}? " + (a <= b)); // Output: True
// Greater than
Console.WriteLine($"Is {a} greater than {b}? " + (a > b)); // Output: False
// Greater than or equal to
Console.WriteLine($"Is {a} greater than or equal to {b}? " + (a >= b)); // Output: False
// Equal to
Console.WriteLine($"Is {a} equal to {b}? " + (a == b)); // Output: False
// Not equal to
Console.WriteLine($"Is {a} not equal to {b}? " + (a != b)); // Output: True
}
}
using System;
class Program
{
static void Main(string[] args)
{
int a = 25;
int b = 50;
// Less than
if (a < b)
{
Console.WriteLine($"{a} is less than {b}");
}
else
{
Console.WriteLine($"{a} is not less than {b}");
}
// Less than or equal to
if (a <= b)
{
Console.WriteLine($"{a} is less than or equal to {b}");
}
else
{
Console.WriteLine($"{a} is neither less than nor
equal to {b}");
}
// Greater than
if (a > b)
{
Console.WriteLine($"{a} is greater than {b}");
}
else
{
Console.WriteLine($"{a} is not greater than {b}");
}
// Greater than or equal to
if (a >= b)
{
Console.WriteLine($"{a} is greater than or equal
to {b}");
}
else
{
Console.WriteLine($"{a} is neither greater than
nor equal to {b}");
}
// Equal to
if (a == b)
{
Console.WriteLine($"{a} is equal to {b}");
}
else
{
Console.WriteLine($"{a} is not equal to {b}");
}
// Not equal to
if (a != b)
{
Console.WriteLine($"{a} is not equal to {b}");
}
else
{
Console.WriteLine($"{a} is equal to {b}");
}
}
}