CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Java Programming > Java Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Java Programming Ask your Java programming question and help out others with theirs.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old January 30th, 2005, 04:28 AM
    freesoft_2000 freesoft_2000 is offline
    Member
     
    Join Date: Aug 2004
    Posts: 147
    freesoft_2000 is an unknown quantity at this point (<10)
    Question GoTo

    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

    Code:
    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
    Reply With Quote
      #2    
    Old January 30th, 2005, 09:45 AM
    _uj _uj is offline
    Banned
     
    Join Date: Nov 2003
    Posts: 1,405
    _uj is a jewel in the rough (200+)_uj is a jewel in the rough (200+)_uj is a jewel in the rough (200+)
    Re: GoTo

    Quote:
    Originally Posted by freesoft_2000
    I hope someone can help me this problem
    Well, what's the problem? The wanted program logic isn't at all clear from the description. When the if-statement is left you come to the place you said you wanted to jump to without even jumping.

    It has been proven that any program logic can be expressed with the control structures available in todays high-level languages without the use of goto, so the goto's you've used in C in the past weren't really necessary. Programs become easier to follow without them.
    Reply With Quote
      #3    
    Old January 30th, 2005, 12:49 PM
    dlorde dlorde is offline
    Elite Member
    Power Poster
     
    Join Date: Aug 1999
    Location: UK
    Posts: 9,159
    dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)
    Re: GoTo

    As _uj says, you never have to use 'goto', and most experienced OO programmers wouldn't dream of using it (except possibly in complex low-level library code). Maybe this is why Java doesn't use it.

    There are the 'label', 'break' and 'continue' keywords for use in loops and 'if' blocks, and you can read up on them, but they're best avoided unless they will make the code more readable.

    The usual reason for an unstructured jump out of a section of code is when something goes wrong - a method can't continue or an error occurs. In the first case, it may be acceptable to make an early return from a method when, for example, its arguments aren't appropriate. In the second case, you can throw an exception.

    If you want specific recommendations on your own problem, you'll have to be a lot clearer and more detailed about what you're trying to do.

    At the IFIP congress in 1971 I had the pleasure of meeting Dr. Eiichi Goto of Japan, who cheerfully complained that he was always being eliminated...
    D. Knuth
    __________________
    Please use [CODE]...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Java Programming > Java Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 09:47 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009