Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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]]
share|improve this question
1  
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
2  
... or put all your code in a single line? –  kguler Apr 19 at 22:27
1  
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
show 6 more comments

2 Answers 2

up vote 8 down vote accepted

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.

share|improve this answer
    
I find kguler's solution easier but this is good to know. –  daniel Apr 19 at 22:34
2  
@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
add comment

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]);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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