do-while vs. goto?
#1

Hey, this has been bothering me for quite some time now...
Is there any difference between using a goto jump or a do-while loop? For example:
pawn Код:
RANDOMIZATION:
randomSongPick = random(sizeof(aPlayList));
if(aCurrentlyPlaying[playerid] == aPlayList[randomSongPick][0][0]) goto RANDOMIZATION;
And:
pawn Код:
do randomSongPick = random(sizeof(aPlayList));
while(aCurrentlyPlaying[playerid] == aPlayList[randomSongPick][0][0]);
Reply
#2

They really aren't the same thing. Do-while loops are just like while loops except they will always be executed at least once. Goto is also a very bad practice; you can search all over the Internet to find evidence to support that claim:
Quote:

When my students ask me this question, I explain in the following way:

Monopoly is a very simple game. Everyone has played it, and invariably, people modify it with their own "house rules". A rule that you get money if you land in free-parking, or new rules about going to and staying in jail, bankruptcy rules, partnering rules, etc...

Imagine going to a friends house, and they are using house rules. Ok, not a problem, you just have to learn the rules, and use them a while, and you're good.

But what if they don't tell you the house rules. And what if there are so many of them that they keep popping up, and interacting in strange ways that are totally foreign to how you thought Monopoly was supposed to be played...

A goto statement is like a house rule. Programming is hard enough to understand when you can reliably assume that you start at the top, execute 1 line at a time, going down, one line at a time. Using a goto statement throws that assumption out the window, and all of the sudden, the game is uncertain. It's a new and uncertain rule.

Yeah, goto's seem fine to YOU if you wrote the program, but if anyone else wants to understand it, you will have to explain it to them. It's your house rule. If you don't explain it, others will get frustrated trying to figure it out. And no one will want to read your code.

No one will want to play your weird game with all of the house rules. We don't care if you think your game is good, it takes too long to learn to play, and no one else understands it.

The bottom line is that if you use goto statements, real programmers, skilled and practiced professionals, who are worthwhile to associate with, will not want to read your code. And many of them will not want to associate with you because of that, at least professionally.

If you insist on writing bad code, there are plenty of ways to keep doing it without goto .

Your example above can also be achieved by a standard while loop:
pawn Код:
while(aCurrentlyPlaying[playerid] == aPlayerList[randomSongPick][0][0]) randomSongPick = random(sizeof(aPlayList));
Reply
#3

Quote:
Originally Posted by Bakr
Посмотреть сообщение
They really aren't the same thing. Do-while loops are just like while loops except they will always be executed at least once. Goto is also a very bad practice; you can search all over the Internet to find evidence to support that claim:


Your example above can also be achieved by a standard while loop:
pawn Код:
while(aCurrentlyPlaying[playerid] == aPlayerList[randomSongPick][0][0]) randomSongPick = random(sizeof(aPlayList));
Yeah, thanks for fixing my mistake with the loop, but my point with opening this thread was basically seeing the difference between the two and this code was just an example.
Reply
#4

They are not alike at all really. pawn-lang.pdf is a GREAT resource!
Quote:

goto label
Moves program control (unconditionally) to the statement that follows
the specified label. The label must be within the same function
as the goto statement (a goto statement cannot jump out of a function).

Quote:

do statement while ( expression )
Executes a statement before the condition part (the while clause) is
evaluated. The statement is repeated while the condition is logically
“true”. The statement is at least executed once.

Reply
#5

Quote:
Originally Posted by Bakr
Посмотреть сообщение
They are not alike at all really. pawn-lang.pdf is a GREAT resource!
I realise that their definition is not the same and that they were not practically made for the same purpose, but I wanted to see the differences between "loops" that are made with jump statements and loops that are made using while/do-while.
I mean, in Assembly, the only loops you can do are actually conditioned jump statements, that rely on flag states.
My point was, you can basically do most of the loops you do with while/do-while using goto. (EVEN THOUGH they are not meant to serve the same purposes)
And the questions that come up from it - would it be the same? Slower? Faster? Code-wise - would it be better?
Reply
#6

Ah.

As far as answering the speed questions, it depends how they are compiled, which I know virtually nothing about. You could write up some speeds tests, but I doubt one would be significantly faster than the others. Code-wise, goto statements are very poor practice. Again, you can read up why on the thousands of cases documented.
Reply
#7

I recall reading that we actually have loops to avoid having to use goto. Although, there should be nothing explicitly wrong with it in many coding languages, it is considered bad practice. When you use goto, it is an indication that your code could probably be built up in a better fashion (so it is less confusing).

It is up to you to run the speed tests, also see what the assembly output looks like (compile with -a) and see the differences.

Short version - structured programming is here to avoid goto.
Reply
#8

Quote:
Originally Posted by Bakr
Посмотреть сообщение
Ah.

As far as answering the speed questions, it depends how they are compiled, which I know virtually nothing about. You could write up some speeds tests, but I doubt one would be significantly faster than the others. Code-wise, goto statements are very poor practice. Again, you can read up why on the thousands of cases documented.
From reading up, I see that the only fault of goto statements is that so called "bad practice".
It makes your code look like it could've been written better, and it looks bad on the eye.
However, from looking on my example again, I don't see anything too messy or painful to understand, just a simple jump statement.

Quote:

I recall reading that we actually have loops to avoid having to use goto. Although, there should be nothing explicitly wrong with it in many coding languages, it is considered bad practice. When you use goto, it is an indication that your code could probably be built up in a better fashion (so it is less confusing).

It is up to you to run the speed tests, also see what the assembly output looks like (compile with -a) and see the differences.

Short version - structured programming is here to avoid goto.

Well, I actually got the point that if you use goto, people regard to your code as pretty bad, confusing and messy.
But then again, I don't see anything confusing in the basic example that I wrote that is hard to understand.
It is used in a simple loop, with only one condition, like most of the loops scripters around here write.
A loop can be badly written regardless of it consists goto statements or do-while loops, it is all up to the organization the scripter dedicates to his coding.

Why was this moved here?
This topic is a discussion and not a help request.
Reply
#9

Try digging deeper into why people decided to look for alternatives to goto. The basic encounters were much more complicated than the first one here. And more about strucuted programming.

Also, I did a small speed test which revealed that using a while loop instead of goto in a situation such as yours is about 1.3-1.4 times faster. But please do have a look yourself.

But then again, I'm not saying goto is hideous or awful. It probably is not, and I have used it myself in the past for working with large loops, but retired from doing so after seeing a famous comic.
Reply
#10


I'll just leave jump statements to ASM I guess.
I am kinda working on something so I'm not in the mood of doing a speed test right now, because I'll ditch goto anyway.
Now it's even better as I know I'm not losing anything.
Just wanted to create an interesting discussion, though.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)