C# offers a variety of conditional statements that allow you to control the flow of your program based on different conditions. Here are the main types of conditional statements in C#:
1. if
Statement
The if
statement evaluates a condition and executes a block of code if the condition is true.
csharp
int number = 10;
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
2. if..else
Statement
The if..else
statement executes one block of code if the condition is true, and another block if the condition is false.
csharp
int number = -5;
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else
{
Console.WriteLine("The number is non-positive.");
}
3. if..else if..else
Statement
The if..else if..else
statement allows you to test multiple conditions.
csharp
int number = 0;
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else if (number < 0)
{
Console.WriteLine("The number is negative.");
}
else
{
Console.WriteLine("The number is zero.");
}
4. Nested if
Statements
You can nest if
statements within each other to evaluate more complex conditions.
csharp
int number = 5;
if (number > 0)
{
if (number % 2 == 0)
{
Console.WriteLine("The number is positive and even.");
}
else
{
Console.WriteLine("The number is positive and odd.");
}
}
else
{
Console.WriteLine("The number is non-positive.");
}
5. switch
Statement
The switch
statement provides an efficient way to dispatch execution to different parts of code based on the value of an expression.
csharp
char grade = 'B';
switch (grade)
{
case 'A':
Console.WriteLine("Excellent!");
break;
case 'B':
Console.WriteLine("Well done");
break;
case 'C':
Console.WriteLine("Good");
break;
case 'D':
Console.WriteLine("Needs improvement");
break;
case 'F':
Console.WriteLine("Failed");
break;
default:
Console.WriteLine("Invalid grade");
break;
}
6. Ternary Operator
The ternary operator (?:
) is a shorthand for if..else
statements.
csharp
int age = 18;
string status = (age >= 18) ? "Adult" : "Minor";
Console.WriteLine(status);
Example Code:
Here's a complete example illustrating different conditional statements:
csharp
using System;
class Program
{
static void Main()
{
int number = 15;
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else if (number < 0)
{
Console.WriteLine("The number is negative.");
}
else
{
Console.WriteLine("The number is zero.");
}
if (number > 0)
{
if (number % 2 == 0)
{
Console.WriteLine("The number is even.");
}
else
{
Console.WriteLine("The number is odd.");
}
}
char grade = 'B';
switch (grade)
{
case 'A':
Console.WriteLine("Excellent!");
break;
case 'B':
Console.WriteLine("Well done");
break;
case 'C':
Console.WriteLine("Good");
break;
case 'D':
Console.WriteLine("Needs improvement");
break;
case 'F':
Console.WriteLine("Failed");
break;
default:
Console.WriteLine("Invalid grade");
break;
}
int age = 21;
string status = (age >= 18) ? "Adult" : "Minor";
Console.WriteLine("Status: " + status);
}
}
These conditional statements in C# are powerful tools for controlling program flow based on various conditions. If you need more details or have specific questions about any of these, feel free to ask!