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 executed the codes that you wanted, you might want to exit the loop to continue your program and save processing time. Terminating the loop is advisable rather than waiting for your loop to finish.

Ways on how to terminate a loop in Java

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

  1. Using the break keyword.
  2. Using the return keyword.
  3. And using the continue keyword to skip certain loops.

Using the break keyword

The 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 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.

Using return keyword

The return keyword is used if you want to terminate the loop and return the value or the control to the calling method.

For example, suppose that you need to find and return the index of a specific string in an 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 finds the 'car' in the string array, it will automatically break the loop and return the index of the 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++) {
    if (i==5) {
    //this code block will be skip if i == 5.
    System.out.println("The value of i is: "+i);

In the above example, we have set a condition that if variable i is equal to 5, we continue the for loop by skipping 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 the 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

Share this tutorial!