C++ - Q&A

Does Java support goto?



Unlike C/C++, Java does not have goto statement. This could be due to the reason that it makes difficult to trace the control flow of a program, making hard to understand and modify the program. Although, Java supports label statement which can be used to get the desired result.

The label statement is used with break or continue statements. It is used to prefix a statement with an identifier which can be referred. A label can be specified by any name other than the reserved words in Java. The syntax for using label is given below:

Syntax

label :
  statements;

Label statement with Continue statement

A label is used to identify a loop, and then continue statement is used to indicate when to skip the current iteration.

public class MyClass {
  public static void main(String[] args) {
    loop1 :
    for (int i = 1; i <= 5; i++){
      if(i == 3)
        continue loop1;
      System.out.println("i = " + i);
    } 
  }
}

The output of the above code will be:

i = 1
i = 2
i = 4
i = 5

In the example below, label statement is used to skip the inner and outer loop respectively whenever the conditions are met.

public class MyClass {
  public static void main(String[] args) {

    System.out.println("#Skips the inner loop");
    loop1 :
    for (int i = 1; i <= 3; i++){
      loop2 :
      for (int j = 1; j <= 3; j++){
        if(i == 2 && j == 2)
          continue loop2; 
        System.out.println("i = " + i + ", j = " + j); 
      }
    }

    System.out.println("\n#Skips the outer loop");
    loop3 :
    for (int i = 1; i <= 3; i++){
      loop4 :
      for (int j = 1; j <= 3; j++){
        if(i == 2 && j == 2)
          continue loop3; 
        System.out.println("i = " + i + ", j = " + j); 
      }
    }
  }
}

The output of the above code will be:

#Skips the inner loop
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 2, j = 3
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3

#Skips the outer loop
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3

Label statement with Break statement

A label is used to identify a loop, and then break statement is used to indicate when to get out of the loop.

public class MyClass {
  public static void main(String[] args) {
    loop1 :
    for (int i = 1; i <= 5; i++){
      if(i == 3)
        break loop1;
      System.out.println("i = " + i);
    } 
  }
}

The output of the above code will be:

i = 1
i = 2

In the example below, label statement is used to skip the inner and outer loop respectively whenever the conditions are met.

public class MyClass {
  public static void main(String[] args) {

    System.out.println("#Breaks from the inner loop");
    loop1 :
    for (int i = 1; i <= 3; i++){
      loop2 :
      for (int j = 1; j <= 3; j++){
        if(i == 2 && j == 2)
          break loop2; 
        System.out.println("i = " + i + ", j = " + j); 
      }
    }

    System.out.println("\n#Breaks from the outer loop");
    loop3 :
    for (int i = 1; i <= 3; i++){
      loop4 :
      for (int j = 1; j <= 3; j++){
        if(i == 2 && j == 2)
          break loop3; 
        System.out.println("i = " + i + ", j = " + j); 
      }
    }
  }
}

The output of the above code will be:

#Breaks from the inner loop
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3

#Breaks from the outer loop
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1