7
$\begingroup$

If anyone could suggest a reason why Label[begin] ... Goto[begin] would return Hold[Goto[Begin]], and (better) how can I avoid this problem, I'd appreciate it. I suspect this is an outmoded control structure for simple programs, but I like it.

I had an earlier version of Mathematica and it was fickle. Sometimes adjusting the spacing cured the problem. The newer version is less forgiving, if it is user error.

Edit: a silly example maybe but it gives the error message.

p[1] = .9; i = 1; Label[begin];
i++;
p[i] = p[i - 1] + 1;
Print[i];
If[i < 5, Goto[begin], Goto[end]];
Label[end];
2

Goto::nolabel: Label begin not found. >>

Hold[Goto[begin]]
$\endgroup$
11
  • 1
    $\begingroup$ Can you provide a minimal working example that reproduces the problem? $\endgroup$
    – Michael E2
    Commented Apr 19, 2014 at 22:12
  • $\begingroup$ Are any messages produced? In particular, ones referencing $IterationLimit or $RecursionLimit? $\endgroup$ Commented Apr 19, 2014 at 22:17
  • $\begingroup$ @OleksandrR: no, just Hold[Goto[...]] $\endgroup$
    – daniel
    Commented Apr 19, 2014 at 22:19
  • 2
    $\begingroup$ ... or put all your code in a single line? $\endgroup$
    – kglr
    Commented Apr 19, 2014 at 22:27
  • 1
    $\begingroup$ No idea what is causing the problem. Apparently, without the parentheses the parser is seeing multiple compound expressions; and the label begin is not in the same compound expression that contains Goto[label]. $\endgroup$
    – kglr
    Commented Apr 19, 2014 at 22:35

2 Answers 2

11
$\begingroup$

From the docs for Label

Label must appear as an explicit element of a CompoundExpression object.

So this works:

p[1] = .9; i = 1; Label[begin]; i++; p[i] = p[i - 1] + 1; Print[i]; If[i < 5, Goto[begin], Goto[end]]; Label[end];

But this doesn't:

p[1] = .9; i = 1; Label[begin];
i++;
p[i] = p[i - 1] + 1;
Print[i];
If[i < 5, Goto[begin], Goto[end]];
Label[end];

It's because the newlines in an input cell separate the CompoundExpression into several input expressions.

$\endgroup$
5
  • $\begingroup$ I find kguler's solution easier but this is good to know. $\endgroup$
    – daniel
    Commented Apr 19, 2014 at 22:34
  • 3
    $\begingroup$ @daniel Yes, either way, it keeps the input a single CompoundExpression. Parentheses are usually what I use, too, for the same reasons, I suspect. $\endgroup$
    – Michael E2
    Commented Apr 19, 2014 at 22:37
  • $\begingroup$ Isn't it a duplicate that you've closed one time? :P $\endgroup$
    – Kuba
    Commented Apr 20, 2014 at 20:16
  • $\begingroup$ @Kuba D'oh. Memory fault. +1 to you! :) I guess nobody remembers, since there are any close votes yet. $\endgroup$
    – Michael E2
    Commented Apr 20, 2014 at 20:34
  • $\begingroup$ Happens :) +1 for different documentation quote :) $\endgroup$
    – Kuba
    Commented Apr 20, 2014 at 20:38
5
$\begingroup$

A fix with minimal editing is to introduce parentheses.

p[1] = .9; i = 1;
(Label[begin];
 i++;
 p[i] = p[i - 1] + 1;
 Print[i];
 If[i < 5, Goto[begin], Goto[end]];
 Label[end]);
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.