Skip to content

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 lets you selectively run a branch of code

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.

using static System.Console;
string language;
Write("What language do you use? ");
language = ReadLine();
if (language == "C#")
{
WriteLine("Good choice, C# is a fine language.");
}
WriteLine("Great chat!");

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.

Shows the state in the computer where the language variable has the data 'C#' and the code is about to run line 4.
Shows the state in the computer with C# in the language, and the code starting to run the if statement.
Shows the state of the computer running the sequence in the if statement.
Line 8 runs and prings the output 'Great chat!' to the terminal. The program counter is incremented, and the program ends
Shows the state of the computer back at line 4 with 'C++' in the language.
Line 8 runs, and outputs 'Great chat!' to the terminal. The program counter is incremented, and the program ends

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#”.

using static System.Console;
string language;
Write("What language do you use? ");
language = ReadLine();
if (language == "C#")
{
WriteLine("Good choice, C# is a fine language.");
}
else
{
WriteLine("Well... good luck with that!");
}
WriteLine("Great chat!");

The else adds to the actions the if statement needs to perform. Review the following images to see how this works.

Shows the state of the computer at line 4 with 'C#' in the language variable.
Shows the cpu runs line 5-7 - the true branch.
Shows the cpu at the else line - line 8
Shows the cpu at line 12
Shows the program back to line 4, with 'C++' in the language
Shows the program running lines 9 to 11 as the statements within the else branch
Shows the program at line 12 after the if statement.

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.

using static System.Console;
string language;
Write("What language do you use? ");
language = ReadLine();
if (language == "C#")
{
WriteLine("Good choice, C# is a fine language.");
}
else if ((language == "C") || (language == "C++"))
{
WriteLine("These are great languages.");
}
else
{
WriteLine("Well... good luck with that!");
}
WriteLine("Great chat!");

Review the following images to see how the two if statements work together.

Shows the language is set to C# after running lines 1 to 3
Shows the computer will run lines 4 to 7
The computer is at line 8 with the else part highlighted
Back to the start, with C++ now in the language
Shows line 4 testing the condition and jumping to the else branch
Shows the program evaluating the second if statements condition on line 8
Shows the statements in the second if statements true branch running
The else must jump the second if's else branch
Back to the start again, with Fortran in the language
Line 4 is run, which jumps to line 8
Line 8 is run, jumping to line 13
Lines 13 to 15 and 16 then run

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.