The if
and if-else
conditionals are fundamental control structures that allow making decisions based on boolean evaluations (true or false).
If you want to learn more about Conditionals
check the Introduction to Programming Course read more
The IF Conditional
The if
structure evaluates a boolean expression and executes a block of code only if the expression is true
. The basic syntax of an if
conditional in C# is:
if (condition)
{
// Code to execute if the condition is true
}
Let’s see it with an example,
int number = 10;
if (number > 5)
{
Console.WriteLine("The number is greater than 5");
}
In this example, the condition number > 5
evaluates to true
, so the message “The number is greater than 5” is printed to the console.
The IF ELSE Conditional
The if
conditional allows adding an else
block of alternative code that will execute if the if
condition is false
. The basic syntax is:
if (condition)
{
// Code to execute if the condition is true
}
else
{
// Code to execute if the condition is false
}
Let’s see it with an example,
int number = 3;
if (number > 5)
{
Console.WriteLine("The number is greater than 5");
}
else
{
Console.WriteLine("The number is not greater than 5");
}
In this case,
- The condition
number > 5
isfalse
- So the block of code inside the
else
executes - Therefore, “The number is not greater than 5” is printed to the console
The IF ELSE-IF Conditional
To evaluate multiple conditions, you can chain multiple if / else-if / else
blocks. This allows evaluating several conditions in sequence until one of them is true.
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 all previous conditions are false
}
Using Logical Operators in Conditionals
To evaluate multiple conditions within a single if
, you can use logical operators like &&
(logical AND) and ||
(logical OR).
For example, the &&
operator evaluates to true
only if both conditions are true.
int number = 10;
if (number > 5 && number < 15)
{
Console.WriteLine("The number is between 5 and 15");
}
While the ||
operator evaluates to true
if at least one of the conditions is true.
int number = 20;
if (number < 5 || number > 15)
{
Console.WriteLine("The number is less than 5 or greater than 15");
}
Practical Examples
Determine if a number is less than 5 or greater than 15
In this example, we determine if a number is less than 5, greater than 15, or within the intermediate range.
// Declare an integer variable
int number = 10;
// Evaluate if the number is greater than 15
if (number > 15)
{
// If it is greater than 15, print this message
Console.WriteLine("The number is greater than 15");
}
// Evaluate if the number is greater than 5 but not greater than 15
else if (number > 5)
{
// If it is greater than 5 but less than or equal to 15, print this message
Console.WriteLine("The number is greater than 5 but less than or equal to 15");
}
else
{
// If the number is not greater than 5, print this message
Console.WriteLine("The number is 5 or less");
}
Here, we sequentially evaluate the conditions:
number > 15
isfalse
- but
number > 5
istrue
- Therefore, “The number is greater than 5 but less than or equal to 15” is printed.
Determine the largest of three numbers
In this example, we find the largest of three numbers using if-else structures.
// Declare three integer variables and a variable to store the largest value
int a = 5, b = 10, c = 3;
int largest;
if (a > b && a > c) // Evaluate if 'a' is greater than 'b' and 'c'
{
largest = a; // If 'a' is the largest, assign 'a' to the variable 'largest'
}
else if (b > a && b > c) // Evaluate if 'b' is greater than 'a' and 'c'
{
largest = b; // If 'b' is the largest, assign 'b' to the variable 'largest'
}
else
{
largest = c; // If 'c' is the largest, assign 'c' to the variable 'largest'
}
// Print the largest number
Console.WriteLine($"The largest number is: {largest}");
Check grade ranges
In this example, we assign a letter grade based on a numeric score using a series of if-else conditions.
// Declare an integer variable for the grade and a string variable for the range
int grade = 85;
string range;
if (grade >= 90) // Evaluate if the grade is 90 or higher
{
range = "A"; // If the grade is 90 or higher, assign 'A' to the range
}
else if (grade >= 80) // Evaluate if the grade is 80 or higher
{
range = "B"; // If the grade is 80 or higher, assign 'B' to the range
}
else if (grade >= 70) // Evaluate if the grade is 70 or higher
{
range = "C"; // If the grade is 70 or higher, assign 'C' to the range
}
else if (grade >= 60) // Evaluate if the grade is 60 or higher
{
range = "D"; // If the grade is 60 or higher, assign 'D' to the range
}
else
{
range = "F"; // If the grade is less than 60, assign 'F' to the range
}
// Print the grade range
Console.WriteLine($"The grade is: {range}");