1

Possible Duplicate:
Is there a goto statement in java?

In my android application i used goto statement to control the flow. But I received error as "Syntax error on token goto, throw expected". Here is my code

label:
if(alc)
{
  r_code=st.nextToken();
  AlertDialog.Builder alert=new AlertDialog.Builder(Fetch.this);
  alert.setTitle(count+" records found for "+rytname.getText().toString());
  alert.setMessage("Are you sure want to search for "+r_code+"?");
  alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
      ff=1;
      alc=false;
    }
  });

  alert.setNegativeButton("NO", new DialogInterface.OnClickListener() {

   @Override
   public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
      ff=0;
   }
  });
  alert.show();
  if (ff==0)
  {
    goto label;
}

I'm new to this android, and help me to avoid this error

Community
  • 1
  • 1
Linga
  • 10,379
  • 10
  • 52
  • 104

3 Answers3

3

There is no working goto in Java

Even though the Java keyword list specifies the goto keyword, it is marked as not used. Hence, it cannot work. You have to rewrite your code without using the goto keyword.

A general hint: there are Labeled Statements

Statements may have label prefixes.

LabeledStatement:
     Identifier : Statement

LabeledStatementNoShortIf:
     Identifier : StatementNoShortIf

The Identifier is declared to be the label of the immediately contained Statement.

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 scope of a label of a labeled statement is the immediately contained Statement. – JLS (§14.7)

But what you really want, is to rewrite your constructs without using that, for instance by using while

while (f == 0) {
     // ...
}
Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
0

Instead of:

label:
...// the rest of your code
if (ff == 0) 
{
    goto label;
}

Use this:

do {
...// the rest of your code
while (ff == 0);

Even better if you can convert it to:

while (ff == 0) {
...// the rest of your code
}
jdb
  • 4,419
  • 21
  • 21
-2

It is not advised to use goto in the code since it will be unclear from readability point of view. Instead of goto, one can use break and continue. Alternative for goto is here

Community
  • 1
  • 1
Raja Asthana
  • 2,080
  • 2
  • 19
  • 35