Mastering Conditional Statements in C#: Unlocking the Power of Decision-Making

The if Statement: A Simple yet Powerful Tool

The if statement is the most basic form of conditional statement in C#. It allows your program to execute a block of code if a specific condition is true.

if (boolean-expression) { code to execute }

Here’s how it works:

  • The boolean-expression is evaluated, returning either true or false.
  • If the expression is true, the code inside the if block is executed.
  • If the expression is false, the code inside the if block is ignored.

Example 1: A Simple if Statement


int number = 2;
if (number < 5)
{
    Console.WriteLine("The value of number is less than 5");
}

In this example, the output will be “The value of number is less than 5” because the condition number < 5 is true.

Taking it to the Next Level: if…else Statements

What if you want to execute a different block of code if the condition is false? That’s where the if…else statement comes in.

if (boolean-expression) { code to execute } else { alternative code }

Here’s how it works:

  • The boolean-expression is evaluated, returning either true or false.
  • If the expression is true, the code inside the if block is executed.
  • If the expression is false, the code inside the else block is executed.

Example 2: if…else Statement


int number = 12;
if (number < 5)
{
    Console.WriteLine("The value of number is less than 5");
}
else
{
    Console.WriteLine("The value of number is greater than or equal to 5");
}

In this example, the output will be “The value of number is greater than or equal to 5” because the condition number < 5 is false.

Handling Multiple Conditions: if…else if Statements

What if you have multiple conditions to test and execute different blocks of code? That’s where the if…else if statement comes in.

if (boolean-expression) { code to execute } else if (another-expression) { alternative code } else { default code }

Here’s how it works:

  • The boolean-expression is evaluated, returning either true or false.
  • If the expression is true, the code inside the if block is executed.
  • If the expression is false, the code inside the else if block is executed.
  • If none of the expressions are true, the code inside the else block is executed.

Example 3: if…else if Statement


int number = 12;
if (number < 5)
{
    Console.WriteLine("The value of number is less than 5");
}
else if (number > 5)
{
    Console.WriteLine("The value of number is greater than 5");
}
else
{
    Console.WriteLine("The value of number is equal to 5");
}

In this example, the output will be “The value of number is greater than 5” because the condition number > 5 is true.

Nested if…else Statements: A Powerful Combination

What if you need to test one condition followed by another? That’s where nested if…else statements come in.

if (boolean-expression) { if (another-expression) { code to execute } else { alternative code } } else { default code }

Here’s how it works:

  • The outer if statement is evaluated, returning either true or false.
  • If the expression is true, the code inside the if block is executed.
  • If the expression is false, the code inside the else block is executed.
  • Inside the if block, another if statement is evaluated, returning either true or false.
  • If the inner expression is true, the code inside the inner if block is executed.
  • If the inner expression is false, the code inside the inner else block is executed.

Example 4: Nested if…else Statement


int num1 = 10;
int num2 = 20;
int num3 = 30;

if (num1 > num2)
{
    if (num1 > num3)
    {
        Console.WriteLine("num1 is the largest");
    }
    else
    {
        Console.WriteLine("num3 is the largest");
    }
}
else
{
    if (num2 > num3)
    {
        Console.WriteLine("num2 is the largest");
    }
    else
    {
        Console.WriteLine("num3 is the largest");
    }
}

In this example, the output will be “num3 is the largest” because the condition num1 > num2 is false, and the condition num2 > num3 is also false.

Leave a Reply