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.

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]]
share|improve this question

closed as off-topic by m_goldberg, bobthechemist, Silvia, rasher, Michael E2 Apr 2 at 0:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, bobthechemist, Silvia, rasher, Michael E2
If this question can be reworded to fit the rules in the help center, please edit the question.

    
related –  Kuba Apr 1 at 12:07

1 Answer 1

up vote 5 down vote accepted

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}]
share|improve this answer

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