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 (4)¶
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
Applications (2)¶
Label marks the target that Goto returns to
As a bare statement a Label evaluates to Null
Implementation notes¶
Algorithm. Label[tag] marks a jump target inside a CompoundExpression.
Label is Protected. builtin_label validates arity (exactly one argument) and
returns expr_new_symbol(SYM_Null), so evaluated as an ordinary statement a
Label is a no-op worth Null. Its real role is passive: the raw held
Label[tag] node, as it appears literally in the enclosing CompoundExpression,
is what builtin_compoundexpression scans for when it consumes a Goto[tag]
sentinel (see Goto). Tags are compared structurally (conventionally a
literal symbol or integer).
Data structures. None of its own; it is a plain two-node EXPR_FUNCTION that
CompoundExpression reads by position among its statements.
Complexity / limits. A Label is meaningful only as an explicit element of a
CompoundExpression; the matching Goto resolves it by a linear scan of that
compound expression's statements, then of enclosing ones. A Label reached in
normal top-to-bottom flow simply evaluates to Null and execution continues.
- 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/funcprog.c - Specification:
docs/spec/builtins/control-flow.md - Tests:
tests/test_goto_label.c
Notes & additional examples¶
Notes¶
Label[tag] marks a point that Goto[tag] can jump to. It must appear as an
explicit element of a CompoundExpression ā it is the literal Label[tag] node
that the compound expression scans for when it consumes a Goto sentinel.
Evaluated in ordinary top-to-bottom flow a Label is a no-op worth Null, so
reaching one by falling through simply continues to the next statement; only a
Goto gives it an effect.