Student Support Forum: 'Goto doesn't work' topicStudent Support Forum > General > "Goto doesn't work"

 < Previous Comment Help | Reply To Comment | Reply To Topic
 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,

 Subject (listing for 'Goto doesn't work') Author Date Posted Goto doesn't work PhysicsGuy 12/15/12 6:48pm Re: Goto doesn't work jf 12/16/12 7:02pm Re: Re: Goto doesn't work PhysicsGuy 12/16/12 8:50pm Re: Goto doesn't work yehuda 12/17/12 09:05am Re: Goto doesn't work Forum Modera... 12/17/12 10:50am Re: Goto doesn't work Michael 12/17/12 12:10pm
 < Previous Comment Help | Reply To Comment | Reply To Topic