Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (degenerate_762, DonQuijote), 933 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Improve goto (no really!) #395035
02/19/12 03:20
02/19/12 03:20
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline OP
Expert
JibbSmart  Offline OP
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
From the manual:
Originally Posted By: goto label
The target label must be within the same winged brackets ({ .. }) as the goto instruction.
This is unusual, as far as I've seen in C and C++ -- normally the only requirement is that the label is within the same function.

I know use of goto is normally frowned upon, but consider the following small piece of code:
Code:
int i, j, k;
for (i=0; i<4; i++) {
	for (j=i+1; j<4; j++) {
		for (k=j+1; k<4; k++) {
			... do stuff ...
			goto foundFace;
		}
	}
}
foundFace:
...

We have some nested loops, in this case to consider the faces of a tetrahedron and stop as soon as we find a face that fits certain conditions. Normally we'd exit a loop with "break", but the only way to do so here would be to have a "break" within each loop, and set another variable to indicate it's time to break, which each loop would check, so it might as well be included as an extra condition in all but the inner-most loop. That's ugly and a little inefficient. Another option would be to put all the loops in a separate function, and use "return" instead of "goto", but this is the only instance where I need these loops since they serve a very specific purpose, and calling a new function for this purpose would be inefficient.

Yes, if I'm so concerned about efficiency I could unroll the loops. But that's not the problem -- it's the balance of efficiency and readability, and the use of "goto" is the most readable in this case. "goto" can lead to spaghetti code, but it can also be used as a deep "break".

So yeah, between the other stuff you guys have going on, if it's not too much trouble, I think it'd be nice if Lite-C was a little more C-like in this regard -- "goto" shouldn't be confined to a label in the same set of winged brackets laugh


Formerly known as JulzMighty.
I made KarBOOM!
Re: Improve goto (no really!) [Re: JibbSmart] #395036
02/19/12 03:24
02/19/12 03:24
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
I've got nothing against the suggestion to re-implement the old behaviour of goto, but what I do in such a case is the following:

for (i=0; i<4; i++) {
for (j=i+1; j<4; j++) {
for (k=j+1; k<4; k++) {
... do stuff ...
j = 4; i = 4; break;
}
}
}


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Re: Improve goto (no really!) [Re: Superku] #395037
02/19/12 03:26
02/19/12 03:26
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline OP
Expert
JibbSmart  Offline OP
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
Oh true! How did I not think of that? That's what I'll do here.

As an example of where goto would be useful, though, consider the case that the i, j, and k variables should be preserved for the rest of the function (which, thankfully, is not the case in my application).

Thanks laugh


Formerly known as JulzMighty.
I made KarBOOM!
Re: Improve goto (no really!) [Re: JibbSmart] #395085
02/19/12 19:08
02/19/12 19:08
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
Superku, of course your approach is working, but it's not a good style from my point of view as it introduces new dependencies. Whenever you change the limit of the loops you have to remember that you have to adapt the inner assignment as well. That is likely to be forgotten.

I'd introduce a new function, extract the three nested loops to this function and use a return instead of the break.


Always learn from history, to be sure you make the same mistakes again...
Re: Improve goto (no really!) [Re: Uhrwerk] #395090
02/19/12 19:28
02/19/12 19:28
Joined: Jul 2008
Posts: 1,178
England
M
MrGuest Offline
Serious User
MrGuest  Offline
Serious User
M

Joined: Jul 2008
Posts: 1,178
England
how about
bFindFace = true;
for (i=0; i<4 && bFindFace; i++) {
for (j=i+1; j<4 && bFindFace; j++) {
for (k=j+1; k<4 && bFindFace; k++) {
bFindFace = false;
... do stuff ...
}
}
}

Re: Improve goto (no really!) [Re: MrGuest] #395114
02/19/12 23:10
02/19/12 23:10
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline OP
Expert
JibbSmart  Offline OP
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
MrGuest, that works, but it introduces conditions that will be checked for every loop. It's not much, but it's inefficient, and would be avoided if goto behaved a little more like C.

Uhrwerk, I don't want to introduce a function I'm only going to use once, but I agree it's the "cleanest" solution without goto.

I went with Superku's because it's a little more efficient, and I don't need to preserve i or j -- I do everything I need to with them within the loop.

I understand Lite-C isn't the same as C in every respect, but "goto" is really useless here.


Formerly known as JulzMighty.
I made KarBOOM!
Re: Improve goto (no really!) [Re: JibbSmart] #396261
03/04/12 23:02
03/04/12 23:02
Joined: Jun 2004
Posts: 2,234
Wisconsin USA
FoxHound Offline
Expert
FoxHound  Offline
Expert

Joined: Jun 2004
Posts: 2,234
Wisconsin USA
Just Copy those vara when you would have used got and the reassign those values back.


---------------------
There is no signature here.


QUIT LOOKING FOR ONE!
Re: Improve goto (no really!) [Re: FoxHound] #396262
03/04/12 23:44
03/04/12 23:44
Joined: Feb 2010
Posts: 886
Random Offline
User
Random  Offline
User

Joined: Feb 2010
Posts: 886
Would this work in a while loop?



Re: Improve goto (no really!) [Re: Random] #404149
07/03/12 15:35
07/03/12 15:35
Joined: Apr 2006
Posts: 159
Latvija
Arrovs Offline
Member
Arrovs  Offline
Member

Joined: Apr 2006
Posts: 159
Latvija
i really really agree.
I vant old goto - my code have statements vhich cant be vritten in svitch and in break place i have goto to get out of multiple state execution.


Arrovs once will publish game
Re: Improve goto (no really!) [Re: Arrovs] #404166
07/03/12 18:34
07/03/12 18:34
Joined: Mar 2006
Posts: 2,252
Hummel Offline
Expert
Hummel  Offline
Expert

Joined: Mar 2006
Posts: 2,252
"break n" (n - number of loops to break) could be a suitable alternative here, since "goto" is unlikely to be re-implemented, I think.

Page 1 of 2 1 2

Moderated by  aztec, Spirit 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1