Statement (computer science)

Last updated

In computer programming, a statement is a syntactic unit of an imperative programming language that expresses some action to be carried out. [1] A program written in such a language is formed by a sequence of one or more statements. A statement may have internal components (e.g. expressions).

Contents

Many programming languages (e.g. Ada, Algol 60, C, Java, Pascal) make a distinction between statements and definitions/declarations. A definition or declaration specifies the data on which a program is to operate, while a statement specifies the actions to be taken with that data.

Statements which cannot contain other statements are simple; those which can contain other statements are compound. [2]

The appearance of a statement (and indeed a program) is determined by its syntax or grammar. The meaning of a statement is determined by its semantics.

Simple statements

Simple statements are complete in themselves; these include assignments, subroutine calls, and a few statements which may significantly affect the program flow of control (e.g. goto, return, stop/halt). In some languages, input and output, assertions, and exits are handled by special statements, while other languages use calls to predefined subroutines.

Compound statements

Compound statements may contain (sequences of) statements, nestable to any reasonable depth, and generally involve tests to decide whether or not to obey or repeat these contained statements.

Notation for the following examples:
  • <statement> is any single statement (could be simple or compound).
  • <sequence> is any sequence of zero or more <statements>
Some programming languages provide a general way of grouping statements together, so that any single <statement> can be replaced by a group:
  • Algol 60: begin <sequence> end
  • Pascal: begin <sequence> end
  • C, PHP, Java: { <sequence> }
Other programming languages have a different special terminator on each kind of compound statement, so that one or more statements are automatically treated as a group:
  • Ada: if test then <sequence> end if;

Many compound statements are loop commands or choice commands. In theory only one of each of these types of commands is required. In practice there are various special cases which occur quite often; these may make a program easier to understand, may make programming easier, and can often be implemented much more efficiently. There are many subtleties not mentioned here; see the linked articles for details.

Syntax

Apart from assignments and subroutine calls, most languages start each statement with a special word (e.g. goto, if, while, etc.) as shown in the above examples. Various methods have been used to describe the form of statements in different languages; the more formal methods tend to be more precise:

BNF uses recursion to express repetition, so various extensions have been proposed to allow direct indication of repetition.

Statements and keywords

Some programming language grammars reserve keywords or mark them specially, and do not allow them to be used as identifiers. This often leads to grammars which are easier to parse, requiring less lookahead.

No distinguished keywords

Fortran and PL/1 do not have reserved keywords, allowing statements like:

  • in PL/1:
    • IF IF = THEN THEN ... (the second IF and the first THEN are variables).
  • in Fortran:
    • IF (A) X = 10... conditional statement (with other variants)
    • IF (A) = 2 assignment to a subscripted variable named IF
As spaces were optional up to Fortran 95, a typo could completely change the meaning of a statement:
  • DO 10 I = 1,5 start of a loop with I running from 1 to 5
  • DO 10 I = 1.5 assignment of the value 1.5 to the variable DO10I

Flagged words

In Algol 60 and Algol 68, special tokens were distinguished explicitly: for publication, in boldface e.g. begin; for programming, with some special marking, e.g., a flag ('begin), quotation marks ('begin'), or underlined (begin on the Elliott 503). This is called "stropping".

Tokens that are part of the language syntax thus do not conflict with programmer-defined names.

Reserved keywords

Certain names are reserved as part of the programming language and can not be used as programmer-defined names. The majority of the most popular programming languages use reserved keywords. Early examples include FLOW-MATIC (1953) and COBOL (1959). Since 1970 other examples include Ada, C, C++, Java, and Pascal. The number of reserved words depends on the language: C has about 30 while COBOL has about 400.

Semantics

Semantics is concerned with the meaning of a program. The standards documents for many programming languages use BNF or some equivalent to express the syntax/grammar in a fairly formal and precise way, but the semantics/meaning of the program is generally described using examples and English prose. This can result in ambiguity. [8] In some language descriptions the meaning of compound statements is defined by the use of 'simpler' constructions, e.g. a while loop can be defined by a combination of tests, jumps, and labels, using if and goto.

The semantics article describes several mathematical/logical formalisms which have been used to specify semantics in a precise way; these are generally more complicated than BNF, and no single approach is generally accepted as the way to go. Some approaches effectively define an interpreter for the language, some use formal logic to reason about a program, some attach affixes to syntactic entities to ensure consistency, etc.

Expressions

A distinction is often made between statements, which are executed, and expressions, which are evaluated. Expressions always evaluate to a value, which statements do not. However, expressions are often used as part of a larger statement.

In most programming languages, a statement can consist of little more than an expression, usually by following the expression with a statement terminator (semicolon). In such a case, while the expression evaluates to a value, the complete statement does not (the expression's value is discarded). For instance, in C, C++, C#, and many similar languages, x = y + 1 is an expression that will set x to the value of y plus one, and the whole expression itself will evaluate to the same value that x is set to. However, x = y + 1; (note the semicolon at the end) is a statement that will still set x to the value of y plus one because the expression within the statement is still evaluated, but the result of the expression is discarded, and the statement itself does not evaluate to any value. [9]

Expressions can also be contained within other expressions. For instance, the expression x = y + 1 contains the expression y + 1, which in turn contains the values y and 1, which are also technically expressions.

Although the previous examples show assignment expressions, some languages do not implement assignment as an expression, but rather as a statement. A notable example of this is Python, where = is not an operator, but rather just a separator in the assignment statement. Although Python allows multiple assignments as each assignment were an expression, this is simply a special case of the assignment statement built into the language grammar rather than a true expression. [10]

Extensibility

Most languages have a fixed set of statements defined by the language, but there have been experiments with extensible languages that allow the programmer to define new statements.

See also

Related Research Articles

Structured programming is a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making extensive use of the structured control flow constructs of selection (if/then/else) and repetition, block structures, and subroutines.

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 science, Backus–Naur form is a notation used to describe the syntax of programming languages or other formal languages. It was developed by John Backus and Peter Naur. BNF can be described as a metasyntax notation for context-free grammars. Backus–Naur form is applied wherever exact descriptions of languages are needed, such as in official language specifications, in manuals, and in textbooks on programming language theory. BNF can be used to describe document formats, instruction sets, and communication protocols.

In computer science, a high-level programming language is a programming language with strong abstraction from the details of the computer. In contrast to low-level programming languages, it may use natural language elements, be easier to use, or may automate significant areas of computing systems, making the process of developing a program simpler and more understandable than when using a lower-level language. The amount of abstraction provided defines how "high-level" a programming language is.

In computer science, imperative programming is a programming paradigm of software that uses statements that change a program's state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program consists of commands for the computer to perform. Imperative programming focuses on describing how a program operates step by step, rather than on high-level descriptions of its expected results.

ALGOL W is a programming language. It is based on a proposal for ALGOL X by Niklaus Wirth and Tony Hoare as a successor to ALGOL 60. ALGOL W is a relatively simple upgrade of the original ALGOL 60, adding string, bitstring, complex number and reference to record data types and call-by-result passing of parameters, introducing the while statement, replacing switch with the case statement, and generally tightening up the language.

In computer programming, a block or code block or block of code is a lexical structure of source code which is grouped together. Blocks consist of one or more declarations and statements. A programming language that permits the creation of blocks, including blocks nested within other blocks, is called a block-structured programming language. Blocks are fundamental to structured programming, where control structures are formed from blocks.

<span class="mw-page-title-main">Conditional (computer programming)</span> Control flow statement that executes code according to some condition(s)

In computer science, conditionals are programming language commands for handling decisions. Specifically, conditionals perform different computations or actions depending on whether a programmer-defined Boolean condition evaluates to true or false. In terms of control flow, the decision is always achieved by selectively altering the control flow based on some condition . Although dynamic dispatch is not usually classified as a conditional construct, it is another way to select between alternatives at runtime. Conditional statements are the checkpoints in the programme that determines behaviour according to situation.

<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.

ALGOL 60 is a member of the ALGOL family of computer programming languages. It followed on from ALGOL 58 which had introduced code blocks and the begin and end pairs for delimiting them, representing a key advance in the rise of structured programming. ALGOL 60 was one of the first languages implementing function definitions. ALGOL 60 function definitions could be nested within one another, with lexical scope. It gave rise to many other languages, including CPL, PL/I, Simula, BCPL, B, Pascal, and C. Practically every computer of the era had a systems programming language based on ALGOL 60 concepts.

<span class="mw-page-title-main">History of programming languages</span> History of languages used to program computers

The history of programming languages spans from documentation of early mechanical computers to modern tools for software development. Early programming languages were highly specialized, relying on mathematical notation and similarly obscure syntax. Throughout the 20th century, research in compiler theory led to the creation of high-level programming languages, which use a more accessible syntax to communicate instructions.

In computer programming, a return statement causes execution to leave the current subroutine and resume at the point in the code immediately after the instruction which called the subroutine, known as its return address. The return address is saved by the calling routine, today usually on the process's call stack or in a register. Return statements in many programming languages allow a function to specify a return value to be passed back to the code that called the function.

In computer science, a relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities. These include numerical equality and inequalities.

In computer programming languages, a switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map.

Jensen's device is a computer programming technique that exploits call by name. It was devised by Danish computer scientist Jørn Jensen, who worked with Peter Naur at Regnecentralen. They worked on the GIER ALGOL compiler, one of the earliest correct implementations of ALGOL 60. ALGOL 60 used call by name. During his Turing Award speech, Naur mentions his work with Jensen on GIER ALGOL.

Programming languages are used for controlling the behavior of a machine. Like natural languages, programming languages follow rules for syntax and semantics.

The following outline is provided as an overview of and topical guide to computer programming:

This comparison of programming languages compares the features of language syntax (format) for over 50 computer programming languages.

This article compares a large number of programming languages by tabulating their data types, their expression, statement, and declaration syntax, and some common operating-system interfaces.

<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.

References

  1. "statement". webopedia. September 1996. Retrieved 2015-03-03.
  2. Backus, J.W.; Bauer, F.L.; Green, J.; Katz, C.; McCarthy, J.; Naur, P.; Perlis, A.J.; Rutishauser, H.; Samuelson, K.; Vauquois, B.; Wegstein, J.H.; van Wijngaarden, A.; Woodger, M. Naur, Peter (ed.). "Revised Report on the Algorithmic Language Algol 60". mass:werk. Section "4.1". Retrieved January 23, 2021.
  3. Backus, J.W.; Bauer, F.L.; Green, J.; Katz, C.; McCarthy, J.; Naur, P.; Perlis, A.J.; Rutishauser, H.; Samuelson, K.; Vauquois, B.; Wegstein, J.H.; van Wijngaarden, A.; Woodger, M. Naur, Peter (ed.). "Revised Report on the Algorithmic Language Algol 60". mass:werk. Section "1.1". Retrieved January 23, 2021.
  4. "FORTRAN" (PDF). United States of America Standards Institute. 1966. Retrieved February 19, 2021 via WG5 Fortran Standards.
  5. "Working draft J3/04-007" (PDF). J3 Fortran. May 10, 2004. Retrieved February 19, 2021.
  6. "ASCII COBOL Programming Reference Manual" (PDF). unisys. June 2010. Retrieved January 23, 2021.
  7. Jensen, Kathleen; Wirth, Niklaus (1974). Goos, G.; Hartmanis, J. (eds.). "PASCAL User Manual and Report" (PDF). Lecture Notes in Computer Science. "Appendix D". Retrieved February 19, 2021.
  8. Knuth, D. E. (Jul 1967). "The Remaining Trouble Spots in Algol 60" (PDF). The ALGOL Family. Retrieved February 24, 2021.
  9. "ISO/IEC 9899:1999 (E)" (PDF). ISO/IEC. Archived (PDF) from the original on Feb 7, 2024.
  10. "7. Simple statements". Python 3.10.8 documentation.