CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2006
    Posts
    194

    [RESOLVED] C Help - Multiple goto's in a Function

    I'm trying to understand multiple goto's in a function. For the code below, if it goes to Return1 & does the line:

    RetVal++; , would it then go back into the loop or would it proceed down to the line: return RetVal; ? Thanks for the help!

    Code:
    int one_bisect(int type, int l_et)
    {
       int i;
       int line[7];
       int RetVal = 0;
    
       if (type == -1) 
       {
          for (i = 0; i < 3 + 1; i++)
             if ((line[i] - line[i + 3]) > l_et)
                goto Return0;
          goto Return1;
       }
       else
       {
          for (i = 0; i < 3 + 1; i++)
             if ((line[i + 3] - line[i]) > l_et)
                goto Return0;
          goto Return1;
       }
    
    Return1:
       RetVal++;  //would this go back to the loop or to return RetVal?
    Return0:
    
       return RetVal;
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: C Help - Multiple goto's in a Function

    No, it won't "go back into the loop". It will "proceed down to the line: return RetVal;"

    PS. I havent't seen so silly written code (with "goto" that are no needed al all!) since 70th of the last century when I did my first steps in Fortran programming!
    Neither C nor C++ needs using operator goto. Instead using it is a bad style and makes any code not only bad readable/understandable but also not structured and hard maintainable.
    IMHO; operator goto should be removed from any modern language.
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2001
    Location
    Texas
    Posts
    645

    Re: C Help - Multiple goto's in a Function

    Change your logic a little.

    Try this

    Code:
    int one_bisect(int type, int l_et)
    {
       int i;
       int line[7];
       int RetVal = 1;
    
       if (type == -1) 
       {
          for (i = 0; i < 3 + 1; i++)
             if ((line[i] - line[i + 3]) > l_et)
             {
                RetVal = 0;
                break;
             }
       }
       else
       {
          for (i = 0; i < 3 + 1; i++)
             if ((line[i + 3] - line[i]) > l_et)
             {
                RetVal = 0;
                break;
             }
       }
    
       return RetVal;
    }

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: C Help - Multiple goto's in a Function

    Quote Originally Posted by cvogt61457
    Change your logic a little.

    Try this
    ...

    This code is much better!
    Victor Nijegorodov

  5. #5
    Join Date
    Jun 2006
    Posts
    194

    Re: C Help - Multiple goto's in a Function

    Oh believe me, I fully agree with your logic This is just pre-existing C code that has been given to me to convert into Java and I wanted to make sure I had the right idea of what was going on. Thanks for the help!

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: [RESOLVED] C Help - Multiple goto's in a Function

    I believe you, slowcoder! (Frankly, it was my guess this code is not youth!)
    Victor Nijegorodov

  7. #7
    Join Date
    Aug 2001
    Location
    Texas
    Posts
    645

    Re: [RESOLVED] C Help - Multiple goto's in a Function

    One goal to strive for is "readability"

    Can you make sense of the code easily?

    If the code is difficult to read, then something is probably wrong.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured