Group Abstract Group Abstract

Message Boards Message Boards

0
|
4.1K Views
|
5 Replies
|
3 Total Likes
View groups...
Share
Share this post:

How to use Goto function

Posted 3 years ago

I'm having a bit of a syntax problem. I'm trying to figure out how the Goto function works. here is what I have as a simple test case:

i = 0;

Label[start];
i ++;
If[i == 20, Goto[end], Goto[start]];

Label[end];
Print[i]

this should increment i until it equals 20, then print it. However, when I try to run this I get the error message "Goto: nolabel: label start not found". It seems that it can't find the start label, even though it is right there.

I do not understand why it is not working, I followed the syntax in the documentation, and it doesnt seem like it's really any different than the Goto statement in other programming languages...

POSTED BY: Shion Arita
5 Replies
Posted 3 years ago

Thanks a lot, Eric!! Your explanation has gotten me past most of the issues I was having with understanding how things work in Wolfram language. This was extremely helpful. Seeing a lot of these things as infix 'sugar' on top of the lisp-like expression structure really explains what is happening in a way that I haven't seen in any other material I've found about this.

Henrik, your link also has a lot of useful things, thanks!

POSTED BY: Shion Arita
Posted 3 years ago
POSTED BY: Eric Rimbey
Posted 3 years ago
POSTED BY: Shion Arita
Posted 3 years ago

I'll try to answer your question, but first...

DO NOT USE Goto.

I don't know why they added Goto to the language, but you don't need it and it has just led to this specific problem coming up repeatedly.

So, before I answer your question, let me remind you...

DO NOT USE Goto EVER, EVEN AFTER I EXPLAIN HOW TO FIX THIS

I can't even really specify exactly the sequence of events that leads to this, but it has something to do with how the front end parses an expression and then whether it thinks the expression containing the Goto and Label are in the same CompoundExpression (or some containing CompoundExpression higher up the syntax tree). In your case, there's no question that they really are in the same CompoundExpression, but somewhere along the line between front-end parsing and back end evaluation, it got confused and can't determine that the Goto and Label are in the same CompoundExpression.

Here's how you can fix it, but first let me remind you...

IF YOU THINK YOU NEED TO USE Goto IN YOUR CODE, YOU ARE WRONG--YOU DO NOT EVER EVER EVER NEED TO USE Goto

Okay, you need to help the front end (or back end, wherever the confusion comes from) not get confused. You can do this by:

  • Delete all newlines. This way the CompoundExpression will be on one line. Now, the front end actually tries to displays long expressions nicely, so you will see some wraparound, and it may end up looking exactly the same as what you started with. But anyway, newlines cause trouble here.
  • Okay, now we're getting really crazy. Wrap your whole expression in ReleaseHold[Hold[...]]:

    ReleaseHold[Hold[
      i = 0;
      Label[start];
      i++;
      If[i == 20, Goto[end], Goto[start]];
      Label[end]; Print[i]]]
    

    I honestly don't know if that's guaranteed to always work, but it does work for your code. Somehow this interrupts execution in a way that allows the parsing to work correctly. Or something. Shrug.

  • You can also try just wrapping your expression in parentheses. This is another syntax/structure hint to the parser, and it did work on your code, but again, I'm reallly not sure if this is a guaranteed fix.

Now, please forget everything I told you and forget that Goto exists.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard