Bits of Java - Episode 8: Labels in Loops

This week’s topic will focus on the use of labels in loops.

Did you know that you can use labels to identify your loops? I, for sure, did not know that! It’s true that is not very common, because in most of the cases they decrease your code readability, which is always not a good idea, especially when you work with other people and your code has to be handled also by someone else.

However, sometimes they can be useful, so it’s good to know that they exist and how we can use them. So, let’s see!

Suppose you have two for loops, one inside the other, like this:


int[][] array = {{1,2,3},{4,5,6}};
for(int row = 0; row < 2; row++) {
    for(int col = 0; col < 3; col++) {
        System.out.println(array[row][col]);
    }   
}

So far nothing new, I believe. We are just looping over the two dimensions of our array and printing each element.

Well, what if I tell you that you could have written it also like this:


int[][] array = {{1,2,3},{4,5,6}};
LOOP_OVER_ROWS: for(int row = 0; row < 2; row++) {
        LOOP_OVER_COLUMNS: for(int col = 0; col < 3; col++) {
            System.out.println(array[row][col]);
    }
}

As you can see, we have added two labels, one to identify the outer most loop and the other one to identify the inner most loop. The name of the label has to be followed by the :, and the allowed names for a label are the same as for the variables, but in general they are written in upper cases and in snake_style (a style which foresees an _ between each word).

OK, now we know that these labels exist and how to place them in our code. But, what exactly are they good for? Well, in our previous example at not much, actually. Their use becomes more interesting when we start to add to our loops break or continue statements.


int[][] array = {{1,2,3},{4,5,6}};
LOOP_OVER_ROWS: for(int row = 0; row < 2; row++) {
        LOOP_OVER_COLUMNS: for(int col = 0; col < 3; col++) {
            if(col == 1) {
                break;
            }
            System.out.println(array[row][col]);
    }
}

We have added now a break statement , which will be executed if the loop variable col is equal to 1. If this condition is met, the break statement will interrupt the inner most loop, going then back to the outer most which then will go on with its actions. So, in this case, the output of this flow would be:

1
4

meaning that we would print just the first element of each row of our array. What if, instead, we wanted to exit also the outer most loop when the condition is met? Well, here labels can help!


int[][] array = {{1,2,3},{4,5,6}};
LOOP_OVER_ROWS: for(int row = 0; row < 2; row++) {
        LOOP_OVER_COLUMNS: for(int col = 0; col < 3; col++) {
            if(col == 1) {
                break LOOP_OVER_ROWS;
            }
            System.out.println(array[row][col]);
    }
}

We have added at the break statement the label corresponding to the loop we want to actually exit when the condition is met. In this case our code would simply output 1, namely the first element of the first row of the array. Then the condition is met and the outer most loop is interrupted.

This was a simple example, and the same result could have also been achieved without the use of labels. But when you have more complicated code, which a lot of intricate loops, it is good to know that simply adding a label can help you refer to the right loop you want an action to be taken on!

This was all for this week; next episode will be about String vs StringBuilder!

by Ilenia Salvadori