How do I get the remainder of a division?

The remainder or modulus operator (%) let you get the remainder of a division of two numbers. This operator can be used to obtain a reminder of an integer or floating point types.

package org.kodejava.basic;

public class RemainderOperatorDemo {
    public static void main(String[] args) {
        int a = 10;
        double b = 49;

        // The reminder operator (%) gives you the remainder of
        // an integer or floating point division operation.
        System.out.println("The result of " + a + " % 5 = " + (a % 5));
        System.out.println("The result of " + b + " % 9.5 = " + (b % 9.5));
    }
}

Here is the result of the program:

The result of 10 % 5 = 0
The result of 49.0 % 9.5 = 1.5

How do I use the ternary operator?

The ternary operator or conditional operator can be use as a short version of the if-then-else statement. When you have a simple if-then-else statement in your code that return a value you might use the ternary operator, it can make your code easier to read.

The ternary operator is written using the symbol of ?: and it has the following syntax:

result = testCondition ? value1 : value2;

When the test condition evaluates to true the expression value1 will be returned else the expression value2 will be returned. The value1 or value2 is not only for a simple field or variable, it can be a call to a method for example. But it is advisable to use the ternary operator for a simple thing, because if you over do it, it will make your code harder to read.

Let’s see the following code:

package org.kodejava.basic;

public class TernaryOperatorDemo {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;

        // Get the maximum value
        int min = a < b ? a : b;

        // The use of ternary operator above is an alternative
        // of the following if-then-else statement.
        int minValue;
        if (a < b) {
            minValue = a;
        } else {
            minValue = b;
        }

        // Get the maximum value.
        int max = a > b ? a : b;

        // Get the absolute value.
        int abs = a < 0 ? -a : a;

        System.out.println("min      = " + min);
        System.out.println("minValue = " + minValue);
        System.out.println("max      = " + max);
        System.out.println("abs      = " + abs);
    }
}

The output of the code snippet above:

min      = 10
minValue = 10
max      = 20
abs      = 10

How do I use the continue statement?

The continue statement has two forms, the unlabeled and labeled continue statement. The first example shows you how to use the unlabeled continue statement while the second example shows you how to use the labeled continue statement.

package org.kodejava.basic;

public class ContinueDemo {
    public static void main(String[] args) {
        int[] numbers = {5, 11, 3, 9, 12, 15, 4, 7, 6, 17};
        int counter = 0;

        for (int number : numbers) {
            // When number is greater or equals to 10 skip the
            // current loop and continue to the next loop because
            // we only interested to count number less than 10.
            if (number >= 10) {
                continue;
            }

            counter++;
        }

        System.out.println("Found " + counter + " numbers less than 10.");

        // The example below used a labeled continue statement. In the
        // loop below we sum the number in the array until reminder of
        // the number divided by 2 equals to zero. We skip to the next
        // array if the reminder is 0.
        int[][] values = {
                {8, 2, 1},
                {3, 3},
                {3, 4, 5},
                {5, 4},
                {6, 5, 2}};

        int total = 0;

        outer:
        for (int[] value : values) {
            for (int item : value) {
                if (item % 2 == 0) {
                    continue outer;
                }
                total += item;
            }
        }

        System.out.println("Total = " + total);
    }
}

The output of the code snippet:

Found 6 numbers less than 10.
Total = 14

How do I use the break statement?

The break statement has two forms, the unlabeled and labeled break. On the example below you see the first example as the unlabeled break. This type of break statement will terminate the innermost loop such as the for, while and do-while loop.

On the second example you’ll see the labeled break. We have two loops, the infinite while and the inner for loop. Using the labeled break we can terminate the outermost loop. In the for loop when the value of y equals to 5 then it will break to the start: label which will continue the program execution to the line after the while loop.

package org.kodejava.basic;

public class BreakDemo {
    public static void main(String[] args) {
        int[] numbers = {5, 3, 6, 9, 8, 7, 4, 2, 1, 10};

        int index;
        boolean found = false;

        int search = 7;
        for (index = 0; index < numbers.length; index++) {
            if (numbers[index] == search) {
                found = true;
                break;
            }
        }

        if (found) {
            System.out.println(search + " found at index " + index);
        } else {
            System.out.println(search + " was not found.");
        }

        start:
        while (true) {
            for (int y = 0; y < 10; y++) {
                System.out.print("y = " + y + "; ");
                if (y == 5) {
                    System.out.println();
                    break start;
                }
            }
        }
    }
}

How do I use the for loop statement?

The for loop can be used to iterate over a range of values. For instance if you want to iterate from 0 to 10 or if you want to iterate through all the items of an array. Below you’ll see two forms of a for loop. The first one is the general form of a for loop and the second one is an enhanced for loop that also known as the for..each loop.

The general form of for loop consists of three parts:

for (initialization; termination; increment) {
    ....
}
  • The initialization: it initializes the loop, it executed once at the beginning of the loop.
  • The termination: the loop executes as long as the termination evaluates to true.
  • The increment: it executed at the end of every loop, the expression can be either an increment or decrement.
package org.kodejava.basic;

public class ForDemo {
    public static void main(String[] args) {
        // Do a loop from 0 to 10.
        for (int i = 0; i <= 10; i++) {
            System.out.println("i = " + i);
        }

        // Loop through all the array items.
        int[] numbers = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        for (int number : numbers) {
            System.out.println("number = " + number);
        }
    }
}

The result of the program is:

i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
i = 10
number = 0
number = 1
number = 2
number = 3
number = 4
number = 5
number = 6
number = 7
number = 8
number = 9
number = 10