Label¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
Label[tag]
Marks a point in a CompoundExpression to which control can be transferred with Goto[tag]. As a statement it evaluates to Null.
Examples (2)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (2)¶
In[1]:= Module[{i = 0, s = 0}, Label[top]; i = i + 1; s = s + i; If[i < 5, Goto[top]]; s]
Out[1]= 15
In[2]:= f[a_] := Module[{x = 1., xp}, Label[begin]; If[Abs[xp - x] < 10^-8, Goto[end]]; xp = x; x = (x + a/x)/2; Goto[begin]; Label[end]; x]; f[2]
Out[2]= 1.41421
Implementation notes¶
- Both are
Protected.tagis evaluated (conventionally a literal symbol or integer) and compared structurally to eachLabel's tag. - Like
Catch/Throw,Gotois implemented by sentinel propagation through the evaluator's normal return paths (nosetjmp/longjmp), so aGotofired inside a nested call (e.g. anIfbranch) still reaches the enclosingCompoundExpression. Leak-free. - A
Gotoloop is a genuine loop with no artificial iteration cap; termination is the program's responsibility (as withWhile). - A
Goto[tag]that reaches the top level with no matchingLabelanywhere emits aGoto::nolabelmessage (stderr) and returns the inertGoto[tag]node. The message fires only when truly unmatched — aGotothat legitimately propagates from an inner to an outerCompoundExpressionmid-evaluation is silent.
Attributes: Protected.
References¶
See also: Goto, CompoundExpression, Catch, Throw, If, While
- Source:
src/core.c - Specification:
docs/spec/builtins/control-flow.md - Tests:
tests/test_goto_label.c