Language Syntax

Control Flow


SPP provides a full set of control flow constructs found in most modern languages such as conditional execution and repetition. Some of these have already appeared in examples. An SPP control flow construct executes a statement either conditionally or repetitively. The statement to be executed may be a simple one line statement, a compound statement enclosed in curly brackets or braces, or the null statement (; on a line by itself). An assortment of repetitive constructs are provided for convenience. The simplest constructs are while, which tests at the top of the loop, and repeat until, which tests at the bottom. The do construct is convenient for simple sequential operations on arrays. The most general repetitive construct is the for statement.

Two statements are provided to interrupt the flow of control through one of the repetitive constructs. The
break statement causes an immediate exit from the loop, by jumping to the statement following the loop. The next statement shifts control to the next iteration of a loop. If break and next are embedded in a conditional construct which is in turn embedded in a repetitive construct, it is the outer repetitive construct which will determine the point to which control is shifted. Note that formatting in the form of indentation and white space is not mandatory, but makes the code more readable and therefore easier to maintain.

if...else

The if and if else constructs are shown below. The expr part may be any boolean expression (see "Expressions" on page 31). The statement part may be a simple statement, compound statement enclosed in braces, or the null statement. The statement(s) will be executed if the expression resolves to true. Otherwise, it will fall through to the next block consisting of an else or else if.

	 if (expr)
		 [else if (expr)
		 statement]
		 [else (expr)
		 statement]
The control flow constructs may be nested indefinitely. There may be an if clause without an else or else if. There is no end if. A simple example of an if ... else ... else if is:

switch...case

The switch case construct evaluates an integer expression once, then branches to the matching case. Each case must be a unique integer constant. The maximum number of cases is limited only by table space within the compiler. A case may consist of a single integer constant, or a list of integer constants, separated by commas and terminated by the colon character:. The special case default, if included, is selected if the switch value does not match any of the other cases. If the switch value does not match any case, and there is no default case, control passes to the statement following the body of the switch statement. In every case, control passes to the statement following the switch. A break statement is not needed after each case (in contrast to the switch ... case statement in C). Each case of the switch statement may consist of an arbitrary number of statements, which do not have to be enclosed in braces. The body of the switch statement, however, must be enclosed in braces as shown below.

	 switch (expr) {
	 case list:
		           statements
	 [case list:
		           statements]
	 .
	 .
	 [default:
		           statements]
	 }
For example:

The switch construct will execute most efficiently if the cases form a monotonically increasing sequence without large gaps between the cases (i.e., case 1, case 2, case 3, etc.). Ideally, the cases should be defined parameters or character constants, rather than explicit numbers.

while

The while statement repetitively executes a statement or a block of statements as long as the specified condition expression is true. The condition is tested at the beginning of the loop, so it is possible for the statement not to be executed at all.

while (expr)
          statement

repeat...until

The
repeat construct repetitively executes a statement or a block of statements. The simpler form simply repeats forever. The statement block might include a break statement to terminate the loop.

The repeat...until form executes the statement as long as the logical expression in the until statement is false. The condition is tested at the end of the loop, so the statement will always be executed at least once.

repeat								repeat
    statement								    statement	
    until (expr)

for

The
for construct consists of an initialization part, a test part, a loop control part, and a statement to be executed. The initialization part consists of a statement which is executed once before entering the loop. The test part is a boolean expression, which is tested before each iteration of the loop. The loop control statement is executed after the last statement in the body of the for, before branching to the test at the beginning of the loop. When used in a for statement, next causes a branch to the loop control statement. The for construct is very general, because of the lack of restrictions on the type of initialization and loop control statements chosen. Any or all of the three parts of the for may be omitted, but the semicolon delimiters must be present. Only one statement is permitted for each control section, unlike C.

for (init; test; control)
    statement
For example:

This for statement searches the string str backwards until the character 'z' is encountered, or until the beginning of the string is reached. Note the use of the null statement (;) in the body of the for, since everything has already been done in the for itself. The strlen procedure is shown in a later example. Note that the above example may result in an error if the string is null, in which case ip = 0 and the test str[ip] != 'z' will try and access a character before the beginning of the string.

do

The do construct is a special case of the for construct. It is ideal for simple array operations, and since it is implemented with the Fortran DO statement, its use should result in particularly efficient code.

do lcp = initial, final [, step]
          statement
General expressions are permitted as loop control in the do statement but their result must be integers. The loop may run forward or backward, with any step size. Note that to operate backward, the step must be negative, and the initial value should be larger than the final value. The body of the do will not be executed if the initial value of the loop control parameter satisfies the termination condition. For example:

break

The break statement causes an immediate exit from a loop by jumping to the statement following the loop.

next

The next statement immediately shifts control to the next iteration of a loop.

return

The return statement assigns a value to a function or returns control to the calling procedure. This value is passed back to the calling procedure as the function value. The returned value is an expression which resolves to the declared data type of the function. For example:

goto

The goto statement unconditionally branches to another point in a procedure. The target statement is specified by a label, which is an integer constant on the beginning of a line, preceding an executable (unnumbered) statement. For example:

Alternately, the label may be assigned a symbolic value using the define statement. This permits more mnemonic labels.

The underscore at the end of the label (termin_ in the example above) is not required. but is a recommended convention to permit the labels to stand out as distinct from other identifiers.

if...else
switch...case
while
repeat...until
for
do
break
next
return
goto

Generated with CERN WebMaker