C# Beginners – If Else Selection

C# Logo - C# Beginners - If Else Selection
C# Logo


C# Beginners – If Else Selection; is a tutorial on using ‘if’, ‘else if’ and ‘else’ to perform selections in C#. This is the first of three posts covering selection. Another two will follow, covering switch statements and try/catch blocks. This tutorial has a video version at the bottom of the page or by clicking here.

Firstly, you are assumed to be comfortable with your IDE of choice and know how to run a C# program. Additionally, you are assumed to know how to create string, integer and double-type variables. If not, please view the list of previous tutorials at the bottom of this blog or visit the C# Beginners Series Introduction page.

If/Else If/Else use expressions that are evaluated as being either true or false. For Example:

If the sun is shining, then output “the sun is shining”; else, output “the sun is not shining”.

First, you look at the statement “the sun is shining” and decide if that is true or false.

If it is true, then output “the sun is shining”; else, output “the sun is not shining”.

Booleans – C# Beginners – If Else Selection

Before we talk about making choices (selection) in C# (or almost every other programming language), it is beneficial to understand boolean data types. Boolean values are either ‘true’ or ‘false’. Therefore, they are values with only two states. Finally, sometimes boolean values are represented as on/off or 0/1. Essentially these are all the same thing. In C#, booleans are ‘true’ or ‘false.

Boolean types are specified by the word ‘bool’ in C#. Like every other data type, when declaring a boolean value, you select the type, then the variable name, and assign a value to it. E.g.

bool isTrue = false;

Expressions – C# Beginners – If Else Selection

We can write an expression or statement and then say whether that expression is ‘true’ or ‘false’. Those expressions use a few operators to perform a comparison.

>Greater Than
<Less Than
==Equivalent To
>=Greater Than or Equal To
<=Less Than or Equal To
Comparison Operator Table

Using these operators, we can write expressions and then decide if the expression is true or false. E.g.

ExpressionBoolean (Bool)Description
3 == 3True3 is equivalent to 3
3 > 3False3 is greater than 3
3 < 3False3 is less than 3
3 >= 3True3 is greater than or equal to 3
3 <= 3True3 is less than or equal to 3
Numerical comparison expressions.

Similar operations can be performed with strings but only using the equivalent operator. Additionally, the comparison is sensitive to letters being upper or lower case and invisible characters such as space and new lines. E.g.

ExpressionBoolean (Bool)Description
‘name’ == ‘name’True‘name’ is equivalent to ‘name’.
‘name’ == ‘Name’False‘name’ is equivalent to ‘Name’.
‘name’ == ‘ name’False‘name’ is equivalent to ‘ name’.
String comparison expressions.

Writing If/Else blocks in C#

Now let us look at some more code. The example below sets two integer values and then uses an expression to make a choice to decide if the first number is larger than the second.

int num1 = 10;
int num2 = 20;

if (num1 > num2)
    Console.WriteLine(num1 + " is bigger than " + num2);
    Console.WriteLine(num1 + " is not bigger than " + num2);

Firstly, notice that the expression to evaluate goes inside () brackets. So there is a code block that is signified by {}. Secondly, the output to the console is done inside the code block. Thirdly, there can be many lines of code inside a code block. Finally, a code block signifies that it is controlled by the preceding code and does not affect the rest of the program. The correct term for this is ‘scope’. I will cover the scope in more detail in a few tutorials time.

Remember that it evaluates the expression as a boolean and then performs the selection. Consequently, it is possible to write the code differently. However, the code below performs precisely the same operation as the code above.

int num1 = 10;
int num2 = 20;

bool num1isBigger = num1 > num2;

if (num1isBigger)
    Console.WriteLine(num1 + " is bigger than " + num2);
    Console.WriteLine(num1 + " is not bigger than " + num2);

Including Else If – C# Beginners

Next, let us try another example, setting a minimum, maximum, and range of allowed ages.

int minAge = 5;
int maxAge = 10;
int userAge = 7;

if (userAge < minAge)
    Console.WriteLine("Too Young");
else if (userAge <= maxAge)
    Console.WriteLine("Too Old");

In the code above, the first ‘if’ filters out any results where the user age is less than the min age.

The ‘else if’ already knows if the code has got this far; the user must be at least the min age. Therefore it only needs to check against the max-age. The conditional for the else if statement checks to see if the user age is less than or equivalent to the maximum age.

Finally, anything not covered by the ‘if’ or the ‘else if’ gets caught up with the else. There is no conditional; it is literally anything not specified in one of the above selections.

Multiple operators in an expression – Sorry, but no.

Programmers from other languages like Python may have seen code like the example below.

Video Version – C# Beginners – If Else Selection

min = 5
max = 10
user_age = 7

age_ok = bool(min <= user_age <= max)

if age_ok:

There is no equivalent to this in C#. This operation requires the use of logic operators.

And, Or, Not. These will be covered in a later tutorial.

Related Articles – C# Beginners – If Else Selection

Related Post

9 thoughts on “C# Beginners – If Else Selection

Leave a Reply

Your email address will not be published. Required fields are marked *