2
$\begingroup$

Can someone tell me why this code doesn't work and how to fix it ? Thanks.

k = 0;
Do[{
  Label[top],
  k = k + 1,
  Print["k = ", k],
  If[k < 4, Goto[top]]},
{10}]
k = 1

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

Hold[Goto[top]]
$\endgroup$
1
  • $\begingroup$ related $\endgroup$
    – Kuba
    Apr 1, 2014 at 12:07

1 Answer 1

8
$\begingroup$

According to the documentation center:

Goto first scans any compound expression in which it appears directly, then scans compound expressions that enclose this one.

Your Goto - Label construction is part of the List so Mathematica fails to find the label. Taking this under consideration, the following will work:

k = 0;
Do[{
  Label[top]; k = k + 1; Print["k = ", k]; If[k < 4, Goto[top]]
  }, {10}]
$\endgroup$
1
  • 1
    $\begingroup$ "any compound expression" does not read as CompoundExpression -- I think this is not so clear from the documentation. On these grounds I am reopening this question. If the community votes to close it again however I shall not intervene again. $\endgroup$
    – Mr.Wizard
    Jun 26, 2015 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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