Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2005

Tip: Looking for answers? Try searching our database.

GoTo

Thread view: 
freesoft_2000@yahoo.com - 30 Jan 2005 09:39 GMT
Hi everyone,

I need to use something in java that is similar to the C
language goto statement.

This is what i have in a function

public int ret()
{

if
{
//something
//something

//This is the part where i need to jump to the end of the
//function and not exit the function
}

//This is the part i need to jump to

//some more code
//some more code
}

Is there a way in which the above problem can be got around. Usually in
the C language i would use the goto statement. I can't use a labeled
break here as there is no loop in my program.

I hope someone can help me this problem
Thank You

Yours Sincerely

Richard West
Fahd Shariff - 30 Jan 2005 14:04 GMT
1) You could always set a boolean variable and break out of the if
statement, or

2) Copy the same code into the if statement, or

3) Put the code you want to jump to in a new method. Then simply call
the new method and return from the current method:

public int ret()
{

if
{
//something
//something

//This is the part where i need to jump to the end of the
//function and not exit the function
newMethod() ;
return ;

}

}

public void newMethod(){
//This is the part i need to jump to

//some more code
//some more code
}

--
Fahd Shariff
http://www.fahdshariff.cjb.net
"Let the code do the talking... "
Vilya Harvey - 30 Jan 2005 14:27 GMT
> Hi everyone,
>
[quoted text clipped - 24 lines]
> the C language i would use the goto statement. I can't use a labeled
> break here as there is no loop in my program.

You can label the if statement and use a named break to jump out of it.
There's a common misconception that you can only use labelled breaks inside
loops; that is true for labelled continues, but not for labelled breaks. You
can actually label any statement (although it's only useful to label blocks,
as opposed to single statements) and use break to jump out of it:

  public int ret() {
    someLabel: {
      if (...) {
        //something
        break someLabel;
      }
      // whatever code you want to skip
    }
    // the part you need to jump to
    // some more code
  }

Having said that, if you find yourself needing to do this you would probably
be better off trying to restructure your code (by using an else clause, for
example...).

Hope that helps,
Vil.
Wiseguy - 30 Jan 2005 17:59 GMT
freesoft_2000@yahoo.com scribbled on the stall wall:
> Hi everyone,
>
> I need to use something in java that is similar to the C
> language goto statement.
>
> This is what i have in a function

Nicholas Wirth, a prominent figure in computer science once authored
a piece (probably before most of you were born) called "GOTOs, considered
hazardous".  If you need a goto then reevaluate your logic.  There are
legitimate cases where a goto is valid but with modern constructs,
exception checking, etc. their necessity is very infrequent.

The only legitimate use of a goto in a high level language is to exit a
construct, never to jump into one.
Juha Laiho - 30 Jan 2005 20:04 GMT
noone@uber.usachoice.net (Wiseguy) said:
>freesoft_2000@yahoo.com scribbled on the stall wall:
>> Hi everyone,
[quoted text clipped - 7 lines]
>a piece (probably before most of you were born) called "GOTOs, considered
>hazardous".

Sorry, but:
- it was Edsger W. Dijkstra
- it was "Goto statement considered harmful"

Hmm.. on a more careful look: Dijkstra had titled the paper "Notes on
Structured Programming"; later on, Wirth dubbed the paper as "Goto
statement considered harmful". The paper is available at
http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD249.PDF

... but naturally this doesn't change the actual issue.
Signature

Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
        PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)

Wiseguy - 30 Jan 2005 20:57 GMT
Juha Laiho <Juha.Laiho@iki.fi> scribbled on the stall wall:
> noone@uber.usachoice.net (Wiseguy) said:
>>freesoft_2000@yahoo.com scribbled on the stall wall:
[quoted text clipped - 17 lines]
> statement considered harmful". The paper is available at
> http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD249.PDF

I stand corrected...The content seemed more like something Wirth would
have written about, since mod-2 was his baby...but then, a real programmer
can write fortran code in any language. :^)
John McGrath - 30 Jan 2005 23:06 GMT
> Hmm.. on a more careful look: Dijkstra had titled the paper "Notes on
> Structured Programming"; later on, Wirth dubbed the paper as "Goto
> statement considered harmful". The paper is available at
> http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD249.PDF

Actually "Notes on Structured Programming", which Dijkstra wrote in 1969,
is not the same thing as the "Go To Statement Considered Harmful" paper,
which was published in the March 1968 edition of the CACM as a letter to
the editor.  The University of Texas (*the* web site to look for Dijksta's
papers) has the original, titled "A Case against the GO TO Statement":

  http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF

The ACM web site has the paper on their web site, as it was published in
"Communications of the ACM":

  http://www.acm.org/classics/oct95/

In 2001, Dijkstra wrote a historical note describing the experiences that
led him to write the "Notes on Structured Programming" paper.  At the end,
he added the following:

   Finally a short story for the record. In 1968, the Communications of
   the ACM published a text of mine under the title "The goto statement
   considered harmful", which in later years would be most frequently
   referenced, regrettably, however, often by authors who had seen no
   more of it than its title, which became a cornerstone of my fame by
   becoming a template: we would see all sorts of articles under the
   title "X considered harmful" for almost any X, including one titled
   "Dijkstra considered harmful". But what had happened? I had submitted
   a paper under the title "A case against the goto statement", which,
   in order to speed up its publication, the editor had changed into a
   "letter to the Editor", and in the process he had given it a new title
   of his own invention! The editor was Niklaus Wirth.

A PDF of Dijkstra's note (in his own handwriting) is available here:

   http://www.cs.utexas.edu/users/EWD/ewd13xx/EWD1308.PDF

Signature

Regards,

John McGrath



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.