C# > Core C# > Control Flow > if, else if, else Statements
Basic if-else if-else Example
This snippet demonstrates a fundamental use of `if`, `else if`, and `else` statements in C#. It checks a variable's value against multiple conditions and executes the corresponding code block.
Code Example
This code defines an integer variable `number` and then uses `if`, `else if`, and `else` statements to check its value. The first `if` condition checks if `number` is greater than 20. If it is, the corresponding message is printed. If not, the `else if` condition checks if `number` is greater than 10. If that's true, a different message is printed. Finally, if neither of the previous conditions is met, the `else` block is executed, indicating that `number` is not greater than 10. In this example, the output will be 'Number is greater than 10 but not greater than 20.'
using System;
public class IfElseExample
{
public static void Main(string[] args)
{
int number = 15;
if (number > 20)
{
Console.WriteLine("Number is greater than 20.");
}
else if (number > 10)
{
Console.WriteLine("Number is greater than 10 but not greater than 20.");
}
else
{
Console.WriteLine("Number is not greater than 10.");
}
}
}
Concepts Behind the Snippet
The `if`, `else if`, and `else` statements are control flow statements that allow you to execute different blocks of code based on whether certain conditions are true or false. The `if` statement executes its code block if the condition within the parentheses is true. The `else if` statement is used to check multiple conditions in sequence. If the `if` condition is false, the `else if` condition is evaluated. You can have multiple `else if` blocks. The `else` statement executes its code block if none of the preceding `if` or `else if` conditions are true. It provides a default case to handle situations where none of the other conditions are met.
Real-Life Use Case
Imagine you're building a grading system. You can use `if`, `else if`, and `else` statements to assign letter grades based on numerical scores. For example:
if (score >= 90) grade = 'A';
else if (score >= 80) grade = 'B';
else if (score >= 70) grade = 'C';
else if (score >= 60) grade = 'D';
else grade = 'F';
This allows you to automate the process of assigning grades based on different score ranges.
Best Practices
Interview Tip
Be prepared to explain the difference between `if` statements and `switch` statements. Also, understand the concept of short-circuiting in conditional expressions (e.g., `&&` and `||` operators). For example, `if (condition1 && condition2)` will not evaluate `condition2` if `condition1` is false, which can affect the outcome of the expression.
When to Use Them
`if`, `else if`, and `else` statements are ideal when you need to execute different code blocks based on specific conditions. They're suitable for scenarios where you have a limited number of distinct conditions to check and where the logic is relatively straightforward.
Alternatives
Pros
Cons
FAQ
-
What happens if none of the conditions in an `if-else if-else` statement are true?
If none of the `if` or `else if` conditions are true, the code within the `else` block will be executed. If there is no `else` block, then no code within the structure will be executed. -
Can I nest `if` statements inside other `if` statements?
Yes, you can nest `if` statements. However, excessive nesting can make your code difficult to read and maintain. Consider refactoring complex logic into separate functions or methods.