Like Tree1Likes
  • 1 Post By Salem

goto statement help

This is a discussion on goto statement help within the C Programming forums, part of the General Programming Boards category; Now, before anyone says anything, I know that using a goto statement is not "right". Either way, my school professor ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    8

    goto statement help

    Now, before anyone says anything, I know that using a goto statement is not "right". Either way, my school professor asked us to modify a piece of code we have previously written and used to work with go to statements. The following is a bubble sort algorithm that we have to modify. Could you guys explain to me where I would insert go to statements to get this algorithm to work with them? A is the name assigned to the array that holds the numbers that are to be sorted.

    Code:
    int i,pass;
       boolean swapMade;
     
       pass = 1;
       *numberCompares = 0;
       *numberMoves = 0;
       do
       {
          swapMade = false;
          for (i = 0; i <= n-(pass+1); i++)
          {
             *numberCompares += 1;
             if ( ShouldSwap(A[i],A[i+1]) )
             {
                int T = A[i]; A[i] = A[i+1]; A[i+1] = T;
     
                *numberMoves += 3;
                swapMade = true;
             }
          }
          pass++;
       } while ( swapMade );
    Last edited by lscamaro; 10-24-2012 at 06:38 PM.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Well, for loops can be written with gotos as follows:

    Code:
       for(i=0; i<10; i++) {     /*for loop*/
          printf( "%d\n", i );
       }
       
       i=0;             /*same loop made with gotos*/
       start_of_loop: 
          /* do stuff */
          i++; /*increment i; equivalent to the i++ part of the for loop*/
          if( i==10 )
             goto end_of_loop;
       goto start_of_loop;
       end_of_loop:;
    Other kinds of loops can be done in a similar way.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,415
    Either way, my school professor asked us to modify a piece of code we have previously written and used to work with go to statements.
    Please send him by this forum; there are some of us who'd like to smack that bit of insanity out of his head.

    Soma

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Maybe the professor is trying to teach students not to use goto by showing them how ugly and unmaintainable the code is with goto. Experience is the best teacher, and that sort of stuff.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    8
    Quote Originally Posted by christop View Post
    Maybe the professor is trying to teach students not to use goto by showing them how ugly and unmaintainable the code is with goto. Experience is the best teacher, and that sort of stuff.
    I honestly think that might be it. Well at least I hope it is because even he said that goto's were bad and that they should never be used but then he assigns us this task

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Like the for loop, the do while loop can be easily changed to use goto's:

    do - label it startOfDo

    while(yada,yada) - if statement + gototo

    I remember when BASIC didn't have anything but for loops, so goto's were sometimes used like this.
    You can also make the function call with a goto, typically putting the function code at the bottom of the page.

    goto(functionLabel), and
    goto(functionLabelReturn) right below it.
    other code here


    functionLabel
    code for function
    goto functionLabelReturn

    What a mess that can make!

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    8
    How does this look? Is this right?

    Code:
    int i,pass;
       boolean swapMade;
    
       pass = 1;
       *numberCompares = 0;
       *numberMoves = 0;
    
    STARTOFDO:
       {
           swapMade = false;
           i = 0;
           FIRSTLOOP:
           if(!(i <= n-(pass+1))) goto END;
           {
               *numberCompares += 1;
               if(!(ShouldSwap(A[i],A[i+1]))) goto FIRSTLOOP;
               {
                   int T = A[i]; A[i] = A[i+1]; A[i+1] = T;
    
                   *numberMoves += 3;
                   swapMade = true;
               }
           }
           pass++;
       }if(!(swapMade)) goto END;
    END:

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by phantomotap View Post
    Please send him by this forum; there are some of us who'd like to smack that bit of insanity out of his head.

    Soma
    Generally, yes, I would like to smack such a professor, but this would be a natural segue to teaching assembly. Start with a structured, higher-level construct in a language you know, C. Rewrite it in C, but using only the lower-level constructs, if and goto. Then, you can translate the low-level version to assembly in a very direct manner.

    EDIT: But based on post #5, I don't think that's the case.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You are missing the goto at the end of the inner loop.

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    8
    Quote Originally Posted by Adak View Post
    You are missing the goto at the end of the inner loop.
    Where? I'm trying to see where it would go but I can't come up with anything

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by lscamaro View Post
    How does this look? Is this right?
    Pretty close, check the comments in the code

    Code:
    int i,pass;
       boolean swapMade;
    
       pass = 1;
       *numberCompares = 0;
       *numberMoves = 0;
    
    STARTOFDO:
       {
           swapMade = false;
           i = 0;
           FIRSTLOOP:
           // The end of the for loop may not be the same as the end of the do, use two separate labels with good names and goto the right one
           if(!(i <= n-(pass+1))) goto END;
           {
               *numberCompares += 1;
               // Next line is wrong, you skip the increment.  Just use the if condition as it was before (no !) without a goto
               // if (ShouldSwap(...))
               if(!(ShouldSwap(A[i],A[i+1]))) goto FIRSTLOOP;
               {
                   int T = A[i]; A[i] = A[i+1]; A[i+1] = T;
    
                   *numberMoves += 3;
                   swapMade = true;
               }
           }
           pass++;  // This should be i++
           // Don't forget to go back to the start of the for loop here
    // This is where your END_FOR_LOOP label would go
       }if(!(swapMade)) goto END;  // The do loop needs to continue if swapMade is true, try: if (swapMade) goto STARTOFDO
    END:

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by christop View Post
    Maybe the professor is trying to teach students not to use goto by showing them how ugly and unmaintainable the code is with goto. Experience is the best teacher, and that sort of stuff.
    I still reckon he has the task back-to-front. It's far more useful to learn how to improve code and remove gotos rather than put that crap in.

    FYI: The typical way a compiler implements a for-loop actually translates as following, in C.
    This:
    Code:
    for (ini i=0; i<10; ++i)
    {
        printf("Test\n");
    }
    compiles to pretty much like this:
    Code:
        int i=0;
        goto loop_condition;
    loop_start:
        printf("Test\n");
        ++i;
    loop_condition:
        if (i < 10)
            goto loop_start;
    The condition actually gets put at the end, and there is an explicit branch to it. It does it that way so that the loop body only has one branch instruction, rather than one conditional branch and one unconditional branch every time through the loop.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User
    Join Date
    Jan 2012
    Posts
    8
    How about this? I think I got all my mistakes.

    Code:
    int i,pass;
       boolean swapMade;
    
       pass = 1;
       *numberCompares = 0;
       *numberMoves = 0;
    
    STARTOFDO:
       {
           swapMade = false;
           i = 0;
           FIRSTLOOP:
           if(!(i <= n-(pass+1))) goto ENDOFDO;
           {
               *numberCompares += 1;
               i++;
               if(ShouldSwap(A[i],A[i+1])) goto FIRSTLOOP;
               {
                   int T = A[i]; A[i] = A[i+1]; A[i+1] = T;
    
                   *numberMoves += 3;
                   swapMade = true;
               }
           }
           i++;
           goto FIRSTLOOP;
       ENDOFDO:
       }if(swapMade) goto STARTOFDO;

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    > How about this? I think I got all my mistakes.
    Compile it
    Run it
    Provide some test data
    Observe test results

    Then with the reference implementation using while and for
    Compile it
    Run it
    Provide some test data
    Observe test results

    If you get the same results, try some more tests until you're confident that they're functionally equivalent.
    rags_to_riches likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble using GOTO with the SWITCH statement!
    By xxJaRxx in forum C Programming
    Replies: 9
    Last Post: 03-10-2010, 06:42 AM
  2. GOTO statement in c++
    By kibestar in forum C++ Programming
    Replies: 8
    Last Post: 03-22-2009, 08:10 PM
  3. goto statement interpretation in VC 6 and 7
    By w262 in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2005, 10:37 PM
  4. Goto statement
    By _JjC:: in forum C Programming
    Replies: 2
    Last Post: 03-02-2003, 10:43 AM
  5. but why not GOTO?
    By LonelyPlanetWanderer in forum C Programming
    Replies: 13
    Last Post: 12-24-2001, 11:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21