Language: EN

csharp-condicional-if-else

What is and how to use the conditional IF-ELSE in C#

The if and if-else conditionals are fundamental control structures that allow making decisions based on Boolean evaluations.

The IF Conditional

The if structure evaluates a Boolean expression and executes a code block only if the expression turns out to be true. The basic syntax of an if conditional in C# is:

if (condition)
{
    // Code to be executed 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 number > 5 condition is evaluated as 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 alternative else code block that will be executed if the if condition is false. The basic syntax is:

if (condition)
{
    // Code to be executed if the condition is true
}
else
{
    // Code to be executed 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 number > 5 condition is false
  • So the code block inside the else is executed
  • Therefore, “The number is not greater than 5” is printed to the console

The IF ELSE-IF Conditional

To evaluate multiple conditions, multiple if / else-if / else blocks can be chained. This allows evaluating several conditions in sequence until one of them is true.

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 all previous conditions are false
}

Using Logical Operators in Conditionals

To evaluate multiple conditions within a single if, logical operators such as && (logical AND) and || (logical OR) can be used.

For example, the && operator evaluates as 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 as 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 in the intermediate range.

// We declare an integer variable
int number = 10;

// We evaluate if the number is greater than 15
if (number > 15)
{
    // If it is greater than 15, we print this message
    Console.WriteLine("The number is greater than 15");
}
// We 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, we 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, we print this message
    Console.WriteLine("The number is 5 or less");
}

Here, we sequentially evaluate the conditions:

  • number > 15 is false
  • but number > 5 is true
  • So “The number is greater than 5 but less than or equal to 15” is printed.

Determine the greatest of three numbers

In this example, we find the greatest of three numbers using if-else structures.

// We declare three integer variables and a variable to store the greatest value
int a = 5, b = 10, c = 3;
int greatest;

if (a > b && a > c)  // We evaluate if 'a' is greater than 'b' and 'c'
{
    greatest = a;  // If 'a' is the greatest, we assign 'a' to the 'greatest' variable
}

else if (b > a && b > c)  // We evaluate if 'b' is greater than 'a' and 'c'
{  
    greatest = b;  // If 'b' is the greatest, we assign 'b' to the 'greatest' variable
}
else
{  
    greatest = c;  // If 'c' is the greatest, we assign 'c' to the 'greatest' variable
}

// We print the greatest number
Console.WriteLine($"The greatest number is: {greatest}");

Check rating ranges

In this example, we assign a rating letter based on a numerical score using a series of if-else conditions.

// We declare an integer variable for the rating and a string variable for the range
int rating = 85;
string range;

if (rating >= 90)  // We evaluate if the rating is 90 or greater
{
    range = "A"; // If the rating is 90 or greater, we assign 'A' to the range
}
else if (rating >= 80)  // We evaluate if the rating is 80 or greater
{
    range = "B";  // If the rating is 80 or greater, we assign 'B' to the range
}
else if (rating >= 70) // We evaluate if the rating is 70 or greater
{
    range = "C"; // If the rating is 70 or greater, we assign 'C' to the range
}
else if (rating >= 60)  // We evaluate if the rating is 60 or greater
{
    range = "D"; // If the rating is 60 or greater, we assign 'D' to the range
}
else
{
    range = "F"; // If the rating is less than 60, we assign 'F' to the range
}

// We print the rating range
Console.WriteLine($"The rating is: {range}");