Line Number - Line Numbers and GOTOs

Line Numbers and GOTOs

In "unstructured" programming languages such as BASIC and Fortran, line numbers were used to specify the targets of branching statements. For example:

10 IF X = 42 GOTO 40 20 X = X + 1 30 GOTO 10 40 PRINT "X is finally 42!"

GOTO-style branching is now widely considered by programmers to be poor programming style, as it tends to lead to the development of spaghetti code. (See Considered harmful, Structured programming.) Even in some later versions of BASIC that still mandated line numbers, the use of line number-controlled GOTOs was phased out whenever possible in favor of cleaner constructs such as the for loop and while loop.

Many modern languages (including C and C++) include a version of the GOTO statement; however, in these languages the target of a GOTO is specified by a line label instead of a line number, and the use of GOTO is strongly discouraged by most programming style guides. The typically accepted use being when it is used to escape out of deep looping. For example:

while(1) { while (1) { if (done) { goto freedom; } } } freedom:

This is much cleaner than the alternative that follows, which needs to have a much more widely scoped `done` variable.

while(1) { while (1) { if (done) { break; } } if (done) { break; } }

Read more about this topic:  Line Number

Famous quotes containing the words line and/or numbers:

    Men are not to be told anything they might find too painful; the secret depths of human nature, the sordid physicalities, might overwhelm or damage them. For instance, men often faint at the sight of their own blood, to which they are not accustomed. For this reason you should never stand behind one in the line at the Red Cross donor clinic.
    Margaret Atwood (b. 1939)

    The barriers of conventionality have been raised so high, and so strangely cemented by long existence, that the only hope of overthrowing them exists in the union of numbers linked together by common opinion and effort ... the united watchword of thousands would strike at the foundation of the false system and annihilate it.
    Mme. Ellen Louise Demorest 1824–1898, U.S. women’s magazine editor and woman’s club movement pioneer. Demorest’s Illustrated Monthly and Mirror of Fashions, p. 203 (January 1870)