Statements
In programming a statement is the syntactic unit of an imperative programming lanugage that expresses some action to be carried out. A program written in such a language is formed by a sequence of one or more statements.
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
). Some examples
include (The following examples loosely follow C syntax):
- assignment:
variable = expression
- call:
subroutine name(parameters)
- assertion:
assert(relational expression)
- goto:
goto label
- return:
return value
- stop/halt/exit:
exit (expression)
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.
Many compound statements are loop commands or choice commands. Some examples include (The following examples loosely follow C syntax):
- count-controlled loop:
for (index = 1; index <= limit; index += 1) <statement>
- condition-controlled loop (test at start):
while (test) <statement>
- condition-controlled loop (test at end):
do <statement> while (test)
- if statement:
if (test) <statement>
- if statement (two-way choice):
if (test) <statement> else <statement>
- case/switch statement:
switch (c) {case 'a': <statement>; break; case 'b': quit(); break;}
- exception handling:
try {protected code} catch (exception specification) {exception handler}