Conditionals are used to make decisions in your code based on certain conditions. Allowing you to control the flow of your program by executing different blocks of code depending on the condition.

if Statement

The if statement is the simplest type of conditional statement in C#. It allows you to execute a block of code if a certain condition is true.

int number = 10;

if (number > 0)
{
    Console.WriteLine("The number is positive.");
}

In this example, if the value of the number variable is greater than 0, the message “The number is positive.” will be printed to the console.

if-else Statement

The if-else statement allows you to execute one block of code if a condition is true, and a different block of code if the condition is false.

int number = 10;

if (number > 0)
{
    Console.WriteLine("The number is positive.");
}
else
{
    Console.WriteLine("The number is non-positive.");
}

In this example, if the value of the number variable is greater than 0, the message “The number is positive.” will be printed to the console. Otherwise, the message “The number is non-positive.” will be printed.

if-else if-else Statement

The if-else if-else statement allows you to test multiple conditions and execute different blocks of code based on the evaluation of those conditions.

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.");
}

In this example, if the value of number is greater than 0, the message “The number is positive.” will be shown.

If it is less than 0, then the message “The number is negative.” will be printed. If the value is 0, the console will show the message “The number is zero.”.

switch Statement

The switch statement let you to test the value of an expression against a list of case labels and execute the code block associated with the matching case.

int dayOfWeek = 2;
string dayName;

switch (dayOfWeek)
{
    case 1:
        dayName = "Monday";
        break;
    case 2:
        dayName = "Tuesday";
        break;
    case 3:
        dayName = "Wednesday";
        break;
    case 4:
        dayName = "Thursday";
        break;
    case 5:
        dayName = "Friday";
        break;
    case 6:
        dayName = "Saturday";
        break;
    case 7:
        dayName = "Sunday";
        break;
    default:
        dayName = "Invalid day";
        break;
}

Console.WriteLine("Today is " + dayName);

In this example, the value of dayOfWeek is compared against each case label. If a match is found, the corresponding dayName is assigned and the code block is executed. If none of the cases match, the code block associated with the default label is executed.

These are some of the basic examples of C# conditionals. There are other variations and advanced techniques available in C# for handling conditions, but understanding these fundamentals will provide a solid foundation for working with them.