If Statement
The if statement is the most frequently used branching statement. It allows you to selectively run code based on the value of a boolean expression called the condition. Every if statement includes a block of code that will only be executed when the condition is true. There is also an option else branch that tells the computer what code to execute when the condition is false. We call each of these paths a branch of code.
This concept is visualised in the following image.
If
Statement - when, why, and how
The if statement will be a regular feature in your programs. You will use this anywhere you want to optionally run a block of code. The condition has to be a boolean expression, which you can use comparison and logical operators to create. You then provide code within the if statement to you want to run when the condition is true. You can also optionally include an else branch containing code to run when the condition is false.
Remember that things still run in sequence, so you have to position the if statement within your sequence at the right location. The only change to sequence from what you have seen before is in the choice of branch, which depends on the value of the condition.
In C
The keyword for an if statement in C# is if
.
This is followed by the condition in brackets, and then the statement to run when the condition is true.
This can optionally be followed by the else
keyword and a statement to run when the condition is false.
To run more than one statement in either branch you can use a compound statement.
How does if
work?
Let’s work through a few examples to see how the if statement works. Pay attention to how the sequence of actions in the code is being affected. Understanding how this works will help you see how to make use of the if statement within your projects.
We will look at running code if a condition is true, running different code when it is false, and combining if statements for more than two paths.
Standalone if
The most basic use of an if statement is to selectively run a block of code when a condition is true. In the following code, we ask the user to tell us which language they use. Then the if allows us to check if the language is “C#”, and when that is true have the computer output a custom message. If anything else is entered, then the condition will be false and these statements will be skipped over.
Step through the following images to see key points about how this runs within the computer. Pay attention to the actions of the if statement itself.
If
with else
We can add else
to have a second branch that gets run only when the condition is false. The following code adds an else branch to output a different message when the user enters anything other than “C#“.
The else
adds to the actions the if statement needs to perform. Review the following images to see how this works.
Multiple if
statements combined
In many cases you will want more than two paths. To achieve this, you need to combine multiple if statements. This is shown in the following code, which gives different outputs for C and C++ as well.
Review the following images to see how the two if statements work together.
Reading code
The image sequences above should give you an idea of how to go about reading code. You need to be able to read through the code in the way it will run. To do this, you need to be able to picture certain values within your variables to see how the program’s execution would change.
In general, you should be able to approach this in blocks. This is why it is important to have these blocks flow into each other consistently.
Notice that the “Great chat!” message was always output — this is the statement after the if
statement. So the if
statement sits within a sequence, controlling things within its code but still working in the original sequence. It has one entry and one exit.