What Are Continue And Break Statements Do?

In this tutorial, we will learn What Continue and break statements do? As the name suggests Continue is used for continuing in a loop whereas breaking helps in coming out of the loop or given block.

Continue

Continue

As the name suggests continue is used for continuing after skipping that executing iteration in a way so that whatever has happed will be jumped to the updation of the variable which means it will ignore all statements after it and reach updation.

For understanding it in a better way follow the example given below and learn more

class ABC {
    public static void main( String args[])
    {
        for (int i = 0; i < 10; i++) {
            // If the number is 2
            // skip and continue
            if (i == 2)
                continue;
 
            System.out.print (i + " ");
        }
    }
}

Here we can see how to continue to use for the iteration when the value of variable I will is two otherwise it will print all the elements from Zero to Nine but not two as when the value of the variable is two it will continue means skip so it will not be printed.

Break

The break is a statement that we used for breaking the loop means once a break statement is used it will terminate the loop.

To learn more and understand it in more clear manner follow the example given below.

class ABC {
    public static void main(String args[])
    {
    // First label
    first:
        for (int i = 0; i < 3; i++) {
        // Second label
        second:
            for (int j = 0; j < 3; j++) {
                if (i == 1 && j == 1) {
 
                    // Using break statement with label
                    break first;
                }
                System.out.println(i + " " + j);
            }
        }
    }
}

here the loop will end when the values of i and j both will be same or we can say equals. Otherwise, it will keep on printing the values of i and j with a space in between.

Learn more about these statements at Break and continue statements and their examples

Also, visit Java Tutorials To learn more about solutions to any java problems also visit How To Reverse A String In Python? for python programing languages and for learning more about different programing languages.

 

Leave a Comment

%d bloggers like this: