Label (computer science)

Last updated

In programming languages, a label is a sequence of characters that identifies a location within source code. In most languages, labels take the form of an identifier, often followed by a punctuation character (e.g., a colon). In many high-level languages, the purpose of a label is to act as the destination of a GOTO statement. [1] [2] In assembly language, labels can be used anywhere an address can (for example, as the operand of a JMP or MOV instruction). [3] Also in Pascal and its derived variations. Some languages, such as Fortran and BASIC, support numeric labels. [4] Labels are also used to identify an entry point into a compiled sequence of statements (e.g., during debugging).

Contents

C

In C a label identifies a statement in the code. A single statement can have multiple labels. Labels just indicate locations in the code and reaching a label has no effect on the actual execution.

Function labels

Function labels consist of an identifier, followed by a colon. Each such label points to a statement in a function and its identifier must be unique within that function. Other functions may use the same name for a label. Label identifiers occupy their own namespace – one can have variables and functions with the same name as a label.

voidfoo(intnumber){if(number<0)gotoerror;bar(number);return;error:fprintf(stderr,"Invalid number!\n");}

Here error is the label. The statement goto can be used to jump to a labeled statement in the code. After a goto, program execution continues with the statement after the label.

Switch labels

Two types of labels can be put in a switch statement. A case label consists of the keyword case, followed by an expression that evaluates to integer constant. A default label consists of the keyword default. Case labels are used to associate an integer value with a statement in the code. When a switch statement is reached, program execution continues with the statement after the case label with value that matches the value in the parentheses of the switch. If there is no such case label, but there is a default label, program execution continues with the statement after the default label. If there is no default label, program execution continues after the switch.

switch(die){default:printf("invalid\n");break;case1:case3:case5:printf("odd\n");break;case2:case4:case6:printf("even\n");break;}

Within a single switch statement, the integer constant associated with each case label must be unique. There may or may not be a default statement. There is no restriction on the order of the labels within a switch. The requirement that case labels values evaluate to integer constants gives the compiler more room for optimizations.

Examples

Javascript

In JavaScript language syntax statements may be preceded by the label:

top://Label the outermost for-loop.for(vari=0;i<4;i++){for(varj=0;j<4;j++){if(j===3&&i===2){alert("i="+i+", j="+j);//i=2, j=3breaktop;}}}alert("i="+i+", j="+j);//i=2, j=3

It also possible to use break statement to break out of the code blocks:

top:{console.log("foo")console.log("bar")breaktopconsole.log("baz")}// Which would output: // > foo// > bar

Common Lisp

In Common Lisp two ways of defining labels exist. The first one involves the tagbody special operator. Distinguishing its usage from many other programming languages that permit global navigation, such as C, the labels are only accessible in the context of this operator. Inside of a tagbody labels are defined as forms starting with a symbol; the go special form permits a transfer of control between these labels. [5]

(let((iterationNIL))(tagbodystart(print'started)(setfiteration0)increase(printiteration)(incfiteration1)(gocheck)check(if(>=iteration10)(goend)(goincrease))end(print'done)))

A second method utilizes the reader macros #n= and #n#, the former of which labels the object immediately following it, the latter refers to its evaluated value. [6] Labels in this sense constitute rather an alternative to variables, with #n= declaring and initializing a “variable” and #n# accessing it. The placeholder n designates a chosen unsigned decimal integer identifying the label.

(progn#1="hello"(print#1#))

Apart from that, some forms permit or mandate the declaration of a label for later referral, including the special form block which prescribes a naming, and the loop macro that can be identified by a named clause. Immediate departure from a named form is possible by using the return-from special operator.

(blockmyblock(loopforiterationfrom0do(if(>=iteration10)(return-frommyblock'done)(printiteration))))
(loopnamedmyloopforiterationfrom0do(if(>=iteration10)(return-frommyloop'done)(printiteration)))

In a fashion similar to C, the macros case, ccase, ecase, [7] typecase, ctypecase and etypecase define switch statements. [8]

(let((my-value5))(casemy-value(1(print"one"))(2(print"two"))((345)(print"three four or five"))(otherwise(print"any other value"))))
(let((my-value5))(typecasemy-value(list(print"a list"))(string(print"a string"))(number(print"a number"))(otherwise(print"any other type"))))

See also

Related Research Articles

In computer programming, an infinite loop is a sequence of instructions that, as written, will continue endlessly, unless an external intervention occurs. It may be intentional.

In computer science, control flow is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. The emphasis on explicit control flow distinguishes an imperative programming language from a declarative programming language.

In computer programming, the scope of a name binding is the part of a program where the name binding is valid; that is, where the name can be used to refer to the entity. In other parts of the program, the name may refer to a different entity, or to nothing at all. Scope helps prevent name collisions by allowing the same name to refer to different objects – as long as the names have separate scopes. The scope of a name binding is also known as the visibility of an entity, particularly in older or more technical literature—this is from the perspective of the referenced entity, not the referencing name.

Pretty-printing is the application of any of various stylistic formatting conventions to text files, such as source code, markup, and similar kinds of content. These formatting conventions may entail adhering to an indentation style, using different color and typeface to highlight syntactic elements of source code, or adjusting size, to make the content easier for people to read, and understand. Pretty-printers for source code are sometimes called code formatters or beautifiers.

The syntax of the C programming language is the set of rules governing writing of software in the C language. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively high-level data abstraction. C was the first widely successful high-level language for portable operating-system development.

<span class="mw-page-title-main">While loop</span> Control flow statement for repeating execution until a condition is met

In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.

<span class="mw-page-title-main">For loop</span> Control flow statement for repeated execution

In computer science a for-loop or for loop is a control flow statement for specifying iteration. Specifically, a for loop functions by running a section of code repeatedly until a certain condition has been satisfied.

<span class="mw-page-title-main">Foreach loop</span> Control flow statement for traversing items in a collection

In computer programming, foreach loop is a control flow statement for traversing items in a collection. foreach is usually used in place of a standard for loop statement. Unlike other for loop constructs, however, foreach loops usually maintain no explicit counter: they essentially say "do this to everything in this set", rather than "do this x times". This avoids potential off-by-one errors and makes code simpler to read. In object-oriented languages, an iterator, even if implicit, is often used as the means of traversal.

In most computer programming languages a do while loop is a control flow statement that executes a block of code and then either repeats the block or exits the loop depending on a given boolean condition.

<span class="mw-page-title-main">Java syntax</span> Set of rules defining correctly structured program

The syntax of Java is the set of rules defining how a Java program is written and interpreted.

In computer science, a tail call is a subroutine call performed as the final action of a procedure. If the target of a tail is the same subroutine, the subroutine is said to be tail recursive, which is a special case of direct recursion. Tail recursion is particularly useful, and is often easy to optimize in implementations.

In mathematics and in computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages.

xHarbour is a free multi-platform extended Clipper compiler, offering multiple graphic terminals (GTs), including console drivers, GUIs, and hybrid console/GUIs. xHarbour is backward-compatible with Clipper and supports many language syntax extensions, greatly extended run-time libraries, and extensive third party support.

Exception handling syntax is the set of keywords and/or structures provided by a computer programming language to allow exception handling, which separates the handling of errors that arise during a program's operation from its ordinary processes. Syntax for exception handling varies between programming languages, partly to cover semantic differences but largely to fit into each language's overall syntactic structure. Some languages do not call the relevant concept "exception handling"; others may not have direct facilities for it, but can still provide means to implement it.

setjmp.h is a header defined in the C standard library to provide "non-local jumps": control flow that deviates from the usual subroutine call and return sequence. The complementary functions setjmp and longjmp provide this functionality.

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

The syntax and semantics of PHP, a programming language, form a set of rules that define how a PHP program can be written and interpreted.

<span class="mw-page-title-main">Goto</span> One-way control statement in computer programming

Goto is a statement found in many computer programming languages. It performs a one-way transfer of control to another line of code; in contrast a function call normally returns control. The jumped-to locations are usually identified using labels, though some languages use line numbers. At the machine code level, a goto is a form of branch or jump statement, in some cases combined with a stack adjustment. Many languages support the goto statement, and many do not.

In computer programming, ellipsis notation is used to denote ranges, an unspecified number of arguments, or a parent directory. Most programming languages require the ellipsis to be written as a series of periods; a single (Unicode) ellipsis character cannot be used.

References

  1. "C Standard section 6.8.6.1 The goto statement". Archived from the original on 2007-12-24. Retrieved 2008-07-03.
  2. "GOTO Statement QuickSCREEN". Microsoft. 1988. Archived from the original on 2009-07-25. Retrieved 2008-07-03.
  3. O. Lawlor. "nasm x86 Assembly" . Retrieved 2008-07-03.
  4. "Differences Between GW-BASIC and QBasic". Archived from the original on 2010-02-10.
  5. Kent Pitman. "CLHS: Special Operator TAGBODY" . Retrieved 2020-08-18.
  6. Kent Pitman. "CLHS: Section 2.4.8" . Retrieved 2020-08-18.
  7. Kent Pitman. "CLHS: Macro CASE, CCASE, ECASE" . Retrieved 2020-08-20.
  8. Kent Pitman. "CLSH: Macro TYPECASE, CTYPECASE, ETYPECASE" . Retrieved 2020-08-20.