If Statements In C#

One of the basics of C# and an extremely useful and common piece of code, understanding If statements is vital for good developments.

Vincent Taylor
2 min readAug 14, 2021

IF:

In the C# programming language, the IF statement is a logical condition check. Its function is to check if a condition is true, and if so, run some specified code.

If Examples

ELSE IF:

The ELSE-IF statement can appear after an If statement, and specifies a second condition to check for once the first has been evaluated as False.

The code inside an Else-If statement runs ONLY IF the precedingIf statement is false.

Else-If Example

ELSE:

The ELSE statement can be attached to the end of an IF/ELSE-IF chain. Like the name implies, if all IF and ELSE-IF conditions are False, the code contained in ELSE will run.

Else Example

Summary:

The use cases for any type of IF statement are numerous, and it is usually the go-to method to run code based on conditions.

Simplification:

There is also a way to simplify the writing of IF statements even more. It is called a Ternary Operator, and is written like so:

Use the Ternary Operator to simplify IFs

--

--

Vincent Taylor

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer for 10+ years. Currently looking for games industry employment.