Lesson 7 – Conditional Statements

Lesson 7 – Conditional Statements

Ok, In the last lesson we learnt how to use the different types of Loops. But there is another way to change the control flow of our program called branching.

Whats control flow?

Most of the code you have written so far gets executed from top to bottom in the order that you have written it. Control flow statements, however, break up this flow by employing decision making, looping, and branching, enabling your program to execute particular blocks of code depending on whether or not certain conditions are met.


To get started you can use the basic starter project and follow the steps to compile the code examples for the lesson.

If Statements

Let’s dive straight in and set up a new class with a main method. The first example is a basic for loop:

So hopefully the syntax is familiar from the last lesson on loops. In plain English this code can be read:

“If x is greater or equal to zero. Execute the code inside the if statement (the curly braces {})”. If it is not greater or equal to zero don’t.

As always copy the code, call it from your main method and run it. Change the value of x to say -2 and rerun it. Do you see the output this time?

Another If statement

Ok, before we continue let’s have a look at another if condition. This one is going to pass a parameter into a method and based on the result of the if condition inside, return one of the two possible results:

If you have forgotten about the keyword ‘return’ or passing parameters to methods, please revisit lesson 5 on methods.

As you can see, this method takes in an int called grade and returns a String. Which string depends on the grade parameters value. If the grade is greater or equal to 40, then the if condition is true and it will execute the code inside the if condition. Thus returning the first string. If it is not >=40, then the if the condition is false, it will skip over the code enclosed in the if condition and it will return the second string.

To run the code add the following to the main method and change the grade parameter to return the different strings:

If … Else Statements

Ok, time for another conditional. This time, let’s look at the If .. Else condition:

Run the code from your main method and change the grade parameter. After you do this, you may be wondering what the difference is between the if else and the if condition we looked at previously.

The difference is that the if statement either executes or does not depending on the condition. Whereas, the if else condition must execute one of the two possible branches. i.e. it must print one of the two strings inside the if else condition.

Therefore if conditions give you the option to run something or not. If else’s make a choice which code block to execute.

If else if conditions

Following on from the ‘if else’ condition we are now going to have a look at the ‘if else if’ condition! This construct follows on quite naturally from the last example so let’s just take a look first:

So as you can see we can effectively join if else statements together. It’s important to know here however that only one if condition (or the final else) will ever be executed. In the case above thats a bit obvious since the code block inside each if has a return statement. Therefore once one if is true and the code is executed. It will return a value from the method.

But what about the following method?

If I pass in a grade of 80, you could easily be confused into thinking that the first if would set the String result then the second if would set the String result again. Since both of these if conditions are true. But, this is not the case. Java will execute the first true if condition and skip over the rest. Or, if no if condition is true only run the else block

Switch Statement

Switch statements are useful when there are many finite paths the code can go down. Unlike if and if else statements switch statements do not evaluate expressions such as greater than. A switch works with the byte, short, char, and int primitive data types. As well as Strings and Enums(we will cover these later). So let us take a look at our example:

This method converts a numerical representation of a Month and returns the name of the month as a string. So in this example, we are ‘switching’ on an int called monthOfTheYear. You can see there are 12 possible cases and one default case. This means that if we enter 1, case 1 will be matched and the month string will be set to January. If you enter the value 13, no case will be matched and the default case will be used.

When a pattern is matched and the code inside the case is executed we call break; This is something that we need to look out for. If we do not call break the code will move down into the next case statement! For example:

If we enter 5 we expect the method to return May. But in fact, case 5 will match and month will be set to May, then it will fall through into case 6 and set month to June and continue until there is a break or we have reached the end!

In some cases, this is perfectly acceptable, and we may want to use this behaviour. However, it is also the source of potential bugs in the real world.

At this stage, I would encourage you to spend some time writing your own conditional statements. There was a lot of information in this lesson and practice and hands on experience are the best way to learn.

Think of some real world problems and try to find the most suitable construct to implement it. Here are some examples to get you started:

  • Calculating Tax using bands that are based on a persons salary
  • Charge someone for a late return
  • Calculating postage based on distance
  • Converting months to seasons