Line number
Encyclopedia
In computing
Computing
Computing is usually defined as the activity of using and improving computer hardware and software. It is the computer-specific part of information technology...

, a line number is a method used to specify a particular sequence of characters in a text file
Text file
A text file is a kind of computer file that is structured as a sequence of lines of electronic text. A text file exists within a computer file system...

. The most common method of assigning numbers to lines is to assign every line
Line (text file)
In computing, a line is a unit of organization for text files. A line consists of a sequence of zero or more characters, usually displayed within a single horizontal sequence....

 a unique number, starting at 1 for the first line, and incrementing by 1 for each successive line.

In the C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

 the line number of a source code
Source code
In computer science, source code is text written using the format and syntax of the programming language that it is being written in. Such a language is specially designed to facilitate the work of computer programmers, who specify the actions to be performed by a computer mostly by writing source...

 line is one greater than the number of new-line characters read or introduced up to that point.

Line numbers were a required element of syntax
Syntax
In linguistics, syntax is the study of the principles and rules for constructing phrases and sentences in natural languages....

 in some older programming languages such as GW-BASIC
GW-BASIC
GW-BASIC was a dialect of the programming language BASIC developed by Microsoft from BASICA, originally for Compaq. It is compatible with Microsoft/IBM BASICA, but was disk based and did not need the ROM BASIC. It was bundled with MS-DOS operating systems on IBM PC compatibles by Microsoft...

. The primary reason for this is that most operating systems at the time lacked interactive text editors; since the programmer's interface was usually limited to a line editor
Line editor
A line editor is a text editor computer program that manipulates text primarily by the display, modification, and movement of lines. Line editors precede screen-based text editors and originated in an era when a computer operator typically interacted with a teleprinter , with no video display, and...

, line numbers provided a mechanism by which specific lines in the source code
Source code
In computer science, source code is text written using the format and syntax of the programming language that it is being written in. Such a language is specially designed to facilitate the work of computer programmers, who specify the actions to be performed by a computer mostly by writing source...

 could be referenced for editing, and by which the programmer could insert a new line at a specific point. Line numbers also provided a convenient means of distinguishing between code to be entered into the program and direct mode
Direct mode
Direct mode, also known as immediate mode is a computing term referring to the input of textual commands outside the context of a program. The command would be executed immediately and the results printed on screen, in contrast to programming mode where nothing would be executed until a specific...

 commands to be executed immediately when entered by the user (which do not have line numbers).

Largely due to the prevalence of interactive text editing in modern operating system
Operating system
An operating system is a set of programs that manage computer hardware resources and provide common services for application software. The operating system is the most important type of system software in a computer system...

s, line numbers are not a feature of most programming languages.

Line numbers and style

It was a matter of programming style
Programming style
Programming style is a set of rules or guidelines used when writing the source code for a computer program. It is often claimed that following a particular programming style will help programmers to read and understand source code conforming to the style, and help to avoid introducing errors.A...

, if not outright necessity, in these languages to leave gaps between successive line numbers—i.e., a programmer would use the sequence (10, 20, 30, …) rather than (1, 2, 3, …). This permitted the programmer to insert a line of code at a later time. For example, if a line of code between lines 20 and 30 was left out, the programmer might insert the forgotten line at line number 25. If no gaps were left in the numbering, the programmer would be required to renumber line 3 and all subsequent lines in order to insert the new line after line 2. Of course, if the programmer needed to insert more than nine additional lines, renumbering would be required even with the sparser numbering. However, this renumbering would be limited to renumbering only 1 line per ten lines added; when the programmer finds he needs to add a line between 29 and 30, only line 30 would need to be renumbered and line 40 could be left unchanged.

Some BASICs had a RENUM command, which typically would go through the program (or a specified portion of it), reassigning line numbers in equal increments. It would also renumber all references to those line numbers so they would continue to work properly.

In a large program containing subroutine
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

s, each subroutine would usually start at a line number sufficiently large to leave room for expansion of the main program (and previous subroutines). For example, subroutines might begin at lines 10000, 20000, 30000, etc.

Line numbers and GOTOs

In "unstructured" programming languages such as BASIC
BASIC
BASIC is a family of general-purpose, high-level programming languages whose design philosophy emphasizes ease of use - the name is an acronym from Beginner's All-purpose Symbolic Instruction Code....

 and Fortran
Fortran
Fortran is a general-purpose, procedural, imperative programming language that is especially suited to numeric computation and scientific computing...

, line numbers were used to specify the targets of branching statements
Branch (computer science)
A branch is sequence of code in a computer program which is conditionally executed depending on whether the flow of control is altered or not . The term can be used when referring to programs in high level languages as well as program written in machine code or assembly language...

. For example:


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


GOTO
Goto
goto is a statement found in many computer programming languages. It is a combination of the English words go and to. It performs a one-way transfer of control to another line of code; in contrast a function call normally returns control...

-style branching is now widely considered by programmers to be poor programming style, as it tends to lead to the development of spaghetti code
Spaghetti code
Spaghetti code is a pejorative term for source code that has a complex and tangled control structure, especially one using many GOTOs, exceptions, threads, or other "unstructured" branching constructs. It is named such because program flow tends to look like a bowl of spaghetti, i.e. twisted and...

. (See Considered harmful
Considered harmful
In computer science and related disciplines, considered harmful is a phrase popularly used in the titles of diatribes and other critical essays ....

, Structured programming
Structured programming
Structured programming is a programming paradigm aimed on improving the clarity, quality, and development time of a computer program by making extensive use of subroutines, block structures and for and while loops - in contrast to using simple tests and jumps such as the goto statement which could...

.) 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
For loop
In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement....

 and while loop
While loop
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....

.

Many modern languages (including C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 and C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

) 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;
}
}

Line numbers and syntax errors

If a programmer introduces a syntax error
Syntax error
In computer science, a syntax error refers to an error in the syntax of a sequence of characters or tokens that is intended to be written in a particular programming language....

 into a program, the compiler
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...

 (or interpreter
Interpreter (computing)
In computer science, an interpreter normally means a computer program that executes, i.e. performs, instructions written in a programming language...

) will inform the programmer that the attempt to compile (or execute) failed at the given line number. This simplifies the job of finding the error immensely for the programmer.

The use of line numbers to describe the location of errors remains standard in modern programming tools, even though line numbers are never required to be manually specified. It is a simple matter for a program to count the newline
Newline
In computing, a newline, also known as a line break or end-of-line marker, is a special character or sequence of characters signifying the end of a line of text. The name comes from the fact that the next character after the newline will appear on a new line—that is, on the next line below the...

s in a source file and display an automatically generated line number as the location of the error. In IDEs
Integrated development environment
An integrated development environment is a software application that provides comprehensive facilities to computer programmers for software development...

 such as Microsoft Visual Studio
Microsoft Visual Studio
Microsoft Visual Studio is an integrated development environment from Microsoft. It is used to develop console and graphical user interface applications along with Windows Forms applications, web sites, web applications, and web services in both native code together with managed code for all...

 or Xcode
Xcode
Xcode is a suite of tools, developed by Apple, for developing software for Mac OS X and iOS. Xcode 4.2, the latest major version, is available on the Mac App Store for free for Mac OS X 10.7 , and on the Apple Developer Connection website for free to registered developers Xcode is a suite of tools,...

, in which the compiler is usually integrated with the text editor, the programmer can even double-click
Double-click
A double-click is the act of pressing a computer mouse button twice quickly without moving the mouse. Double-clicking allows two different actions to be associated with the same mouse button...

on an error and be taken directly to the line containing that error.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK