Named or Labelled Loop in Java - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

Named or Labelled Loop in Java

Share This
Have you ever heard of or used Named or Labelled Loop in Java? If not, then probably, this tutorial will give you some excitement.

A Named or Labeled loop is a java concept that lets us to give a name or a label to a loop. For example, in the following code, we have given the name "loop1" to a for loop and "loop2" to a while loop.
LabelledLoop.java
public class LabelledLoop { public static void main(String[] args) { loop1: for (int i = 0; i < 5; i++) { System.out.println("Hi World!"); } int i = 5; loop2: while (i > 0) { System.out.println("Hello World"); i = i - 1; } } }

Why does it play an important part in Java Coding?

It plays a vital role in Nested Loop Management.

Let us think for a situation where you have three nested loops. Outer loops contains a middle loop and in turn, middle loop contains the inner loop.

Let us further suppose that you need to take an integer as an input from the user in the inner loop with the following conditions

  1. If the input number is < 5, the inner loop will continue.
  2. If the input number is in the range 5 - 9, the inner loop will break, but the middle loop will continue.
  3. If the input number is 10 or more, the middle loop will break, but the outer loop will continue.

Following code will implement this idea without using the named or labelled loops
Source Code to implement the above conditions without using Named or Labelled Loops
public class UnnammedLoop { public static void main(String[] args) { for (int i = 0; i < 5; i++) { System.out.println("I am in outer loop"); for (int j = 0; j < 5; j++) { System.out.println("I am in middle loop"); boolean unnaturalBreakFromMiddleLoop = false; for (int k = 0; k < 5; k++) { System.out.println("I am in inner loop"); System.out.println("\n Enter a number::"); Scanner sc = new Scanner(System.in); int inpNumber = sc.nextInt(); if (inpNumber < 5) // if the number is below 5, inner loop will carry on System.out.println("You entered: " + inpNumber); else { if (inpNumber < 10)// if the number is between 5 - 9, inner loop will break but middle loop // will continue break; // no problem due to absence of named loop else // if the number is 10 or more, middle loop will break and outer loop will // continue { // here is the problem due to absence of named loop. We need to bring in extra // logic and extra variables unnaturalBreakFromMiddleLoop = true; break; } } } if (unnaturalBreakFromMiddleLoop) break; } } } }

Here you can see, we have to use a flag variable and its associated logic to keep track of un-natural break from middle loop. With the increase of levels in the nesting, this process becomes non-manageable and dirty.

But, by using named or labelled loop concept, you can implement the same logic more meaningfully, which will still remain more meaningful, readable, manageable and clean with the increase of levels of the nesting.

Here goes the code to implement the same logic as described earlier, but now with, a named or labeled loop concept

Source Code to implement the above conditions using Named or Labelled Loops
public class LabelledLoop { public static void main(String[] args) { outer: for (int i = 0; i < 5; i++) { System.out.println("I am in outer loop"); middle: for (int j = 0; j < 5; j++) { System.out.println("I am in middle loop"); inner: for (int k = 0; k < 5; k++) { System.out.println("I am in inner loop"); System.out.println("\n Enter a number::"); Scanner sc = new Scanner(System.in); int inpNumber = sc.nextInt(); if (inpNumber < 5) // if the number is below 5, inner loop will carry on System.out.println("You entered: " + inpNumber); else { if (inpNumber < 10)// if the number is between 5 - 9, inner loop will break but middle loop will // continue break inner; else // if the number is 10 or more, middle loop will break and outer loop will // continue break middle; } } } } } }


In this code, we can see that by using a named loop, the management of the nested loops has become more realistic, meaningful, and clean. Even if you go on nesting the loops further, due to the labels attached to the loops, it will be easy to break free of any loops.

A word of caution! By convention, the loop name should not start with a capital letter, because in Java, whatever starts with caps, we take it as a class. Obviously, this is not a rule, but a well-followed custom.


We hope, that you have explored something new with this tutorial :)

Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.