Statement (programming)
From Wikipedia, the free encyclopedia
In computer programming a statement can be thought of as the smallest standalone element of an imperative programming language. A program is formed by a sequence of one or more statements. A statement will have internal components (eg, expressions).
Many languages (eg, 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 |
[edit] Kinds of statements
The following are the major generic kinds of statements with examples in typical imperative languages:
[edit] Definitions and declarations
- definition:
TYPE SALARY = INTEGER
- declaration:
VAR A:INTEGER
[edit] Simple statements
- assignment:
A := A + 1
- call:
CLEARSCREEN()
- return:
return 5;
- goto:
goto 1
- assertion:
assert(ptr != NULL);
[edit] Compound statements
- statement block:
begin WRITE('Number? '); READLN(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
[edit] 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'.
[edit] Programming languages
The syntax and semantics of statements is specified by the definition of the programming language. Most programming languages do not allow new statements to be created during program execution (Snobol 4 is a language that does), or existing statements to be modified (Lisp is well known for supporting self modifying code).