Welcome back! In this article, we’ll explore the if…else condition in C# and demonstrate how to use it effectively. Conditional statements like if…else are fundamental constructs in programming that allow you to make decisions based on certain conditions. Let’s dive into the details.
What is an if…else Condition?
The if…else condition is used to execute a block of code if a specified condition is true. If the condition is false, another block of code can be executed (the else block). This allows you to control the flow of your program based on different scenarios.
Syntax of if…else Condition
Here’s the basic syntax of an if…else statement in C#:
if (condition)
{
// Code to execute if the condition is true
}
else
{
// Code to execute if the condition is false
}
- if: The keyword used to specify a condition.
- condition: A boolean expression that evaluates to true or false.
- else: The keyword used to specify an alternative block of code to execute if the condition is false.
Example: Basic if…else Condition
Let’s start with a simple example that demonstrates the basic usage of an if…else condition:
using System;
class IfElseDemo
{
static void Main(string[] args)
{
int number = 10;
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else
{
Console.WriteLine("The number is not positive.");
}
}
}
In this example:
- The variable
number
is set to 10. - The if statement checks if
number
is greater than 0. - If the condition is true, it prints “The number is positive.”
- If the condition is false, it prints “The number is not positive.”
Example: if…else if…else Condition
You can also use multiple conditions with the else if statement. This allows you to check multiple conditions in sequence.
Syntax of if…else if…else Condition
if (condition1)
{
// Code to execute if condition1 is true
}
else if (condition2)
{
// Code to execute if condition1 is false and condition2 is true
}
else
{
// Code to execute if both condition1 and condition2 are false
}
Example:
using System;
class IfElseIfDemo
{
static void Main(string[] args)
{
int number = 0;
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else if (number < 0)
{
Console.WriteLine("The number is negative.");
}
else
{
Console.WriteLine("The number is zero.");
}
}
}
In this example:
- The variable
number
is set to 0. - The if statement checks if
number
is greater than 0. - If the condition is true, it prints “The number is positive.”
- If the first condition is false, the else if statement checks if
number
is less than 0. - If the else if condition is true, it prints “The number is negative.”
- If both conditions are false, the else statement prints “The number is zero.”
Nested if Statements
You can also nest if statements inside other if statements. This allows you to create more complex conditions.
Example:
using System;
class NestedIfDemo
{
static void Main(string[] args)
{
int number = 10;
bool isEven = number % 2 == 0;
if (number > 0)
{
if (isEven)
{
Console.WriteLine("The number is positive and even.");
}
else
{
Console.WriteLine("The number is positive and odd.");
}
}
else
{
Console.WriteLine("The number is not positive.");
}
}
}
In this example:
- The variable
number
is set to 10. - The variable
isEven
is a boolean that checks ifnumber
is even. - The outer if statement checks if
number
is greater than 0. - If true, the nested if statement checks if
number
is even. - If both conditions are true, it prints “The number is positive and even.”
- If the nested condition is false, it prints “The number is positive and odd.”
- If the outer condition is false, it prints “The number is not positive.”
Example Program: Using if…else in a Real Scenario
Here’s a simple program that demonstrates using if…else statements in a real-world scenario:
using System;
class GradeEvaluation
{
static void Main(string[] args)
{
Console.WriteLine("Enter your grade:");
string input = Console.ReadLine();
int grade;
if (int.TryParse(input, out grade))
{
if (grade >= 90)
{
Console.WriteLine("Your grade is A.");
}
else if (grade >= 80)
{
Console.WriteLine("Your grade is B.");
}
else if (grade >= 70)
{
Console.WriteLine("Your grade is C.");
}
else if (grade >= 60)
{
Console.WriteLine("Your grade is D.");
}
else
{
Console.WriteLine("Your grade is F.");
}
}
else
{
Console.WriteLine("Invalid input. Please enter a numeric grade.");
}
}
}
In this program:
- The user inputs their grade.
- The program checks if the input is a valid integer using
int.TryParse
. - Based on the grade, it evaluates and prints the corresponding letter grade using if…else if…else statements.
Conclusion
The if…else condition is a fundamental construct in C# that allows you to control the flow of your program based on different conditions. By understanding and using if…else statements, you can make your programs more dynamic and responsive.
Practice using if…else statements in your own projects to become more comfortable with their syntax and behavior. Stay tuned for more tutorials to further enhance your C# programming skills. Happy coding!