C sharp - if-else
What is if-else
in C#?
The if-else
statement is a conditional control structure that executes different blocks of code based on whether a condition is true
or false
.
Syntax
if (condition) {
// Code to execute if condition is true
}
else
{
// Code to execute if condition is false
}
Example
using System;
class Program
{
static void Main()
{
int number = 10;
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else
{
Console.WriteLine("The number is not positive.");
}
}
}
Output:
The number is positive.