Conditional statements are fundamental in programming as they allow you to execute different blocks of code based on certain conditions. In C#, the primary conditional statements are if, else if, else, and switch.

  1. The if Statement

The if statement evaluates a boolean expression and executes the block of code inside it if the expression is true.

Syntax

if (condition)
{
    // Code to execute if condition is true
}

Example

int number = 10;

if (number > 5)
{
    Console.WriteLine("The number is greater than 5.");
}

Explanation

  • number > 5 is the condition.
  • If the condition is true, the message "The number is greater than 5." is printed to the console.

  1. The else Statement

The else statement follows an if statement and executes a block of code if the if condition is false.

Syntax

if (condition)
{
    // Code to execute if condition is true
}
else
{
    // Code to execute if condition is false
}

Example

int number = 3;

if (number > 5)
{
    Console.WriteLine("The number is greater than 5.");
}
else
{
    Console.WriteLine("The number is not greater than 5.");
}

Explanation

  • If number > 5 is false, the message "The number is not greater than 5." is printed to the console.

  1. The else if Statement

The else if statement allows you to check multiple conditions.

Syntax

if (condition1)
{
    // Code to execute if condition1 is true
}
else if (condition2)
{
    // Code to execute if condition2 is true
}
else
{
    // Code to execute if none of the above conditions are true
}

Example

int number = 7;

if (number > 10)
{
    Console.WriteLine("The number is greater than 10.");
}
else if (number > 5)
{
    Console.WriteLine("The number is greater than 5 but less than or equal to 10.");
}
else
{
    Console.WriteLine("The number is 5 or less.");
}

Explanation

  • The first condition number > 10 is false.
  • The second condition number > 5 is true, so the message "The number is greater than 5 but less than or equal to 10." is printed to the console.

  1. The switch Statement

The switch statement is used to select one of many code blocks to be executed.

Syntax

switch (variable)
{
    case value1:
        // Code to execute if variable equals value1
        break;
    case value2:
        // Code to execute if variable equals value2
        break;
    default:
        // Code to execute if variable does not match any case
        break;
}

Example

int day = 3;

switch (day)
{
    case 1:
        Console.WriteLine("Monday");
        break;
    case 2:
        Console.WriteLine("Tuesday");
        break;
    case 3:
        Console.WriteLine("Wednesday");
        break;
    default:
        Console.WriteLine("Invalid day");
        break;
}

Explanation

  • The variable day is compared against each case.
  • Since day is 3, the message "Wednesday" is printed to the console.

Practical Exercises

Exercise 1

Write a program that checks if a number is positive, negative, or zero.

Solution

int number = -5;

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

Exercise 2

Write a program that assigns a grade based on a score using a switch statement. The score ranges from 0 to 100.

Solution

int score = 85;

switch (score / 10)
{
    case 10:
    case 9:
        Console.WriteLine("Grade: A");
        break;
    case 8:
        Console.WriteLine("Grade: B");
        break;
    case 7:
        Console.WriteLine("Grade: C");
        break;
    case 6:
        Console.WriteLine("Grade: D");
        break;
    default:
        Console.WriteLine("Grade: F");
        break;
}

Common Mistakes

  • Forgetting the break statement in a switch case: This can cause the program to execute the next case unintentionally.
  • Not using curly braces {} in if statements: This can lead to logical errors, especially when adding more lines of code inside the if block.

Conclusion

In this section, you learned about the different types of conditional statements in C#. These include if, else if, else, and switch statements. Understanding and using these statements effectively allows you to control the flow of your program based on various conditions. In the next section, we will explore loops, which allow you to execute a block of code multiple times.

© Copyright 2024. All rights reserved