Conditional Statements (If-Else, Switch-Case)

Conditional statements are an essential part of any programming language, including C#. They allow the program to make decisions based on certain conditions and execute different blocks of code accordingly. In C#, two commonly used conditional statements are if-else and switch-case.

If-Else Statement

The if-else statement is used to specify two blocks of code - one block to be executed when the condition is true and another block to be executed when the condition is false.

if (condition)
{
    // Code to be executed if the condition is true
}
else
{
    // Code to be executed if the condition is false
}

The condition inside the if statement is a boolean expression, which evaluates to either true or false. If the condition is true, the block of code inside the if statement will be executed. Otherwise, the block of code inside the else statement will be executed.

Here's an example:

int age = 25;

if (age >= 18)
{
    Console.WriteLine("You are eligible to vote.");
}
else
{
    Console.WriteLine("You are not eligible to vote.");
}

In this example, if the value of the age variable is greater than or equal to 18, the program will output "You are eligible to vote." Otherwise, it will output "You are not eligible to vote."

Switch-Case Statement

The switch-case statement provides an organized way to execute different blocks of code based on the value of a variable or an expression. It eliminates the need for writing multiple if-else statements.

switch (expression)
{
    case value1:
        // Code to be executed when expression is equal to value1
        break;
    case value2:
        // Code to be executed when expression is equal to value2
        break;
    case value3:
        // Code to be executed when expression is equal to value3
        break;
    default:
        // Code to be executed when expression doesn't match any case
        break;
}

The program evaluates the value of the expression and compares it with the constants specified in the case statements. If a match is found, the corresponding block of code is executed. The break statement is used to exit the switch-case block after executing the correct code.

Here's an example:

int dayOfWeek = 4;

switch (dayOfWeek)
{
    case 1:
        Console.WriteLine("Monday");
        break;
    case 2:
        Console.WriteLine("Tuesday");
        break;
    case 3:
        Console.WriteLine("Wednesday");
        break;
    case 4:
        Console.WriteLine("Thursday");
        break;
    case 5:
        Console.WriteLine("Friday");
        break;
    default:
        Console.WriteLine("Weekend");
        break;
}

In this example, the program will output "Thursday" because the value of dayOfWeek is 4.

Both if-else and switch-case statements are incredibly useful in controlling the flow of a program based on specific conditions. They allow developers to create dynamic and flexible code by executing different blocks of code based on different scenarios.

Remember to choose the appropriate conditional statement depending on the complexity and type of conditions you need to evaluate. Mastering these conditional statements will greatly enhance your ability to write efficient and functional C# programs.

© NoobToMaster - A 10xcoder company