C# Conditional Statements and Control Flow

Example: If-Else Statement

Here is an example that demonstrates the use of if, else if, and else statements to check the value of a variable and print corresponding messages.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
int number = 10;

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

Explanation

  1. If Statement

    • The if statement checks if the condition inside the parentheses is true.
    • If number > 10 is true, it executes the code block inside the if statement.
    1
    2
    3
    4
    
    if (number > 10)
    {
        Console.WriteLine("The number is greater than 10");
    }
    
  2. Else If Statement

    • The else if statement provides an additional condition to check if the initial if condition is false.
    • If number == 10 is true, it executes the code block inside the else if statement.
    1
    2
    3
    4
    
    else if (number == 10)
    {
        Console.WriteLine("The number is equal to 10");
    }
    
  3. Else Statement

    • The else statement is executed if none of the preceding if or else if conditions are true.
    • It provides a fallback action.
    1
    2
    3
    4
    
    else
    {
        Console.WriteLine("The number is less than 10");
    }
    

Output

Given number = 10, the output will be:

1
The number is equal to 10

Additional Notes

  • Multiple Conditions: You can have multiple else if conditions to handle more complex scenarios.
  • Logical Operators: You can use logical operators like && (AND), || (OR), and ! (NOT) to combine multiple conditions in a single if statement.

Example with Logical Operators

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
int number = 10;

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

Switch Statement

The switch statement is another way to control the flow of your program based on the value of a variable. It is often used when you have multiple specific values to compare.

Example: Switch Statement

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
int number = 10;

switch (number)
{
    case > 10:
        Console.WriteLine("The number is greater than 10");
        break;
    case 10:
        Console.WriteLine("The number is equal to 10");
        break;
    default:
        Console.WriteLine("The number is less than 10");
        break;
}

Explanation

  1. Case Statements

    • Each case statement compares the value of number to a specific value.
    • If number matches the value, the corresponding code block is executed.
    1
    2
    3
    
    case 10:
        Console.WriteLine("The number is equal to 10");
        break;
    
  2. Default Statement

    • The default statement is executed if none of the case conditions match.
    • It serves as a fallback.
    1
    2
    3
    
    default:
        Console.WriteLine("The number is less than 10");
        break;
    
  3. Break Statement

    • The break statement exits the switch block, preventing the execution of subsequent cases.
    1
    
    break;
    

Output

Given number = 10, the output will be:

1
The number is equal to 10

Previous Chapter String

Next Chapter Loop