Meta Description
Learn how to use if
, else if
, and nested if
statements in C# to make dynamic decisions in your applications. Discover how to combine multiple conditions with logical operators (&&
, ||
) to create flexible and robust control flows, with practical examples and clear explanations.
Sequential Flow vs. Conditional Flow
In programming, conditional statements enable dynamic decision-making. Unlike sequential code, which executes in order, conditional code reacts to specific criteria. For instance, deciding whether an employee qualifies for a bonus depends on their salary, experience, and employment status.
Visualization of Decision Flow
Here’s a simplified flowchart to represent decision-making for employee bonuses:
- Input salary, experience, and full-time status.
-
Condition Check:
- Is the salary
< $50,000
OR experience> 5 years
? - AND is the employee
full-time
?
- Is the salary
-
Output Results:
- If true: Eligible for bonus.
- Otherwise: Not eligible for bonus.
Understanding if
, else
, and Logical Operators
Basic if-else
Statement
An if
statement evaluates a Boolean condition. If true
, it executes the corresponding block of code; otherwise, the else
block runs (if provided).
Example 1: Simple Bonus Check
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.");
}
Logical Operators: Combining Conditions
Logical operators help combine multiple conditions:
-
&&
(AND): Both conditions must be true. -
||
(OR): At least one condition must be true.
Example 2: Bonus Check with Logical Operators
Let’s add conditions for years of experience and full-time status:
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 < 50000 || experience > 5) && isFullTime)
{
Console.WriteLine("Eligible for a bonus.");
}
else
{
Console.WriteLine("Not eligible for a bonus.");
}
Using Nested if
Statements for Complex Logic
Nested if
statements enable multi-level checks. For instance:
- If the salary is
< $40,000
OR experience is> 7 years
, check:- If full-time, grant a special bonus.
- If part-time with
> 10 years
of experience, grant a part-time special bonus.
Example 3: Nested if
with Logical Operators
if (salary < 40000 || experience > 7)
{
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.");
}
Comparison: else if
vs. Nested if
Feature | else if |
Nested if |
---|---|---|
Purpose | Checks mutually exclusive conditions. | Drills down into sub-conditions for detailed checks. |
Code Example | if (salary < 50k) else if (salary > 100k) |
if (salary < 40k) { if (isFullTime) {...} } |
Usage | Sequential and straightforward conditions. | Hierarchical or dependent conditions. |
Common Pitfalls and Edge Cases
-
Boundary Conditions: Test values at the edge of conditions (e.g.,
$50,000
or$40,000
). - Error Handling: Validate user inputs to prevent runtime errors.
if (!double.TryParse(Console.ReadLine(), out double salary))
{
Console.WriteLine("Invalid input. Please enter a valid number.");
return;
}
-
Readability: Avoid deeply nested
if
blocks. Refactor into methods if necessary.
Assignments
Easy:
Write a program that determines if a student passes or fails based on:
- Score >= 50 is a pass.
- Otherwise, it’s a fail.
Medium:
Extend the above to:
- Score >= 80: "Distinction."
- Score >= 50 and < 80: "Pass."
- Otherwise: "Fail."
Difficult:
Create a program to calculate shipping costs based on:
- Order value (
< $50
,$50-$100
,> $100
). - Shipping destination (local, international).
- Membership status (premium, regular).
Conclusion
By mastering conditional statements and logical operators, you can make your C# applications smarter and more dynamic. Whether using if-else
for simple decisions or nested if
for hierarchical logic, clear structure and testing ensure robust programs. Practice with the provided examples and assignments to enhance your skills further!