Files
freeCodeCamp/guide/english/csharp/if-else-statement/index.md
Nitin Sharma f743f1e732 Fixed example (#32827)
Replaced assignment operator with the equality operator.
Fixed example structure.
Improved language.
2019-04-23 08:04:48 -07:00

926 B

title
title
If Else Statement

If...Else Statement

The If...Else statement executes different blocks of code depending on the truthfulness of the specified condition.

Syntax

if (boolean expression)
{
   // execute this code block if expression evalutes to true
}
else
{
   // always execute this code block when above if expression is false
}

Example

int Price = 30;

if (Price == 30)
{
    Console.WriteLine("Price is equal to 30.");
}
else 
{
    Console.WriteLine("Price is not equal to 30.");
}

Since we already declared our integer Price to be 30, this will be the expected output.

Output

Price is equal to 30.

Shortened If...Else Statement

We can use the ternary :? which is great for short if...else statements.

For example:

int Price = 30;
(Price == 30) ? Console.WriteLine("Price is Equal to 30.") : Console.WriteLine("Price is Not Equal to 30.")