-1

I know goto is a keyword which does not have any use in Java. Can I perform something like this using a label or other way to move to a different part of the code?

    public static void main(String[] args) {
        for(int i=5; i>0; i--){
            System.out.println();

            first:
            for(int x=i; x<6; x++){
                System.out.print("*");
            }
        }
        System.out.println("print '*'");{
            break first;
        }
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Varun Jain
  • 81
  • 1
  • 1
  • 13
  • I think that you should explain what you're trying to print exaclty, it'll easier to find a better solution for this – benzen Aug 10 '13 at 13:14

3 Answers3

3

You can do this

first: {
  for(int i=5; i>0; i--){
    System.out.println();
    if (func(i))
       break first;
    for(int x=i; x<6; x++){
        System.out.print("*");
    }
  }
}
System.out.println("print '*'");
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I learned something here: I was under the mistaken impression that you could only break to enclosing loops, not arbitrary enclosing blocks. You might want to elaborate on this point a little! – Ernest Friedman-Hill Aug 10 '13 at 13:09
  • What does func(i) does? Will it go to inner for loop after printing "print '*'" ? – Varun Jain Aug 10 '13 at 13:10
  • 1
    `func()` is just an arbitrary boolean condition -- it doesn't matter what it is. – Ernest Friedman-Hill Aug 10 '13 at 13:12
  • Your example does not do what my code intends to do! – Varun Jain Aug 10 '13 at 13:19
  • @VarunJ Can you change your code to be clearer? I can't read you mind. ;) – Peter Lawrey Aug 10 '13 at 13:25
  • I just want to move to other part of code(like goto). Output should be print '*' * ** *** **** ***** – Varun Jain Aug 10 '13 at 13:32
  • The example I gave does jump to another part of the code. If all you want to do is print a triangle you don't need a goto. Just two nested loops. – Peter Lawrey Aug 10 '13 at 13:41
  • I want the print statement first than the 'stars'. But not including the print statement before for loop. It should be after for loop. Anyways i understand that there is no shortcut way to jump to another part of code without using break, continue or method. – Varun Jain Aug 10 '13 at 13:51
  • Is there a sane reason not to put the code in the order you want to execute it? You can do this but it's ugly and confusing. – Peter Lawrey Aug 10 '13 at 13:58
2

Yes, but there's a reason goto is not used. It's terrible. If you however are just curious, here's a way to do it:

http://www.steike.com/code/useless/java-goto/

If instead you want to this in a proper way, ask your real question and state your end goal, so we can help you design it. :)

Xabster
  • 3,710
  • 15
  • 21
2

You can use the continue to move to different labels in code as:

public class Label {
    public static void main(String[] args) {
        int temp = 0;
        out: // label
        for (int i = 0; i < 3; ++i) {
            System.out.println("I am here");
            for (int j = 0; j < 20; ++j) {
                if(temp==0) {
                    System.out.println("j: " + j);
                    if (j == 1) {
                        temp = j;
                        continue out; // goto label "out"
                    }
                }
            }
        }
        System.out.println("temp = " + temp);
    }
}

The output:

I am here
j: 0
j: 1
I am here
I am here
temp = 1

However I won't recommend that you do it. There are tidier ways to do so as James Gosling created the original JVM with support of goto statements, but then he removed this feature as needless. The main reason goto is unnecessary is that usually it can be replaced with more readable statements (like break/continue) or by extracting a piece of code into a method.

Source: James Gosling, Q&A session

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rahulserver
  • 10,411
  • 24
  • 90
  • 164