# How can I avoid getting the error message Goto::nolabel in my code?

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


Hold[Goto[begin]]

-
Can you provide a minimal working example that reproduces the problem? –  Michael E2 Apr 19 at 22:12
Are any messages produced? In particular, ones referencing $IterationLimit or $RecursionLimit? –  Oleksandr R. Apr 19 at 22:17
@OleksandrR: no, just Hold[Goto[...]] –  daniel Apr 19 at 22:19
... or put all your code in a single line? –  kguler Apr 19 at 22:27
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]. –  kguler Apr 19 at 22:35

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.

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

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]);

-