• How to terminate a loop in Java

    In our previous post, we learned how to construct a looping statement. This article will teach you how to terminate a loop in java you have constructed.

    Loops can be terminated using the break and continue statement. If your program already do what you wnat, you may want to exit the loop to continue your program and also to save processing time. Terminating the loop is advisable rather than waiting for your loop to finish.

    Ways to terminate a loop in Java

    There are multiple ways to terminate a loop in Java. These are:

    1. Using break keyword;
    2. Using return keyword;
    3. Using continue keyword to skip certain loops.

    Using break keyword

    Break keyword will cause the loop to exit and terminate and continue reading the codes after the loop. For example,

    int findMe = 5;
    for(int i=0; i<10; i++){
         if(i==findMe) {
              System.out.println("I've found it!");

    In the above example, there is a for loop that will continue to execute until such time that the variable i is equal to findMe variable. The loop will start from i=0  and end with i=5 and exit the loop because a break statement is written, thus, it will not continue from i=6 to i=10. Therefore, it will save processing time.

    Using return keyword

    The return keyword is used if you want to terminate the loop and return the value or control to the calling method. For example, suppose that you need to find and return the index of a specific string in a array of strings or return -1 if it cannot be found.

    public int getStringFromArray(String strToFind, String[] strToLoop) {
        for(int i = 0; i < strToLoop.length; i++) {
            if(strToFind.equals(strToLoop[i]) {
                return i;
        return -1;

    If we passed an array [‘car’, ‘motorcycle’, ‘laptop’] and the string to find is ‘car’, then when our loop starts, when it find the ‘car’ in the string array, it will automatically breaks the loop and return the index of car which is 0. Thus, it saves execution time such that it skips the loop for the remaining strings inside the array.

    Using continue keyword

    The continue statement will cause the preceding codes not to be executed and will continue the loop for the next iteration. Consider the example below:

    for(int i=0; i<10; i++){
         System.out.println("The value of i is: "+i);

    In the above example, inside the for loop, we have set a condition that if variable i is equal to 5, we continue the for loop and skips the preceding codes. Therefore, if i is equal to 5, the continue statement will be executed, thus, it will skip the System.out. method and will just proceed to incremental definition and continue the loop. The program output will be:

    The value of i is: 0;
    The value of i is: 1;
    The value of i is: 2;
    The value of i is: 3;
    The value of i is: 4;
    The value of i is: 6;//5 will not be printed because of continue statement.
    The value of i is: 7;
    The value of i is: 8;

    In our next tutorial, we will learn how to use the Switch Statement in Java

    Related Post