| Author |
Comment/Response |
Michael
|
12/17/12 12:10pm
The issue, I suspect, is how Mathematica interprets your input. An expression, followed by ";", followed by a newline is a complete expression. The first two would then be two independent expressions. So the For loop is a complete expression that has no Labels in it, as required by Goto.
The example posted by the Forum Moderator works because the lines are contained inside Module[..] and the expression is not complete until the closing "]".
For instance, putting parentheses around the whole thing works (I didn't like the infinite loop, so I changed the code):
(Label["zu"];
Print["zuzu"];
For[i = 1, i < 10, i++, Print["."];
If[i == 5, Goto["zu"], If[i == 4, Goto["bu"]]]];
Label["bu"];)
Now, my explanation is missing something, I'm misunderstanding something, or Mathematica has a bug. The docs (see link) suggest that the following should work, but they don't:
Label["zu"]; \[Continuation]
Print["zuzu"]; \[Continuation]
For[i = 1, i < 10, i++, Print["."];
If[i == 5, Goto["zu"], If[i == 7, Goto["bu"]]]];\[Continuation]
Label["bu"];
The next is worse, in that the backslash connects the first symbol of the next line, so that in this case the previous line becomes part of the Head of the function call:
Label["zu"]; \
Print["zuzu"]; \
For[i = 1, i < 10, i++, Print["."];
If[i == 5, Goto["zu"], If[i == 7, Goto["bu"]]]]; \
Label["bu"]
Try it. The output is
(Label Null)["bu"]
because
expr;\
Label["bu"]
is interpreted as something equivalent to
((expr;) Label)["bu"]
That seems likely to be a bug.
FInally, let me recommend Catch and Throw for handling deep exits gracefully.
URL: http://reference.wolfram.com/mathematica/tutorial/InputSyntax.html#14153, |
|