If statement in C#

HomeC#

If statement in C#

Bitwise Operators in C#
Field keyword in C#
Numeric data types in C#

Let’s dive into the if statement in C#.

The Basics of the if Statement

The if statement is a fundamental control flow construct in C# (and most programming languages). It allows you to execute a block of code only if a specified condition is true.

Syntax:

if (condition)
{
    // Code to execute if the condition is true
}
  • if: Keyword indicating the start of the if statement.
  • condition: A boolean expression (evaluates to true or false).
  • {}: Curly braces enclose the code block that will be executed if the condition is true.

Examples:

  1. Simple if Example:
int age = 25;
if (age >= 18)
{
    Console.WriteLine("You are an adult.");
}
  1. In this example, the message “You are an adult.” will be printed because age is greater than or equal to 18.

if...else Statement

The if...else statement extends the if statement by providing an alternative code block to execute when the condition is false.  

Syntax:

if (condition)
{
    // Code to execute if the condition is true
}
else
{
    // Code to execute if the condition is false
}

Example:

int age = 16;
if (age >= 18)
{
    Console.WriteLine("You are an adult.");
}
else
{
    Console.WriteLine("You are a minor.");
}

In this case, “You are a minor.” will be printed because age is less than 18.

if...else if...else Statement

When you need to check multiple conditions, you can use the if...else if...else statement.

Syntax:

if (condition1)
{
    // Code to execute if condition1 is true
}
else if (condition2)
{
    // Code to execute if condition2 is true (and condition1 is false)
}
else if (condition3)
{
    // Code to execute if condition3 is true (and condition1 and condition2 are false)
}
// ... more else if blocks ...
else
{
    // Code to execute if all conditions are false
}

Example:

int score = 85;
if (score >= 90)
{
    Console.WriteLine("A");
}
else if (score >= 80)
{
    Console.WriteLine("B");
}
else if (score >= 70)
{
    Console.WriteLine("C");
}
else if (score >= 60)
{
    Console.WriteLine("D");
}
else
{
    Console.WriteLine("F");
}

In this example, “B” will be printed because score is between 80 and 89.

Nested if Statements

You can also nest if statements within each other to check multiple levels of conditions.

Example:

int x = 10;
int y = 20;

if (x > 5)
{
    if (y > 15)
    {
        Console.WriteLine("x is greater than 5 and y is greater than 15.");
    }
    else
    {
        Console.WriteLine("x is greater than 5, but y is not greater than 15.");
    }
}
else
{
    Console.WriteLine("x is not greater than 5.");
}

Using Boolean Variables and Operators

You can use boolean variables and logical operators (&& for AND, || for OR, ! for NOT) to create more complex conditions.

Example:

bool isRaining = true;
bool isSunny = false;

if (isRaining && !isSunny)
{
    Console.WriteLine("It's raining, but not sunny.");
}
else if (isRaining || isSunny)
{
    Console.WriteLine("It's either raining or sunny (or both).");
}
else
{
    Console.WriteLine("It's neither raining nor sunny.");
}

Ternary Operator (Conditional Operator)

The ternary operator (?:) is a shorthand way to write simple if...else statements.

Syntax:

condition ? expressionIfTrue : expressionIfFalse;

Example:

int age = 20;
string message = age >= 18 ? "Adult" : "Minor";
Console.WriteLine(message); // Output: Adult

Important Considerations:

  • Always use curly braces {} to define code blocks, even for single-line if statements. It improves readability and avoids potential errors.
  • Ensure your conditions are boolean expressions (evaluate to true or false).
  • Pay attention to the order of else if conditions, as the first matching condition will be executed.
  • Use logical operators to combine multiple conditions effectively.

COMMENTS

DISQUS: 0