The C# if else statement is a control flow statement used to execute different blocks of code based on the evaluation of a condition. The basic structure of an if else statement in C# is as follows:
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
The condition
is a Boolean expression that evaluates to either true
or false
. If the condition is true
, the code within the if
block is executed. If the condition is false
, the code within the else
block is executed.
The else
block can only be used once in an if else statement. It cannot contain any conditions and is executed when all the previous if
and else if
conditions evaluate to false
.
Here’s an example of an if else statement in C#:
int time = 22;
if (time < 10) {
Console.WriteLine("Good morning.");
} else {
Console.WriteLine("Good evening.");
}
In this example, the condition time < 10
is evaluated to false
, so the code within the else
block is executed, and the output will be “Good evening.”
C# also supports the else if
statement, which allows you to specify a new condition to test if the first condition is false
. The else if
statement is used as follows:
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition1 is false and condition2 is true
} else {
// code to be executed if condition1 and condition2 are false
}
The else if
statement can be used to create a chain of conditions, where each condition is evaluated in order until a true
condition is found. If no condition is true
, the code within the else
block is executed.
The role of the C# if else statement in controlling the flow of your program
The if else statement is a fundamental control flow mechanism in C#, allowing you to make decisions and execute different code paths based on the evaluation of a condition. This is crucial for creating dynamic and responsive applications, where the program’s behavior can adapt to various scenarios.
At its core, the if else statement evaluates a Boolean expression, which can be a simple comparison (e.g., `x > 5`) or a more complex logical operation (e.g., `(x > 5) && (y < 10)`). If the condition evaluates to `true`, the code block within the `if` statement is executed. If the condition evaluates to `false`, the code block within the `else` statement is executed.
The flexibility of the if else statement lies in its ability to handle multiple conditions. In addition to the basic `if-else` structure, C# also supports the `else if` statement, which allows you to chain multiple conditions together. This enables your program to make more nuanced decisions based on various factors.
For example, you might use an if else statement to determine the appropriate discount for a customer based on their loyalty level. If the customer’s loyalty level is 1, they receive a 5% discount; if it’s 2, they receive a 10% discount; and if it’s 3, they receive a 15% discount. If the customer’s loyalty level doesn’t match any of these conditions, they receive no discount.
The if else statement is not limited to simple comparisons; it can also be used in combination with other control flow statements, such as loops, to create more complex decision-making logic.
For instance, you might use an if else statement within a loop to process a collection of items, performing different actions based on the characteristics of each item.
By mastering the use of the if else statement, you can write more robust, flexible, and maintainable C# code. It’s a fundamental building block that allows you to create programs that can adapt to a wide range of scenarios and user inputs, making your applications more responsive and user-friendly.
Examples of C# if else statements:
Checking if a number is positive or negative
int number = -5;
if (number > 0) {
Console.WriteLine("The number is positive.");
} else {
Console.WriteLine("The number is negative.");
}
Determining the grade based on a score
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 if (score >= 60) {
Console.WriteLine("Grade: D");
} else {
Console.WriteLine("Grade: F");
}
Checking if a number is even or odd
int number = 7;
if (number % 2 == 0) {
Console.WriteLine("The number is even.");
} else {
Console.WriteLine("The number is odd.");
}
Determining the season based on the month
int month = 6;
if (month == 12 || month == 1 || month == 2) {
Console.WriteLine("The season is Winter.");
} else if (month == 3 || month == 4 || month == 5) {
Console.WriteLine("The season is Spring.");
} else if (month == 6 || month == 7 || month == 8) {
Console.WriteLine("The season is Summer.");
} else {
Console.WriteLine("The season is Fall.");
}
Checking if a person is eligible to vote
int age = 18;
if (age >= 18) {
Console.WriteLine("You are eligible to vote.");
} else {
Console.WriteLine("You are not eligible to vote.");
}
Determining the largest of three numbers
int num1 = 10, num2 = 20, num3 = 15;
if (num1 > num2 && num1 > num3) {
Console.WriteLine("The largest number is " + num1);
} else if (num2 > num1 && num2 > num3) {
Console.WriteLine("The largest number is " + num2);
} else {
Console.WriteLine("The largest number is " + num3);
}
Checking if a character is a vowel or a consonant
char letter = 'a';
if (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u') {
Console.WriteLine("The character is a vowel.");
} else {
Console.WriteLine("The character is a consonant.");
}
Checking if a number is divisible by 3 and 5
int number = 15;
if (number % 3 == 0 && number % 5 == 0) {
Console.WriteLine("The number is divisible by both 3 and 5.");
} else {
Console.WriteLine("The number is not divisible by both 3 and 5.");
}
Determining the day of the week
int dayOfWeek = 3;
if (dayOfWeek == 1) {
Console.WriteLine("It's Monday.");
} else if (dayOfWeek == 2) {
Console.WriteLine("It's Tuesday.");
} else if (dayOfWeek == 3) {
Console.WriteLine("It's Wednesday.");
} else if (dayOfWeek == 4) {
Console.WriteLine("It's Thursday.");
} else if (dayOfWeek == 5) {
Console.WriteLine("It's Friday.");
} else if (dayOfWeek == 6) {
Console.WriteLine("It's Saturday.");
} else {
Console.WriteLine("It's Sunday.");
}
Checking if a string is empty
string name = "";
if (string.IsNullOrEmpty(name)) {
Console.WriteLine("The name is empty.");
} else {
Console.WriteLine("The name is: " + name);
}
Determining the shipping cost based on the order total
double orderTotal = 50.0;
double shippingCost;
if (orderTotal >= 100.0) {
shippingCost = 0.0;
} else if (orderTotal >= 50.0) {
shippingCost = 5.0;
} else {
shippingCost = 10.0;
}
Console.WriteLine("Shipping cost: $" + shippingCost);
Checking if a user is an admin
bool isAdmin = true;
if (isAdmin) {
Console.WriteLine("Welcome, admin!");
} else {
Console.WriteLine("You are not an admin.");
}
Determining the discount based on the customer’s loyalty level
int loyaltyLevel = 3;
double discount;
if (loyaltyLevel == 1) {
discount = 0.05;
} else if (loyaltyLevel == 2) {
discount = 0.10;
} else if (loyaltyLevel == 3) {
discount = 0.15;
} else {
discount = 0.0;
}
Console.WriteLine("Discount: " + (discount * 100) + "%");
Checking if a file exists
string filePath = "example.txt";
if (File.Exists(filePath)) {
Console.WriteLine("The file exists.");
} else {
Console.WriteLine("The file does not exist.");
}
These examples cover a variety of use cases for the C# if else statement, including numerical comparisons, logical operations, and character-based checks.
External Sources
https://www.w3schools.com/cs/cs_conditions_elseif.php