Why there are no "goto", "break" and "switch" commands?

Started by , Mon 05/02/2007 00:14:34

Previous topic - Next topic

DjCzermino

Hi, I have a simple question,
why in the script editor there are no "goto", "for" "switch"  and "break" commands?

i am working on very complex game and i want to optimize my code, but i cant because there are no goto or break/continue commands like C/C++ language...

Why? thankx for reply... Martin, Czech Republic

Alynn

Well, the short answer is: "Because Chris says so", or perhaps "It's a limitation of the scripting language."

But IMO... Goto, it shouldn't be used, usually it's still in many programming languages, but it can cause major problems, there isn't anything that goto does that can't be done another way.

for loops and switch, Yeah, I've wanted these two myself, mostly the switch case blocks since I use plenty code that would use this, instead of all the if else if's.

While you can't really break out of a loop when a certain condition is met, you can use return; to break out of a method procedure. However, since I've never needed to do this, I'm not sure if it stops processing all scripting, or just the current procedure in the stack. If it's just the current procedure then you could theoretically use return; as a break for that instance. However if you want to break out of a loop when a certain condition is met, then continue in the same procedure after that... I don't believe that's possible currently.

It's just one of those things I just got used to, and don't think about too much. However I don't think you will be the last person to ask this, and I know you aren't the first.

Khris

A switch keyword would indeed be helpful, it might already be in the tracker.

As for goto, you'll find you won't need it once you've accommodated yourself to AGS's script language.

I've done quite complex scripting stuff yet, but the lack of switch/break never has bothered me much, and in most cases I was able to work around it without 10 else-ifs in a row.

return [param]; will return from the current function, it can't be used to exit if-statements or the like.

It's a bit like with spoken languages, sometimes you can't translate something directly, but the proper translation might even be more elegant than the original.

SSH

Yeah, return works like a break statement if you farm your loop out to its own function. But really, these are all syntactic sugar that can be worked around...
12

monkey0506

#4
Mmmm...syntaxshugar...two lumps please. :=

An alternative to the break keyword that I've used before is to set a bool variable like this:

Code: ags
bool break = false;
while (!break) {
  // ...
  if (blah == bleh) break = true;
  }


You could just set your bool (i.e., break) to true whenever whatever conditions necessary to satisfy the loop have been met. It's not quite the same, sure, but essentially serves the same function (breaking out of the loop early).

As for "for loops":

Code: ags
for (int i = 0; i < 10; i++) {}


Is the same as:

Code: ags
int i = 0;
while (i < 10) i++;


Which is why for-loops are a low priority request.

Switch statements can be worked around by if-else clauses.

And gotos should be avoided at all costs...IMO. To me it makes the code harder to follow, and therefore easier to make mistakes. If you really want to separate those code snippets, why not just write a new function?

Pumaman

Since you're asking "why", the answer is "because I haven't got round to it yet".

"goto" is an old fashioned syntax that is no longer used in modern programming languages and will not be implemented.
"break" can be simulated as monkey_05_06 has demonstrated (though I would like to add support for it in future).
"switch" can be simulated by using "else if"s (thoug again, I agree it's not as nice and it would be good to add switch support in future).

If you're needing to rely on goto statements to optimise your code, perhaps you could post a sample and we could suggest how it could be rewritten -- you shouldn't need to use gotos in this day and age.

SMF spam blocked by CleanTalk