0
public class Try {


public static void main(String[] args) {
int i=0;
    while(i<10)
    {
        System.out.println("Hello World");

        if (i==6)
        {
            // The Execution pointer should go back to this statement (int i=0);
        }
        i++;
    }

}
}

We use goto keyword in VB to put the execution pointer on particular set of statements. Please tell me How this is possible in Java. How we can place the execution pointer on particular set of statements?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Atishay Jain
  • 87
  • 1
  • 9
  • see here http://stackoverflow.com/questions/2545103/is-there-a-goto-statement-in-java.It may help you – PSR Jul 17 '13 at 06:11
  • While most people will suggest using `continue` I prefer to ask why to have such odd design, is a good valid reason to use this? If so, please explain. – Luiggi Mendoza Jul 17 '13 at 06:13
  • I am completely agree with @LuiggiMendoza. Furthermore, if you jump to `int i=0;`. program execution will enter `while` loop again and again which will make an infinite loop. That is why you should not use unconditional jumps. It is harder to predict program execution. – erencan Jul 17 '13 at 06:15

7 Answers7

5

goto was removed in Java so you can't use that. You will have to use continue or break.

Please don't really do this though. Properly designed code doesn't need to jump to specific lines in code. When I was starting to program in my early teens, goto was an easy crutch in BASIC, but it's still a crutch.

Read this as well: Go To Statement Considered Harmful

Mark M
  • 1,580
  • 10
  • 22
1

You need to use a labelled continue , but it is a bad practice though to write spaghetti code .

somepoint: { int i=0;
while(i<10)
{
    System.out.println("Hello World");

    if (i==6)
    {
       continue somepoint;
    }
    i++;
}
}

Read JLS 14.7 for Labelled Statements.

Unlike C and C++, the Java programming language has no goto statement; identifier statement labels are used with break (§14.15) or continue (§14.16) statements appearing anywhere within the labeled statement.

The Oracle tutorial.

As per JLS 3.9:

The keywords const and goto are reserved, even though they are not currently used. This may allow a Java compiler to produce better error messages if these C++ keywords incorrectly appear in programs.

Also read Should I avoid using Java Label Statements?

Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • 2
    This code involves an infinite loop, but it is a good answer to OP's question. – erencan Jul 17 '13 at 06:18
  • @erencan IMO an answer that tries to solve a bad design with another bad design can't be considered as good =\ – Luiggi Mendoza Jul 17 '13 at 06:18
  • +1 @erencan I agree with the `infinite` loop . – AllTooSir Jul 17 '13 at 06:19
  • This doesn't compile.. – FThompson Jul 17 '13 at 06:19
  • @Vulcan sorry forgot to add the block , i didn't test it though ! – AllTooSir Jul 17 '13 at 06:21
  • With the block, it simply breaks out of the labeled block rather than continue infinitely. I've already written and tested the exact same code. – FThompson Jul 17 '13 at 06:23
  • @LuiggiMendoza This is not a consultancy site ! A simple question with simple answer with pointers to what is not correct . I don't police over OP's design problems , if he has one he will get back to us . First aid is mandatory when there is a casualty , you can report the accident to the police later :) – AllTooSir Jul 17 '13 at 06:23
  • 1
    @TheNewIdiot IMO giving a simple *use `continue`* advice is easy (otherwise look at all the other answers), but giving a solution to the real problem: **bad design** is what **we professional software engineers must do**, otherwise we are simply monkey programmers. By the way, your answer proposes that even having a simple scenario like this, you should use a `continue` statement instead of a `while(true)` to write an infinite loop (which is even a worse design). – Luiggi Mendoza Jul 17 '13 at 06:27
  • @LuiggiMendoza +1 I agree with your point but I am not a professional s/w engineer :) – AllTooSir Jul 17 '13 at 06:29
  • That would explain the bad design you propose... – Luiggi Mendoza Jul 17 '13 at 06:31
  • @LuiggiMendoza I **didn't propose any design** , the OP wanted to know *is there a way to do it* , I just hinted a way . – AllTooSir Jul 17 '13 at 06:33
0

Java has standard keyword for that:

break;

will exit cycle.

continue;

will jump to the start of the cycle. Also, if you have "cycle inside cycle situation", you can use labels.

label1: while(true){
    ...
    label2: while(true){
        ...
        break label2;
        ...
     }
     ...
}
halfer
  • 19,824
  • 17
  • 99
  • 186
bigGuy
  • 1,732
  • 1
  • 22
  • 37
0

There is no goto in java. You can achieve this by

i=0;
while(i<10)
{
    System.out.println("Hello World");

    if (i==6)
    {
        i=0;
        continue;
    }
    i++;
}
vels4j
  • 11,208
  • 5
  • 38
  • 63
0

Try this

   public class Try {


   public static void main(String[] args) {
   int i=0;
   while(i<10)
   {
    System.out.println("Hello World");

    if (i==6)
    {
      i=0;
      continue;  // The Execution pointer should go back to this statement (int i=0);
    }
    i++;
    }

  }
 }
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

What about continue statement here is explanation of The continue Statement

subodh
  • 6,136
  • 12
  • 51
  • 73
0
  1. Your code will not move the pointer to int i=o, it will move to the while loop only.

  2. And it will go to infinite loop, since you added i=o in if(i==6) condition.

So it is completely wrong.

halfer
  • 19,824
  • 17
  • 99
  • 186
Tanu Garg
  • 3,007
  • 4
  • 21
  • 29