How do I use the continue statement?

Category: fundamental, viewed: 422 time(s).

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.example.lang;

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

        for (int i = 0; i < length; i++) {            
            //
            // 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 (numbers[i] >= 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. If the reminder is 
        // zero we skip to the next dimension of the array.
        //
        int[][] data = {
                {8, 2, 1}, 
                {3, 3}, 
                {3, 4, 5}, 
                {5, 4}, 
                {6, 5, 2}};
        int total = 0;

    outer:
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (data[i][j] % 2 == 0) {
                    continue outer;
                }

                total += data[i][j];
            }
        }

        System.out.println("Total = " + total);
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats