DEV Community

mohamed Tayel
mohamed Tayel

Posted on

Mastering C# Fundamentals: 'if`, `else if`, and Multiple Conditions

Meta Description:
Learn how to effectively use if, else if, and nested if statements in C# for decision-making. This guide covers the use of logical operators like && (AND) and || (OR) to combine multiple conditions, allowing you to create dynamic and flexible control flows in your applications with clear examples.

In C#, if statements allow us to control the flow of execution based on specific conditions. By using Boolean operators and conditions, we can make our applications dynamic, responding to user input or other data.

Sequential Flow vs. Conditional Flow

In the previous sections, the execution of our code was straightforward: each line was executed in order. However, in real-life applications, we often need to change this flow. This is where if statements come into play. They allow us to make decisions in our code. Depending on whether a condition is true or false, different code will be executed.

Let’s say we’re building a system to check whether an employee qualifies for a bonus based on multiple criteria. This is where if, else if, and nested if statements shine, especially when combined with logical operators like && (AND) and || (OR).


Understanding if and else

The if statement allows us to execute code based on a Boolean condition. If the condition is true, the code inside the if block will run. If it's false, the code inside the else block will run (if an else block is present).

Example: Simple if-else for Bonus Check

Let’s check if an employee is eligible for a bonus based on their salary:

Console.WriteLine("Enter the employee's salary:");
double salary = double.Parse(Console.ReadLine());

if (salary < 50000)
{
    Console.WriteLine("Eligible for a bonus.");
}
else
{
    Console.WriteLine("Not eligible for a bonus.");
}
Enter fullscreen mode Exit fullscreen mode

Using Logical Operators (&&, ||) to Combine Conditions

Now, let’s introduce logical operators to combine multiple conditions. && (AND) ensures both conditions must be true, while || (OR) ensures that only one of the conditions needs to be true.

Example: Logical Operators for Complex Conditions

Let’s check if the employee is eligible for a bonus if:

  • Their salary is less than $50,000 OR they have more than 5 years of experience, AND
  • They must be full-time employees.
Console.WriteLine("Enter the employee's salary:");
double salary = double.Parse(Console.ReadLine());

Console.WriteLine("Enter the employee's years of experience:");
int experience = int.Parse(Console.ReadLine());

Console.WriteLine("Is the employee full-time (true/false)?");
bool isFullTime = bool.Parse(Console.ReadLine());

// Combining multiple conditions using && and ||
if ((salary < 50000 || experience > 5) && isFullTime)
{
    Console.WriteLine("Eligible for a bonus.");
}
else
{
    Console.WriteLine("Not eligible for a bonus.");
}
Enter fullscreen mode Exit fullscreen mode

Using Nested if Statements for Complex Logic

Nested if statements allow you to perform multiple layers of checks. For instance, we can first check if an employee qualifies for a bonus, and then within that block, check whether they qualify for a special bonus.

Example: Nested if Statements with Logical Operators

Let’s extend the example to check if the employee is eligible for a special bonus. The employee will be eligible if:

  • Their salary is less than $40,000 OR they have more than 7 years of experience, AND
  • They are full-time employees. If not, we check for part-time status with more than 10 years of experience.
Console.WriteLine("Enter the employee's salary:");
double salary = double.Parse(Console.ReadLine());

Console.WriteLine("Enter the employee's years of experience:");
int experience = int.Parse(Console.ReadLine());

Console.WriteLine("Is the employee full-time (true/false)?");
bool isFullTime = bool.Parse(Console.ReadLine());

if (salary < 40000 || experience > 7)
{
    // First level: Check for full-time or special part-time eligibility
    if (isFullTime)
    {
        Console.WriteLine("Eligible for a special bonus as a full-time employee.");
    }
    else if (experience > 10)
    {
        Console.WriteLine("Eligible for a special bonus as a part-time employee with over 10 years of experience.");
    }
    else
    {
        Console.WriteLine("Not eligible for a special bonus.");
    }
}
else
{
    Console.WriteLine("Not eligible for a bonus.");
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • The first condition (salary < 40000 || experience > 7) checks if the salary is below $40,000 or the employee has more than 7 years of experience.
  • Within that block, we use a nested if to check for full-time status. If they are full-time, they qualify for a special bonus.
  • If they are not full-time, another nested if checks if they have more than 10 years of experience. If true, they qualify for a special bonus as a part-time employee.
  • If none of these conditions are met, the employee is not eligible for a special bonus.

Comparison: else if vs. Nested if

  1. else if:
    • else if is useful when you want to check multiple, mutually exclusive conditions in sequence. Once one of the conditions is true, the rest are skipped.
    • Example:
   if (salary < 50000)
   {
       Console.WriteLine("Eligible for a bonus.");
   }
   else if (salary > 100000)
   {
       Console.WriteLine("Eligible for a senior bonus.");
   }
   else
   {
       Console.WriteLine("Standard employee.");
   }
Enter fullscreen mode Exit fullscreen mode
  1. Nested if:
    • Nested if statements are useful when one condition depends on another, meaning you can check multiple conditions inside another if block.
    • This is useful for hierarchical or tiered logic, where you need to drill down into sub-conditions.
    • Example:
   if (salary < 40000 || experience > 7)
   {
       if (isFullTime)
       {
           Console.WriteLine("Eligible for a special bonus.");
       }
       else if (experience > 10)
       {
           Console.WriteLine("Eligible for a part-time bonus.");
       }
   }
Enter fullscreen mode Exit fullscreen mode

Conclusion

Understanding how to use if, else if, multiple if statements, and logical operators like && and || is crucial for decision-making in C# programs. Logical operators allow you to combine multiple conditions into a single statement, while nested if statements help you manage more complex, layered conditions.

Key takeaways:

  • if-else if: Efficient when you need to check mutually exclusive conditions.
  • Multiple if statements: Useful when several conditions could be true at the same time.
  • Logical operators (&&, ||): Allow combining conditions for complex decision-making.
  • Nested if statements: Allow for more detailed, hierarchical checks where each condition depends on another.

Top comments (0)