DEV Community

Cover image for C# - Conditional Statements and Logic
Grant Riordan
Grant Riordan

Posted on • Edited on

C# - Conditional Statements and Logic

Conditional Statements and Logic

If this..do this

When programming there will always be a case when you only want to do something, if certain conditions are met. This can be acccomplished in a couple of ways, lets take a look:

If Statement

The easiest way of checking conditions is utilising the If statement. If statements control the code execution flow. When it's compiled into native machine code, it will evaluate the condition and make a conditional jump. Is the β€œif” statement considered a method? No, even though to some, it may look like one, it is not a method. If is a reserved work in the C# language, which means it will always be treat as such.

Think of IF statement expressions is like stating a condition that has to be met.

Variable Condition Outcome
my age greater than or equal to 18 Legal to Drink Alcohol

How would this be written in code?
Alt Text

Lets deconstruct an if statement:

First you utilise the If reserved word, then open and close brackets, entering a condition expression inside, end with parenthesis , with the code you wish to run upon the condition being met.

Condiitonal Operators & Building Expressions

In order to write an expression for an If statement, you need to understand the conditional operators. These are:

Condition Operator Description
Equals == Checks if the left hand part of the expression is equal to the right
Not Equals != Checks if the left hand part of the expression is not equal to the right
Greater Than > Checks if the left hand part of the expression is greater than the right
Less Than < Checks if the left hand part of the expression is less than the right
Modulus % Returns if there is a remainder left after division
Greater Than Or Equal To >= Checks if the left hand part of the expression is greater than or equal to the right
Less Than Or Equal To <= Checks if the left hand part of the expression is less than or equal to the right

Lets see some examples of these being used...

Alt Text

Joining conditions

What do you do though if you want to join conditions? You can utilise logical operators, to join expressions.

Logical Condition Operator Description
Logical AND "&" Checks both the left and right sides of the expression are both true, will ALWAYS check both sides - sometimes can less productive
Conditional AND "&&" Only checks the right side of expression if the left is true. Otherwise it short-circuits to false, more efficient in most cases
OR || Checks if either of the sides of the expression are true

It might be looking at some common usage examples of these operators:

Alt Text

Here are some examples of how we could use the most common operators. We've joined multiple conditions using a variety of operators.

If / Else Statements

What if though, you want to do want the code only do something if a condition is met. What If you want to do something in one scenario but every other time do something else.

You can use the If / Else statement like so:

Alt Text

So if the condition is not met the else statement code will be executed. As you can see, you don't have to pass a full expression within the "If" statement. You can also pass a boolean value, and it will by default assess if the value is true. If you would like to check if the value is false you can prepend the value name name with ! operator. Which is the negate operator. It works in the same way as the " != " operator, however its a shorthand version for boolean values, like this "poor" example haha:

Alt Text

We can also chain other if statements off of one another using an else if, chain like so:

Alt Text

This will check the first condition, if that's not met, it will cascade to the next else if statement, if that isn't met it will fall to the else code.

The else code will always be executed if none of the conditions are met.

Sometimes though multiple if / else if / else statements can get messy but there is a solution.

This will be discussed in the next part of the series "Conditional Statements Continued - Switch / Case"

Top comments (0)