Conditionals

Updated: 3 years ago

Check out other lessons in Learning the Java syntax

When we are building our applications, we need a way to make a decision on what to do. We can use conditionals to introduce forks in our logic.

Let's imagine we have a client that offers car rentals. They have a requirement that you have to be at least 25 years old to rent a car. So let's use a conditional to tell the user if they can rent a car or not.

package com.codescrubs;

public class Main
{

    public static void main(String[] args)
    {  
        // Let's hardcode the user's age
       int userAge = 33;

       // Age requirment to rent a car
       int ageRequirment = 25;

        // if something is true
       if(userAge >= ageRequirment)
       {
            System.out.println("You can rent a car.");
       }
    }
}

If we're 25 or older, we can rent a car. If statements work by checking if something is true or false and then executes the code within the code block (inside the {}). In our case, when userAge is either larger or equal than ageRequirment it resolves to true and executes the code. If it's smaller the expression returns false and it skips the code block.

Possible expressions

We can give to an if statement a lot of different expressions to work out and each of them will evaluate to a boolean. Above we used equals or larger and we can also do >,< and =<. Note that when we are checking for equality we use double ==, a single = is for assigning values. We can also use != if we're checking if something is not equal.

// equals
if(userAge == ageRequierment)
{
     // execute code if true
}

// not equals
if(userAge != ageRequierment)
{
     // execute code if true
}

else if and else

We know that code executes from top to bottom and when it comes to an if statement it checks the expression and then executes the given code. But sometimes we want to check more than one thing at a time and there's a couple of ways of doing that. Our car rental place has introduced a new requirement, you have to be below 70 years old to rent a car.

// Let's hardcode the user's age
int userAge = 33;

// Age requirment to rent a car
int ageMin = 25;
int ageMax = 80;

if(userAge < ageMin)
{
     System.out.println("You are too young. You need to be " + ageMin);
}
else if (userAge >= ageMax)
{
     System.out.println("You are too old. You need to be below " + ageMax);
}
else 
{
       System.out.println("You can rent a car.");
}

First we check if the user is below the required age. If they are not below the required age, we then check with the else if, if they are older than the required age. If they are not, they are within the required age limits and we output that they can rent a car.

Combining multiple expressions

You can also use || and && to combine different expressions to make a more complicated if statement. We use || to say OR and && to say AND.

// if a user is below minimum age OR above maximum age
if(userAge < ageMin || userAge >= ageMax)
{
      System.out.println("Sorry, you can not rent a car.");
}

// if a user is below maximum age AND above minimum age
if(userAge >= ageMin && userAge < ageMax)
{
     System.out.println("You can rent a car.");
}

Hint! Comparing strings

If you tried comparing two strings ("Monday" == "Monday"), you could see that it doesn't work as it should. That's because comparing strings (and other objects) works a bit differently. You have to call a method equals() and pass in another string to get the proper comparison. Don't worry, equals is included on every object by default so you just have to remember to use == for primitives and equals() for everything else.

String today = "Monday";

if(today.equals("Monday"))
{
      System.out.println("Today is Monday");
{
« Previous lesson:
Arrays
» Next lesson:
Loops