Control Structures There are four important types of non-procedural statements that affect the flow of control of a program: * the for-while loop * the if statement * the break statement * the next statement Each of these structures is explained here. There are some other statements that affect flow of control that have either already been introduced (done/quit/stop), or will be dealt with in the sections Procedures and Error Handling. For-While The two syntactic forms of the for-while statement are: for variable from start by change to finish while condition do statement-sequence od; and: for variable in expression while condition do statement-sequence od; The statement-sequence is repeated for each distinct value of "variable" while "condition" is true. The "for ...", "from ...", "by ...", "to ...", and "while ..." clauses are all optional. If "from start" or "by change" are not specified, they default to a value of 1. In the first case, "variable" is initially assigned the "start" value (so "variable" can be either a mathematical or programming variable prior to use in the loop). With each successive loop if "variable" is less than or equal to "finish" then "variable" is incremented by "change"; if the "while" condition is also true then the statement sequence is repeated. If either the "to" or "while" conditions are false, the loop terminates. In the second case, "variable" is successively assigned the component parts of the expression. This form of the for statement is just a more convenient way of expressing something like: for variable to nops(expression) while condition do statements that use op(variable, expression) od; Example: > printlevel := 0: > listsum := 0; > for i in [1,2,3,4,5] while i < 5 do > listsum := listsum + i; > od; > listsum; 10 > printlevel := 1: If The syntax of the if statement is: if condition then statement-sequence elif condition then statement-sequence ... elif condition then statement-sequence else statement-sequence fi; The elif and else portions are optional; there may be more than one elif clause in a row. The (boolean) condition in the if section is evaluated; the corresponding statement-sequence is evaluated if the condition is true. If the condition is false, control passes to the next elif or else clause. If no condition is true and there is no else clause, no action is taken. Example: > printlevel := 0: > for x by 3 to 20 do > if isprime(x) then > print(x, `is prime`); > elif type(x, odd) then > print(x, `is odd non-prime`); > else > print(x, `is even non-prime`); > fi; > od; 1, is odd non-prime 4, is even non-prime 7, is prime 10, is even non-prime 13, is prime 16, is even non-prime 19, is prime > printlevel := 1: Break The syntax of the break statement is simply: break; The break statement is used in for and while loops to terminate iteration of the loop altogether. Flow of control resumes with the statement following the body of the loop. Example: > printlevel := 0: > listsum := 0; > for i in [1,2,3,4,5] do if i >= 5 then break fi; > listsum := listsum + i; > od; > listsum; 10 > printlevel := 1: Next The syntax of the next statement is simply: next; The next statement is used in for and while loops to terminate the current iteration of the loop. Flow of control resumes with evaluation of the loop condition. Example: > printlevel := 0: > listsum := 0; > for i in [1,2,3,4,5] do > if type(i, odd) then > next; > fi; > listsum := listsum + i; > od; > listsum; 6 > printlevel := 1: