[C#] C# If … Else

Date:     Updated:

Categories:

Tags:

📋 This is my note-taking from what I learned in the c# tutorials!


C# Conditions and If Statements

C# supports the usual logical conditions from mathematics:

  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to: a == b
  • Not Equal to: a != b

C# has the following conditional statements:

  • Use “if” to specify a block of code to be executed, if a specified condition is “true”
  • Use “else” to specify a block of code to be executed, if the same condition is “false”
  • Use “else if” to specify a new condition to test, if the “first condition” is “false”
  • Use “switch” to specify many alternative blocks of code to be executed

The "if" Statement

Use the “if” statement to specify a block of C# code to be executed if a condition is “True”.

Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.

Syntax

if (condition)
{
  // block of code to be executed if the condition is True
}

In the example below, we test two values to find out if 20 is greater than 18. If the condition is True, print some text:

if (20 > 18)
{
  Console.WriteLine("20 is greater than 18");
}
// Output: 20 is greater than 18

We can also test variables:

int x = 20;
int y = 18;
if (x > y)
{
  Console.WriteLine("x is greater than y");
}
// Output: x is greater than y

The "else" Statement

Use the “else” statement to specify a block of code to be executed if the condition is “False.”

Syntax

if (condition)
{
  // block of code to be executed if the condition is True
}
else
{
  // block of code to be executed if the condition is False
}
int time = 20;

if (time < 18)
{
  Console.WriteLine("Good day.");
}
else
{
  Console.WriteLine("Good evening.");
}
// Output: Good evening.

The "else if" Statement

Use the “else if” statement to specify a new condition if the first condition is “False”.

Syntax

if (condition1)
{
  // block of code to be executed if condition1 is True
}
else if (condition2)
{
  // block of code to be executed if the condition1 is false and condition2 is True
}
else
{
  // block of code to be executed if the condition1 is false and condition2 is False
}
int time = 22;

if (time < 10)
{
  Console.WriteLine("Good morning.");
}
else if (time < 20)
{
  Console.WriteLine("Good day.");
}
else
{
  Console.WriteLine("Good evening.");
}
// Output: Good evening.

Short Hand If...Else (Ternary Operator)

There is also a short-hand if else, which is known as the ternary operator because it consists of three operands.

It can be used to replace multiple lines of code with a single line.

It is often used to replace simple if else statements:

Syntax

variable = (condition) ? expressionTrue :  expressionFalse;

Instead of writing:

int time = 20;

if (time < 18)
{
  Console.WriteLine("Good day.");
}
else
{
  Console.WriteLine("Good evening.");
}
// Output: Good evening.

You can simply write:

int time = 20;

string result = (time < 18) ? "Good day." : "Good evening.";
Console.WriteLine(result);
// Output: Good evening.


Nested If-Statement Comparison

Example of a simple If-Statement

//only one if statement

if («boolean_expression»)
{
  c# statements
}
else
{
  c# statements
}


Example of two If-Statement

//first if statement

if («boolean_expression»)
{
  c# statements
}
else
{
  c# statements
}

//second if statement

if («boolean_expression»)
{
  c# statements
}
else
{
  c# statements
}


Example of a Nested If-Statement

Nested If-Statement inside of the true block

//first if statement

if («boolean_expression»)
{

//another if statement in the true block
  if («boolean_expression»)
  {
    c# statements
  }
  else
  {
    c# statements
  }

}
else
{
  c# statements
}

Nested If-Statement inside of the false block

//first if statement

if («boolean_expression»)
{
  c# statements
}
else
{

//another if statement in the false block
  if («boolean_expression»)
  {
    c# statements
  }
  else
  {
    c# statements
  }

}


Nested If vs. Else If

Nested If

//first if statement

if («boolean_expression»)
{
  c# statements
}
else
{

//another if statement in the false block
  if («boolean_expression»)
  {
    c# statements
  }
  else
  {
    c# statements
  }

}

Else If

//first if statement

if («boolean_expression»)
{
  c# statements
}
else if («boolean_expression»)
{
  c# statements
}
else
{
  c# statements
}
  • Earlier we saw that an if-statement splits the control flow and processing only continues in one of the two routes.
    • This two-way split is enough to do everything that is doable.
  • If another if-statement is placed within either the true block or the false block, we have a nested if.
  • Of course, it is possible to have an if in both the true block and the false block or even more deeply nested if statements.
  • Nested if simplifies the assertion and decreases the number of if- statements necessary to realize the same logic.
  • A nested if-statement can be rewritten without nesting!




Back to Top

See other articles in Category CS

Leave a comment