Information Science    
   
Table of contents
(Prev) State (computer science)Statement (logic) (Next)

Statement (computer science)

In computer programming a statement is the smallest standalone element of an imperative programming language. A program written in such a language is formed by a sequence of one or more statements. A statement will have internal components (e.g., expressions).

Many languages (e.g. C) make a distinction between statements and definitions, with a statement only containing executable code and a definition declaring an identifier. A distinction can also be made between simple and compound statements; the latter may contain statements as components.

Contents

Kinds of statements

The following are the major generic kinds of statements with examples in typical imperative languages:

Simple statements

Compound statements

  • block: begin integer NUMBER; WRITE('Number? '); READLN(NUMBER); A:= A*NUMBER end
  • if-statement: if A > 3 then WRITELN(A) else WRITELN("NOT YET"); end
  • switch-statement: switch (c) { case 'a': alert(); break; case 'q': quit(); break; }
  • while-loop: while NOT EOF DO begin READLN end
  • do-loop: do { computation(&i); } while (i < 10);
  • for-loop: for A:=1 to 10 do WRITELN(A) end

Syntax

The appearance of statements shapes the look of programs. Programming languages are characterized by the flavor of statements they use (e.g.: The curly brace language family). Many statements are introduced by identifiers like if, while or repeat. Often statement keywords are reserved such that they cannot be used as names of variables or functions. Imperative languages typically use special syntax for each statement, which looks quite different to function calls. Common methods to describe the syntax of statements are Backus–Naur Form and syntax diagrams.

Semantics

Semantically many statements differ from subroutine calls by their handling of parameters. Usually an actual subroutine parameter is evaluated once before the subroutine is called. This contrasts to many statement parameters which can be evaluated several times (e.g. the condition of a while loop) or not at all (e.g. the loop body of a while loop). Technically such statement parameters are closures which are executed when needed (see also lazy evaluation). When closure parameters are available for subroutines a statement like behaviour can be implemented by subroutines also (see Lisp). For languages without closure parameters the semantic description of a loop or conditional is usually beyond the capabilities of the language. Therefore standard documents often refer to semantic descriptions in natural language.

Expressions

In most languages, statements contrast with expressions in that statements do not return results and are executed solely for their side effects, while expressions always return a result and often do not have side effects at all. Among imperative programming languages, Algol 68 is one of the few in which a statement can return a result. In languages which mix imperative and functional styles, such as the Lisp family, the distinction between expressions and statements is not made: even expressions executed in sequential contexts solely for their side effects and whose return values are not used are considered 'expressions'. In purely functional programming, there are no statements; everything is an expression.

Extensibility

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

See also

References

  • [1]
(Prev) State (computer science)Statement (logic) (Next)