Conditional statements allow a program to perform different actions based on certain conditions. They are usually written in the form of an "if-then" statement, where a block of code only runs if a particular condition is true.

In C#, there are different ways you can write conditional statements. This includes standard if-else statements, switch statements, ternary operators, and nullable ternary operators.

How to Write Conditional Statements in C#

There are different ways to write conditional statements in JavaScript, Java, C++, Python, and other languages. In C#, you can start practicing these statements in a simple C# console application in Visual Studio.

Standard if/else-if/else Statement

An if-statement tests for a condition that evaluates to true. If the condition evaluates to true, the code within the block will execute. Otherwise, it will not.

        int age = 25;

if (age >= 18)
{
  Console.WriteLine("You are an adult.");
}

You can add additional else-if blocks if you want another block of code to execute for a different set of conditions. An else block will run when none of the previous conditions evaluate to true.

        int age = 25;

if (age >= 18)
{
  Console.WriteLine("You are an adult.");
}
else if (age >= 13)
{
  Console.WriteLine("You are a teenager.");
}
else
{
  Console.WriteLine("You are a child.");
}

In the above example, the first if statement is true and runs the first code block. If the value of the age variable was 15, the program would run the second code block instead. If the age variable was 8, the program would run the code inside the else block.

Single Line if Statement (Without Braces)

If the code block you want to execute only consists of one line, you can remove the curly braces after the if condition:

        if (age >= 18)
  Console.WriteLine("You are an adult.");

This also applies to else-if and else blocks:

        if (age >= 18)
  Console.WriteLine("You are an adult.");
else if (age >= 13)
  Console.WriteLine("You are a teenager.");
else
  Console.WriteLine("You are a child.");

Switch Statement

A switch statement in C# can be a more convenient way to organize conditions if you want to avoid using too many if statements.

In a switch, you can input a value to compare against multiple cases. Cases include the possible options that the value could match.

        int score = 4;
char grade;

switch (score)
{
  case 5:
   grade = 'A';
   break;
 case 4:
   grade = 'B';
   break;
 case 3:
   grade = 'C';
   break;
 case 2:
   grade = 'D';
   break;
 case 1:
   grade = 'E';
   break;
 default:
   grade = 'F';
   break;
}

Console.WriteLine("Your grade is: " + grade);

Ternary Operator

A ternary operator is a shorthand way of writing an if-statement in C#. It follows this syntax:

        condition ? code when true : code when false
    

The statement includes the condition, followed by a question mark. On the left side of the colon is code the program will run when the condition is true. On the right of the colon is the code the program will run when the condition is false.

        int result = 49;
var message = result > 50 ? "You passed!" : "You failed!";
Console.WriteLine(message);

Nullable Ternary Operator

You can use the nullable ternary operator to assign a value to a variable that may be null. It uses the following syntax:

        var result = value ?? defaultValue
    

The example below assigns the value of the num variable to the number variable. If the num variable is null, it will assign the default value of 0 to the number variable instead.

        int? num = null;
int number = num ?? 0;

Using Conditional Statements in C#

You can use conditional statements to create applications that can respond to specific conditions. When building your app, it is also important to consider other structures that can make your code more efficient.