If statements

So far, all our programming has taken a linear form - every time we run the programme the same thing happens, with the only variant being if we output some data that we have read in using the scanner. Our code does not, as yet, take any decisions for us.

The simplest way to enable a decision to be made by the computer is to use an if statement. This allows us to check if a condition has been met (if the condition has been met, then it is said to be 'true' otherwise it is 'false'). If the condition has been met, then the computer will take some form of action. An analogy of this would be a bartender cashier checking if a customer is 18 (the condition) before selling them a Jagerbomb (the action). We could model this in code as follows:

int drinkingAge = 18;
int customerAge = 18;
if (customerAge == drinkingAge) {
    System.out.println("Here is your JagerBomb");
There are a few key elements to the 'If' statement, firstly the keyword if, followed by a statement in parentheses (round brackets). The statement above checks if the customerAge variable is equal to 18. Finally, a set of braces (curly brackets) are used to contain some code, which is only executed if the condition within the parentheses is met.


One common mistake new programmers often make is to confuse the assignment operator: = (used to set the value for a variable), with the equality check operator: ==. One way to remember which to use, is to think of the single equals sign as representing the word 'is', and the double equals to represent the words 'is equal'. In the above code we first create a variable called customerAge and say that it is 18, and we later check, in the 'if' statement, that the customer age is equal to 18.

Task 1

  1. Reproduce the above code in a Java programme
  2. Run it to confirm that the statement is output
  3. Change the value of the customerAge variable to 17
  4. Run the programme and check that the statement is not output

Greater than operators

You may have noticed that the above code is not particularly useful, in that if the customer is over 18 they will not get their Jagerbomb. This is because the check only confirms if the value is exactly 18. We can check if a value is over a certain value by using the greater than operator: >.

An example solution to this would appear as follows:

if (customerAge > drinkingAge) {
    System.out.println("Here is your JagerBomb");
Run the code, and for any value over 18 the Jagerbomb line will be output

Again you may notice another problem: if we were to translate this code into English we would say something like “if the customerAge is over 18, give them a Jagerbomb”. Whilst you or I would understand this correctly and give everyone who is '18 or over' the drink, the computer does not have the ability to understand that someone who is 18 may have a drink. Instead we have to be explicit about it. There are two potential solutions to the problem:

This leaves us with code that appears as follows
if (customerAge >= drinkingAge) {
    System.out.println("Here is your JagerBomb");

Task 2

Update the code, so that the system correctly recognises if the customer's age is 18 or over.

Less than operators

You may have already deduced that there are another couple of operators for checking if a value is less than another value, and indeed there are as follows:

These are used in the same way to the 'greater than' and 'greater than or equal to' operators.

Not equal operator

Sometimes we may want to check if something is not equal, for example, we could consider that a vehicle is moving if its speed is not 0. We can check something is not equal using the != operator. Example code might appears as follows:

if (vehicleSpeed != 0){
    System.out.println("Vehicle is moving");

Boolean values

Sometimes we might want to store the result of checking the condition of a variable, as we've seen above, the conditions are either met, or they are not - e.g. they are either true or false. A variable that can only every be true or false is known as a boolean value, we've seen how to declare these directly as either true or false in the Data types page, but we can also declare them to hold the value of the result of an expression. Taking the moving vehicle example above, we could create a Boolean variable to hold the true of false value corresponding to whether the vehicle is moving and then use that variable in the if statement instead (which would be more useful if we were going to use it again, or to make code more readable), here's an example:

boolean vehicleIsMoving = (vehicleSpeed != 0); //will be true or false
if (vehicleIsMoving) {
    System.out.println("Vehicle is moving");

Inverting (Negating) Boolean values

In the same way we can use the ! symbol to say not equal, we can use it to change a boolean value to its opposite, in the above example, if we wanted to check if the vehicle is not moving, we simply prefix the vehicleIsMoving variable in the if statement with an explanation mark as follows:

if (!vehicleIsMoving) {
    System.out.println("Vehicle is stationary");

Task 3 - true or false quiz

  1. Create a new command line project in IntelliJ
  2. Add code to print your name
  3. Add code to print a message with a truth about yourself (try and think of something interesting)
  4. Add code to print a message with a lie about yourself
  5. Flip a coin, and if you get 'head' modify the code so the lie appears before the truth
  6. Modify the code so that the first statement printed starts with '1. ' and the second starts with '2. '
  7. Add another line of code to ask the user to type 1 or 2 depending on which statement they think is correct
  8. Add code to read in an integer from the user (using the scanner)
  9. Use an 'if' statement to check if the user has guessed correcty, and notify them either way
  10. Ask one of your peers to try out your programme (and the tutor if you have one)
  11. Adapt your programme so it includes two or more lies

Task 4 - life expectancy

This task will create a simple application to generate life expectancy

  1. Create a new command line project in IntelliJ
  2. Add code to print a message asking if the user is male, informing them to type 'true' or 'false'
  3. Use the scanner to read in a boolean value (use the code completion to help you find the correct method), and store the boolean in a variable called isMale
  4. Create an if statement to check the isMale variable, and if this value is true, print out a message informing him that he could expect to live to around 79
  5. Test the programme and type 'true' (without the quotes) when prompted, verify the age message is displayed
  6. Test the programme again and type 'false' (again, no quotes), and verify no message appears
  7. Add another if statement, this time checking if the user is female (i.e. not male), if so, output a message informing her that she could expect to live until around 83. (Do not ask the user if they are female!)
  8. Test the programme again with both true and false as the input, and verify the correct message appears in each case