Conditional statements

General

Not all the code represents simple equations, sometimes it has to “choose” one or another route of execution depending on the input data (or something else). That’s when conditional statements come to rescue. Lets see a simple example with method which decides to print or not some line of text depending on value of parameter number.

public static void main(String... args) {
    checkNumber1(6);
}

private static void checkNumber1(int number) {
    if (number > 5) {
        System.out.println("Number " + number + " is greater than 5");
    }
}

Generally conditional expression consists of the condition itself ( number > 5 in our case) and block of code that should be executed on if condition returns true (System.out.println… , but there can be any quantity of lines of code or calls to other methods). Condition should be an expression that returns boolean value (true, x != 1, x || y – are examples of valid conditions).

Sometimes we also require to perform some actions both when condition is true and when its false. Then we should add else to our code. Rules are simple: when condition returns true if code block (branch) gets executed otherwise else is executed. The whole if-else statement is deterministic – only one of it’s branches can be executed at one time, never none, never both.

private static void checkNumber2(int number) {
        if (number > 5) {
            System.out.println("Number " + number + " is greater than 5");
        } else {
            System.out.println("Number " + number + " is less than or equals 5");
        }
}

For more complex expressions else if blocks should be added. They work the same as first if and you can add as much of them as required.

private static void checkNumber3(int number) {
        if (number > 5) {
            System.out.println("Number " + number + " is greater than 5");
        } else if (number < 5) {
            System.out.println("Number " + number + " is less than 5");
        } else {
            System.out.println("Number " + number + " equals 5");
        }
}

You always can rewrite such if-else block with separate ifs

private static void checkNumber3simple(int number) {
        if (number > 5) {
            System.out.println("Number " + number + " is greater than 5");
        } 
        if (number < 5) {
            System.out.println("Number " + number + " is less than 5");
        }
        if (number == 5) {
            System.out.println("Number " + number + " equals 5");
        }
}

But there are some differences:

  • else-if shows that always one of the blocks get executed. In case of multiple if blocks – from 0 to all blocks can be executed.
  • else-if is lazy meaning that conditions are check only before any of them returns true. In case of multiple if blocks – al conditions are checked.

You should choose the right option depending on your needs.

Conditions

You could merge multiple conditions using logical operators.

private static void checkNumber4(int number) {
        if (number >= 5 && number <= 10) {
            System.out.println("Number " + number + " is between 5 and 10");
        }
}

Example of using method as condition

private static void checkNumber5(int number) {
        if (isNumberEven(number)) {
            System.out.println("Number " + number + " is even");
        }
}

private static boolean isNumberEven(int number) {
        return number % 2 == 0;
}

Any condition could be inverted using ! operator.

private static void checkNumber6(int number) {
        if (!isNumberEven(number)) {
            System.out.println("Number " + number + " is odd");
        }
}

Ternary operator

Sometimes you have such conditions as in the example

private static int limitNumber(int number) {
        if (number < 5) {
            return number;
        } else {
            return 5;
        }
}

If conditions match the following conditions that means they could be converted to ternary operator:

  • has both if and else branches
  • both branches return value as result

Ternary operator uses special symbol ? to describe an if branch and symbol : to describe an else branch. More complex branching could be implemented by nesting other ternary expression inside first, but it’s not recommended, because it decreases code readability). Example of ternary expression that do the same as previous code example:

private static int limitNumberTernary(int number) {
        return number < 5 ? number : 5;
}
Scroll to Top