Thread: avoid GOTO

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    61

    avoid GOTO

    Hi there,

    I need to change a code from fortran77 into C. I do not want to use the GOTO statement although apparently accepted in C as well; is anyone able to change a statement of the type as below into one where the goto is not present?

    It would help a lot,
    thank you
    S.M.

    Ex.:
    Code:
    if ( A[i] = I[i])
       goto 10 /*where 10 in this case is a line that comes BEFORE this control

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It sounds like you are looking to use a loop, say a do while loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Presumably you're in a loop of some kind? If you want to skip one iteration of a loop, you can use "continue". Otherwise you'll have to be more specific.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Part of the reason "goto" might be considered obsolete (or inappropriate) is that a function call is conceptually almost the same thing.

    Find blocks of code that could work just as well as independent functions and make them that. Then you can use:
    Code:
    if ( A[i] = I[i])  somefunc();
    And guess what? It's dead easy to return back where you started from! Using recursive functions, you can simulate all kinds of additional flow control.

    I remember goto from BASIC programming as a kid, but it honestly would never occur to me to use it in C -- which supports the argument that, in fact, it can never be necessary. It's banned in c99 (??).
    Last edited by MK27; 02-22-2009 at 11:19 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Thank you to both for replying;
    Indeed I was thinking of a loop, but would not really know how to set it correctly.
    There are specifically two GOTO statements, one sending me further down (line 030) with respect to where it is called, and one further up (line 013);

    I quote a little more of the code and see if you want to help:

    Code:
    for (i = j + 1; i<=3; i++){
    013            m := m + 1
    014            if( A[m] ==  0){
    015               A[m] = I[i]
    016               GOTO 030
    017            }
    018            if( A[m] < I[i] ){ 
    019                GOTO 013
    020            }
    ...
    ...
    030              continue;
    ...
    ...

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    It's banned in c99 (??).
    As far as I know, no, it is not deprecated/obsoleted in C99.

    EDIT:
    Why not post the whole loop? I am not very willing to suggest changes to what appears to be spaghetti code when the entire context is not within my view.
    Last edited by laserlight; 02-22-2009 at 11:29 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by simone.marras View Post
    Thank you to both for replying;
    Indeed I was thinking of a loop, but would not really know how to set it correctly.
    There are specifically two GOTO statements, one sending me further down (line 030) with respect to where it is called, and one further up (line 013);

    I quote a little more of the code and see if you want to help:

    Code:
    for (i = j + 1; i<=3; i++){
    013            m := m + 1
    014            if( A[m] ==  0){
    015               A[m] = I[i]
    016               GOTO 030
    017            }
    018            if( A[m] < I[i] ){ 
    019                GOTO 013
    020            }
    ...
    ...
    030              continue;
    ...
    ...
    Is that what F95 looks like these days, or is this one-third Pascal, one-third Fortran, and one-third C?

    I'm hoping (or at least it is consistent with what I outlined in my first post) that the 30 continue matches up with do 30 .... up top somewhere, which means that "goto 30" is equivalent to the C "continue" (not the same as the Fortran "continue").

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It looks to me like both the goto's could be continue -- except that goto13 avoids incrementing i. If you want to do that you could use
    Code:
          if (condition) {i--; continue;}
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Quote Originally Posted by tabstop View Post
    Is that what F95 looks like these days, or is this one-third Pascal, one-third Fortran, and one-third C?

    I'm hoping (or at least it is consistent with what I outlined in my first post) that the 30 continue matches up with do 30 .... up top somewhere, which means that "goto 30" is equivalent to the C "continue" (not the same as the Fortran "continue").
    Hi, no, it's not a mixture of F90,Pascal and C, it actually comes from a pseudo code. I need to write it in C but the GOTO statements are making it tricky for me. I am not a programmer by profession, but I need to program for my applications.
    The GOTO 30 I was assuming that a continue would work, but when it sends me to a previous line with GOTO 13, that is when I get stuck

    Thank you
    S.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by simone.marras View Post
    Hi, no, it's not a mixture of F90,Pascal and C, it actually comes from a pseudo code. I need to write it in C but the GOTO statements are making it tricky for me. I am not a programmer by profession, but I need to program for my applications.
    The GOTO 30 I was assuming that a continue would work, but when it sends me to a previous line with GOTO 13, that is when I get stuck

    Thank you
    S.
    That's the easy one, since it's just the end of a while loop.

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    This is what I came out with; can you guys tell me if it is the correct "translation"? Correct in the sense, not only syntatically, but also in what the function should be doing

    I'd really appreciate that
    Thanks
    S.

    Code:
    for( i = j + 1; i <= 2; i++){
    		while( JA[m] < I1[i] ){
    		m++;
    				
    			if( JA[m] == 0){
    				JA[m] = I1[i];
    				continue;
    			}
    			
    			if( JA[m] == I1[i] )
    				return;
    				}
    		}
    }

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You can use break to arbitrarily exit a for or while loop, if that is what you intended with return.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Quote Originally Posted by MK27 View Post
    You can use break to arbitrarily exit a for or while loop, if that is what you intended with return.
    I meant to return from the function because this is the last command to run; should I use break or return would exit the function directly?

    On the other hand, I just realize that I cannot use WHILE because there the value of A[] can change and be either > or < than I[] throughout; that means that if I use WHILE, I may be exiting the loop before the task is completed.
    So that, Im back to thread number 1!

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by simone.marras View Post
    This is what I came out with; can you guys tell me if it is the correct "translation"? Correct in the sense, not only syntatically, but also in what the function should be doing

    I'd really appreciate that
    Thanks
    S.

    Code:
    for( i = j + 1; i <= 2; i++){
    		while( JA[m] < I1[i] ){
    		m++;
    				
    			if( JA[m] == 0){
    				JA[m] = I1[i];
    				continue;
    			}
    			
    			if( JA[m] == I1[i] )
    				return;
    				}
    		}
    }
    Given that I have no idea what you're trying to do, I have no idea whether this code accurately reflects your pseudocode. I didn't see the last part in your previous posts, but perhaps that was lines 20-29.

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Use while (1) and set more dynamic break conditions inside.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  2. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 10:40 AM
  3. helpppp
    By The Brain in forum C Programming
    Replies: 1
    Last Post: 07-27-2005, 07:05 PM
  4. Not important, only a curiosity...
    By BrownB in forum C Programming
    Replies: 35
    Last Post: 12-27-2004, 03:32 PM
  5. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM