The if statement in C# is a fundamental control structure used to make decisions based on conditions.
In this tutorial, we'll cover the basics of if statements, including conditional expressions, else and else if statements, nested if statements, and the switch alternative for more complex cases.
1. Basic if Statement
The if statement evaluates a condition and executes code only if the condition is true.
Syntax:
if (condition) { // Code to execute if condition is true }
Example:
using System; public class IfStatementExample { public static void Main() { int age = 20; if (age >= 18) { Console.WriteLine("You are eligible to vote."); } } }
In this example, the condition checks if age is greater than or equal to 18. If the condition is true, it prints “You are eligible to vote.”
2. if-else Statement
The if-else statement provides two code paths: one for when the condition is true and another for when it is false.
Syntax:
if (condition) { // Code to execute if condition is true } else { // Code to execute if condition is false }
Example:
using System; public class IfElseExample { public static void Main() { int age = 16; if (age >= 18) { Console.WriteLine("You are eligible to vote."); } else { Console.WriteLine("You are not eligible to vote."); } } }
Here, if the age is less than 18, it prints “You are not eligible to vote.”
3. if-else if-else Statement
The if-else if-else statement allows multiple conditions to be checked in sequence. If one of the conditions is true, the corresponding block executes, and the rest are skipped.
Syntax:
if (condition1) { // Code if condition1 is true } else if (condition2) { // Code if condition2 is true } else { // Code if none of the conditions are true }
Example:
using System; public class IfElseIfExample { public static void Main() { int score = 85; if (score >= 90) { Console.WriteLine("Grade: A"); } else if (score >= 80) { Console.WriteLine("Grade: B"); } else if (score >= 70) { Console.WriteLine("Grade: C"); } else { Console.WriteLine("Grade: F"); } } }
In this example, the program checks the score and prints a grade based on the range it falls into.
4. Nested if Statements
You can place one if statement inside another if statement. This is called “nesting” and is useful for checking complex conditions.
Example:
using System; public class NestedIfExample { public static void Main() { int age = 25; bool hasLicense = true; if (age >= 18) { if (hasLicense) { Console.WriteLine("You can drive."); } else { Console.WriteLine("You need a driving license to drive."); } } else { Console.WriteLine("You are too young to drive."); } } }
Here, the first if checks if the person is 18 or older. If true, it then checks if the person has a license. If both conditions are satisfied, it prints “You can drive.”
5. if Statements with Logical Operators
C# allows using logical operators such as && (AND), || (OR), and ! (NOT) within if conditions. These operators make conditions more powerful by combining multiple checks.
Example:
using System; public class LogicalOperatorsExample { public static void Main() { int age = 20; bool hasLicense = true; if (age >= 18 && hasLicense) { Console.WriteLine("You are allowed to drive."); } else { Console.WriteLine("You are not allowed to drive."); } } }
In this example:
- age >= 18 && hasLicense ensures both conditions are true for the if block to execute.
- If either condition is false, the program will print “You are not allowed to drive.”
6. if Statements with Ternary Operator
The ternary operator ?: is a shorthand way to write simple if-else statements. It’s useful when you want to assign a value based on a condition.
Syntax:
variable = condition ? value_if_true : value_if_false;
Example:
using System; public class TernaryExample { public static void Main() { int age = 17; string eligibility = (age >= 18) ? "Eligible to vote" : "Not eligible to vote"; Console.WriteLine(eligibility); } }
Here:
- The condition (age >= 18) is checked.
- If true, eligibility is set to “Eligible to vote”; otherwise, it is set to “Not eligible to vote.”
7. if vs. switch Statement
For scenarios where you have multiple conditions on the same variable, switch statements can often be cleaner and more readable than multiple if-else if statements. switch is generally used for discrete values rather than ranges.
Example using switch:
using System; public class SwitchExample { public static void Main() { int day = 3; switch (day) { case 1: Console.WriteLine("Monday"); break; case 2: Console.WriteLine("Tuesday"); break; case 3: Console.WriteLine("Wednesday"); break; case 4: Console.WriteLine("Thursday"); break; case 5: Console.WriteLine("Friday"); break; case 6: Console.WriteLine("Saturday"); break; case 7: Console.WriteLine("Sunday"); break; default: Console.WriteLine("Invalid day"); break; } } }
Here:
- switch checks the value of day and matches it with one of the case statements.
- This approach is often more readable than multiple if-else if statements.
Summary
The if statement in C# is essential for decision-making in code.
Here’s a quick summary of what we covered:
- Basic if statement: Checks a condition.
- if-else statement: Provides two code paths based on a condition.
- if-else if-else statement: Checks multiple conditions in sequence.
- Nested if statements: Allows for complex, multi-level conditions.
- Logical operators: Combine conditions with &&, ||, and !.
- Ternary operator: Shortens simple if-else assignments.
- switch statement: Alternative for handling multiple discrete values.