1. ## 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;

pass = 1;
*numberCompares = 0;
*numberMoves = 0;
do
{
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;
}
}
pass++;

2. 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.

3. 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. 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. Originally Posted by christop
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. Like the for loop, the do while loop can be easily changed to use goto's:

do - label it startOfDo

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. How does this look? Is this right?

Code:
```int i,pass;

pass = 1;
*numberCompares = 0;
*numberMoves = 0;

STARTOFDO:
{
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;
}
}
pass++;
END:```

8. Originally Posted by phantomotap
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. You are missing the goto at the end of the inner loop.

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. Originally Posted by lscamaro
How does this look? Is this right?
Pretty close, check the comments in the code

Code:
```int i,pass;

pass = 1;
*numberCompares = 0;
*numberMoves = 0;

STARTOFDO:
{
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;
}
}
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. Originally Posted by christop
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.

Code:
```int i,pass;

pass = 1;
*numberCompares = 0;
*numberMoves = 0;

STARTOFDO:
{
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;
}
}
i++;
goto FIRSTLOOP;
ENDOFDO:

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.