# Problem with Label in a Do loop, “nolabel”

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

Hold[Goto[top]]

-
related –  Kuba Apr 1 '14 at 12:07

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}]

-
"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. –  Mr.Wizard Jun 26 at 17:02