Ch 6. Statements [ HP Pascal/iX Reference Manual ] MPE/iX 5.0 Documentation
HP Pascal/iX Reference Manual
Chapter 6 Statements
A statement is a sequence of special symbols, reserved words, and
expressions that either performs a specific set of actions on a program's
data or controls program flow. Table 6-1 lists and describes
statements.
Table 6-1. HP Pascal Statements and Purposes
---------------------------------------------------------------------------
| | |
| Statement Type | Purpose |
| | |
---------------------------------------------------------------------------
| | |
| compound | Group statements |
| | |
---------------------------------------------------------------------------
| | |
| empty | Do nothing |
| | |
---------------------------------------------------------------------------
| | |
| assignment | Assign a value to a variable |
| | |
---------------------------------------------------------------------------
| | |
| procedure | Invoke a procedure |
| | |
---------------------------------------------------------------------------
| | |
| GOTO | Transfer control unconditionally |
| | |
---------------------------------------------------------------------------
| | |
| IF, CASE | Conditional selection |
| | |
---------------------------------------------------------------------------
| | |
| WHILE, REPEAT, FOR | Iterate a group of statements |
| | |
---------------------------------------------------------------------------
| | |
| WITH | Manipulate record fields |
| | |
---------------------------------------------------------------------------
The empty, assignment, procedure, and GOTO statements are commonly called
simple statements. The compound, IF, CASE, WHILE, REPEAT, FOR, and WITH
statements are referred to as structured statements because they
themselves may contain other statements.
Syntax
Statements:
Compound Statements
A compound statement is a sequence of statements bracketed by the
reserved words BEGIN and END. A semicolon (;) delimits one statement from
the next. Certain statements may alter the flow of execution in order to
achieve effects such as selection, iteration, or invocation of another
procedure or function. For instance, after the last statement in the
body of a routine has executed, control is returned to the point in the
program from which the routine is called. Note the use of non-local
GOTOs voids this statement. The program terminates after the last
statement is executed.
A compound statement has two primary uses. First, it defines the
statement part of a block and second, it groups a series of statements
into a single statement. A compound statement may also serve to
logically group a series of statements.
Note that compound statements are allowed, but are unnecessary in the
following cases:
* The statements between REPEAT and UNTIL.
* The statements between OTHERWISE and the end of the CASE
statement.
Example
PROCEDURE check_min;
BEGIN { This }
IF min > max THEN { compound }
BEGIN { Compound } { statement }
writeln('Min is wrong.'); { statement is } { is }
min := 0; { part of IF } { the }
END; { statement. } { procedure's }
END; { body. }
. . .
BEGIN { Nested compound statements }
IF part_to_start=part_1 THEN
BEGIN { for logically grouping statements. }
start_part_1;
finish_part_1;
{ empty statement here }
END
ELSE
BEGIN
start_part_2;
finish_part_2;
END;
END;
...
BEGIN .. END
BEGIN and END are reserved words that signify the beginning and ending of
a compound statement or block. BEGIN indicates to the compiler that a
compound statement or block has started, whereas END indicates that a
compound statement or block has terminated.
Syntax
Example
PROGRAM show_begin_end(input, output);
VAR
running : Boolean;
i, j : integer;
BEGIN {begin of program block}
i := 0;
j := 1;
running := true;
writeln('See Dick run.');
writeln('Run Dick run.');
IF running then
BEGIN {begin of compound statement}
i := i + 1;
j := j - 1;
END; {end of compound statement}
END. {end of program block}
Output:
See Dick run.
Run Dick run.
Empty Statements
The empty statement causes only the advancement of program flow to the
next statement. It is often used to indicate that nothing occurs. In
the example, no action occurs when i equals 2, 3, 4, 6, 7, 8, 9, or 10.
Example
CASE i OF
0 : start;
1 : proceed;
2..4 : ;
5 : report_error;
6..10: ;
11 : stop;
OTHERWISE fatal_error;
END;
IF i IN [2..4,6..10] THEN
{ do nothing }
ELSE
{ cases }
NOTE In the following example, the last semicolon is not required. Its
presence means that there is an empty statement before END. If the
semicolon were removed, there would not be an empty statement.
Empty statements do not affect the run-time speed of your program.
BEGIN
I:= J + 1;
K:= I + J;
END
Assignment
An assignment statement assigns a value to a variable access or a
function result. The assignment statement consists of a variable or
function identifier, an optional selector, a special symbol (:=), and an
expression that computes a value. The type of the expression must be
assignment compatible with the type of the receiving element.
The receiving element may be of any type except file, or a structured
type containing a file type component. An appropriate selector permits
assignment to a component of a structured variable or structured function
result.
NOTE An implementation may evaluate the variable access and the
expression in any order.
Syntax
Assignment_statement
Example
PROGRAM show_assign(input,output);
VAR
aaa: integer;
FUNCTION show_assign: integer;
TYPE
rec = RECORD
f: integer;
g: real;
END;
index = 1..3;
table = ARRAY [index] of integer;
CONST
ct = table [10, 20, 30];
cr = rec [f:2, g:3.0];
VAR
s: integer;
a: table;
i: index;
r: rec;
p1,
p: ^integer;
strg: string[10];
BEGIN { show_assign }
s:= 5; i:= 3;
a:= ct;
a [i] := s + 5;
r:= cr;
r.f:= 5;
new (p1);
p:= p1;
p^:= r.f - a [i];
strg:= 'Hi!';
show_assign := p^;
END; {show_assign}
BEGIN
aaa:= show_assign;
END.
CASE
The CASE statement selects a certain action based upon the value of an
ordinal expression. It consists of the reserved word CASE, a selector,
the reserved word OF, a list of case constants and statements, and the
reserved word END. Optionally, the reserved word OTHERWISE and a list of
statements may appear after the last constant and its statement.
The selector must be an ordinal expression in that it must return an
ordinal value. A case constant may be a literal, a constant identifier,
or a constant expression that is type compatible with the selector.
Subranges may also appear as case constants.
A case constant cannot appear more than once in a list of case constants.
Subranges used as case constants may not overlap other constants or
subranges. However, several constants may be associated with a
particular statement by listing them separated by commas.
Note that statements between OTHERWISE and END need not be bracketed with
BEGIN..END.
When the system executes a CASE statement, the following occurs:
1. It evaluates the selector.
2. If the value corresponds to a specified case constant, it executes
the statement associated with that constant. Control then passes
to the statement following the CASE statement.
3. If the value does not correspond to a specified case constant, it
executes the statements between OTHERWISE and END. Control then
passes to the statement after the CASE statement. The OTHERWISE
clause must be present or the selector must match any CASE label.
Syntax
Example
PROCEDURE scanner;
BEGIN
get_next_char;
CASE current_char OF
'a'..'z', { Subrange CASE label }
'A'..'Z':
scan_word;
'0'..'9':
scan_number;
OTHERWISE scan_special;
END;
END;
. . . .
FUNCTION octal_digit (d:digit): Boolean; { TYPE digit = 0..9 }
BEGIN
CASE d OF
0..7: octal_digit := true;
8..9: octal_digit := false;
END;
END;
. . . .
FUNCTION op { TYPE operators=(plus,minus,times,divide) }
(operator: operators;
operand1,
operand2: real)
: real;
BEGIN
CASE operator OF
plus: op := operand1 + operand2;
minus: op := operand1 - operand2;
times: op := operand1 * operand2;
divide: op := operand1 / operand2;
END;
END;
IF .. THEN
IF .. THEN .. ELSE
An IF statement specifies a statement the system executes, if a
particular condition is true. If the condition is false, then the system
doesn't execute that statement, and optionally, it executes another
statement starting after the ELSE.
The IF statement consists of the reserved word IF, a Boolean expression,
the reserved word THEN, a statement, and, optionally, the reserved word
ELSE and another statement. The statements after THEN or ELSE may be any
HP Pascal statements, including other IF statements or compound
statements. No semicolon separates the first statement and the reserved
word ELSE.
When an IF statement is executed, the Boolean expression is evaluated to
either true or false, and one of the following three actions is
performed:
* If the value is true, the statement following THEN is executed.
* If the value is false and ELSE is specified, the statement
following the ELSE is executed.
* If the value is false and no ELSE is specified, execution
continues with the statement following the IF statement.
The following IF statements are equivalent:
IF a = b THEN IF a = b THEN
IF c = d THEN BEGIN
a := c IF c = d THEN
ELSE a := c
a := e; ELSE
a := e;
END;
NOTE ELSE parts are always associated with the nearest preceding
unmatched IF statement.
A common use of the IF statement is to select an action from several
choices. This often appears in the following form:
IF e1 THEN
...
ELSE IF e2 THEN
...
ELSE IF e3 THEN
...
ELSE
...
This form is particularly useful to test for conditions involving real
numbers or string literals of more than one character, since these types
are not legal in CASE labels.
Syntax
If_statement
Example
PROGRAM show_if (output);
VAR
i,j : integer;
s : PACKED ARRAY [1..5] OF char;
found: Boolean;
BEGIN
.
.
IF i = 0 THEN writeln ('i = 0'); { IF with no ELSE. }
IF found THEN { IF with an ELSE part. }
writeln ('Found it')
ELSE
writeln ('Still looking');
.
.
IF i = j THEN { Select among different }
writeln ('i = j') { Boolean expressions. }
ELSE IF i < j THEN
writeln ('i < j')
ELSE { i > j }
writeln ('i > j');
.
.
IF s = 'RED' THEN { This IF statement }
i := 1 { cannot be rewritten as }
ELSE IF s = 'GREEN' THEN { a CASE statement. }
i := 2
ELSE IF s = 'BLUE' THEN
i := 3;
END.
FOR .. DO
The FOR statement executes a statement a predetermined number of times.
The FOR statement consists of the reserved word FOR, a control variable
initialized by an ordinal expression known as the initial value, either
the reserved word TO indicating an increment or the reserved word DOWNTO
indicating a decrement, another ordinal expression known as the final
value, the reserved word DO, and a statement.
The control variable is assigned each value of the range during the
corresponding iteration of the statement. It must be an ordinal variable
and may not be a component of a structured variable or a locally declared
procedure or function parameter. The control variable may be a local or
global variable. Non-local variables are not allowed. The initial and
final values are ordinal expressions that must be assignment compatible
with the control variable. After completion of the FOR statement, the
control variable is undefined.
Syntax
For_statement:
When the system executes a FOR statement, the following occurs:
1. It evaluates the initial and final values and assigns the initial
value to the control variable.
2. It executes the statement after DO.
3. It repeatedly tests the current value of the control variable and
final value for inequality, increments or decrements the control
variable, and executes the statement after DO.
In a FOR..TO construction, the system never executes the statement after
DO if the initial value is greater than the final value. In a
FOR..DOWNTO construction, the statement is never executed if the initial
value is less than the final value.
The FOR statement:
FOR control_var := initial TO final DO
statement
is equivalent to the statement:
BEGIN
temp1 := initial; {No evaluation order is required }
temp2 := final; {for temp1 and temp2. }
IF temp1 <= temp2 THEN
BEGIN
control_var := temp1;
WHILE control_var <= temp2 DO
BEGIN
statement;
control_var := succ(control_var); { increment }
END;
END
ELSE; { Don't execute the statement at all;}
END; { control_var is now undefined. }
The FOR statement:
FOR control_var := initial DOWNTO final DO
statement
is equivalent to the statement:
BEGIN
temp1 := initial; {No evaluation order is required }
temp2 := final; {for temp1 and temp2. }
IF temp1 >= temp2 THEN
BEGIN
control_var := temp1;
WHILE control_var >= temp2 DO
BEGIN
statement;
control_var := pred(control_var); { decrement }
END;
END
ELSE; { Don't execute the statement at all;}
END; { control_var is now undefined. }
In the statement after DO, it is an error if assignment is made to the
control variable. It cannot be used on the left-hand side of an
assignment statement, passed as a reference parameter or used as the
control variable of a second FOR statement nested within the first.
Furthermore, it may not appear as a parameter for the standard procedures
read or readln.
The system determines the range of values for the control variable by
evaluating the two ordinal expressions once, and only once, before making
any assignment to the control variable. So the statement sequence:
i := 5;
FOR i := pred(i) TO succ(i) DO writeln('i=',i:1);
writes:
i=4
i=5
i=6
instead of:
i=4
i=5
Example
{ VAR color: (red, green, blue, yellow); }
FOR color := red TO blue DO
writeln ('Color is ', color);
.
.
FOR i := 10 DOWNTO 0 DO
writeln (i);
writeln ('Blast Off');
.
.
FOR i := (a[j] * 15) TO (f(x) DIV 40) DO
IF odd(i) THEN
x[i] := cos(i)
ELSE
x[i] := sin(i);
REPEAT .. UNTIL
A REPEAT statement executes a statement or group of statements repeatedly
until a Boolean expression is true. It consists of the reserved word
REPEAT, one or more statements, the reserved word UNTIL, and a Boolean
expression (the condition). The statements between REPEAT and UNTIL need
not be bracketed with BEGIN..END.
When the system executes a REPEAT statement, the following occurs:
1. It executes the statement sequence, and then evaluates the Boolean
expression.
2. If it is false, it executes the statement sequence and evaluates
the Boolean expression again.
3. If it is true, control passes to the statement after the
REPEAT...UNTIL statement.
The statement:
REPEAT
statement;
UNTIL condition
is equivalent to the following:
1: statement;
IF NOT condition THEN GOTO 1;
Usually the statement sequence modifies data at some point so that the
condition becomes true. Otherwise, the REPEAT statement loops forever.
Syntax
Repeat_statement:
Example
sum := 0;
count := 0;
REPEAT
writeln('Enter trial value, or "-1" to quit');
read (value);
sum := sum + value;
count := count + 1;
average := sum / count;
writeln ('value = ', value, ' average = ',average)
UNTIL (count >= 10) OR (value = -1);
.
.
REPEAT
writeln (real_array[index]);
index := index + 1;
UNTIL index > limit;
WHILE .. DO
The WHILE statement executes a statement repeatedly as long as a given
condition is true. The WHILE statement consists of the reserved word
WHILE, a Boolean expression (the condition), the reserved word DO, and a
statement.
When the system executes a WHILE statement, the following occurs:
1. It evaluates the condition.
2. If the condition is true, it executes the statement after DO, and
then re-evaluates the condition. When the condition becomes
false, execution resumes at the statement after the WHILE
statement.
3. If the condition is false at the beginning, the system never
executes the statement after DO.
The statement:
WHILE condition DO statement
is equivalent to:
1: IF condition THEN
BEGIN
statement;
GOTO 1;
END;
Usually a program modifies data at some point so that the condition
becomes false. Otherwise, the statement repeats indefinitely.
Syntax
While_statement
Example
WHILE index <= limit DO
BEGIN
writeln (real_array[index]);
index := index + 1;
END;
.
.
WHILE NOT eof (f) DO
BEGIN
read (f, ch);
writeln (ch);
END;
WITH .. DO
A WITH statement allows reference to record fields by field name alone.
A WITH statement consists of the reserved word WITH, one or more record
designators, the reserved word DO, and a statement. A record designator
may be a record identifier, a function call that returns a record, or a
selected record component.
The statement after DO may be a compound statement. In this statement,
reference to a record field contained in one of the designated records
can be made without mention of the record to which it belongs. The
appearance of a function reference as a record designator is an
invocation of the function. Note that a new value may not be assigned to
a field of a record constant or a field of a record returned by a
function.
When the program executes a WITH statement, the following occurs:
1. References to the record designators are evaluated.
2. The statement after the DO statement is executed.
The following statements are equivalent:
WITH rec DO BEGIN
BEGIN rec.field1 := e1;
field1 := e1; writeln(rec.field1
writeln(field1 * field2); * rec.field2);
END; END;
Because the program evaluates a reference to a record designator once and
only once before it executes the statement, the following statement
sequences are equivalent:
f designates a field in the example above.
i := 1;
WITH a[i] DO
BEGIN
writeln(f);
i:=2;
writeln(f)
END;
writeln(a[1].f);
writeln(a[1].f); { NOT writeln(a[2].f) }
That is, within the WITH statement, the implied value of a[i] is not
affected by the change to i.
Records with identical field names may appear in the same WITH statement.
The following interpretation resolves any ambiguity.
The statement:
WITH record1, record2, ..., recordn DO
BEGIN
statement;
END;
is equivalent to:
WITH record1 DO
BEGIN
WITH record2 DO
BEGIN
...
WITH recordn DO
BEGIN
statement;
END;
...
END;
END;
Therefore, if field f is a component of both record1 and record2, the
compiler interprets an unselected reference to f as a reference to
record2.f. The synonymous field in record1 can be accessed using normal
field selection; for example, record1.f.
This interpretation also means that if r and f are records, and f is a
field of r, the statement:
WITH r DO
BEGIN
WITH r.f DO
BEGIN
statement;
END;
END;
is equivalent to
WITH r,f DO
BEGIN
statement;
END;
If a local or global identifier has the same name as a field of a
designated record in a WITH statement, then the appearance of the
identifier in the statement after DO is always a reference to the record
field. The local or global identifier is inaccessible if it happens to
have the same name as the field name in the record.
Syntax
With_statement
Example
PROGRAM show_with;
TYPE
status = (married, widowed, divorced, single);
date = RECORD
month : (jan, feb, mar, apr, may, jun,
july, aug, sept, oct, nov, dec);
day : 1..31;
year : integer;
END;
person = RECORD
name : RECORD
first, last: string[10]
END;
ss : integer;
sex : (male, female);
birth : date;
ms : status;
salary : real
END;
VAR
employee : person;
BEGIN {show_with}
.
WITH employee, name, birth DO
BEGIN
last := 'Hacker';
first := 'Harry';
ss := 214748364;
sex := male;
month := feb;
day := 29;
year := 1952;
ms := single;
salary := 32767.00
END;
.
END. {show_with}
GOTO
A GOTO statement transfers control unconditionally to a statement marked
by a label. It consists of the reserved word GOTO and the specified
label.
The scope of labels is restricted. They may only mark statements
appearing in the executable portion of the block where they are declared.
They cannot mark statements in inner blocks. GOTO statements, however,
may appear in inner blocks and reference labels in an outer block.
Therefore, it is possible to jump out of a procedure or function, but not
into one.
A GOTO statement may not lead into a structured statement from outside
that statement or from another component statement of that statement.
For example, it is illegal to branch to the ELSE part of an IF statement
from either the THEN part, or from outside the IF statement. Note that a
GOTO statement that refers to a non-local label declared in an outer
routine, causes any local files to be closed.
Labels are numeric values in the range 0 through 9999.
NOTE The use of the non-local label form of GOTO may increase execution
time of the program.
Syntax
Goto_statement
Example
PROGRAM show_goto (output);
LABEL 500, 501;
TYPE
index = 1..10;
VAR
i: index;
target: integer;
a: ARRAY[index] OF integer;
PROCEDURE check;
VAR
answer: string [10];
BEGIN
.
{ ask user if OK to search }
IF answer= 'no' THEN GOTO 501; { jumping out of procedure }
.
END;
BEGIN { show_goto }
.
check;
.
FOR i := 1 TO 10 DO
IF target = a[i] THEN GOTO 500;
writeln (' Not found');
GOTO 501;
500:
writeln (' Found');
501:
END. { show_goto }
Procedures
A procedure statement transfers program control to the block of a
declared or standard procedure. After the procedure has executed,
control is returned to the statement following the procedure call. A
procedure statement consists of a procedure identifier and, if required,
a list of actual parameters in parentheses.
The procedure identifier must be the name of a standard procedure or a
procedure declared in a previous procedure declaration. If a procedure
declaration includes a formal parameter list, the procedure statement
must supply the actual parameters. The actual parameters must match the
formal parameters in number, type and order. There are four kinds of
parameters: value, reference, procedural, and functional.
Actual value parameters are expressions that must be assignment
compatible with the formal value parameters or, in the case of value
conformant array parameters, conformable with the conformant array
schema. Actual reference parameters are variables that must be type
identical with the formal reference parameters or, in the case of
reference conformant array parameters, conformable with the conformant
array schema. Components of a packed structure cannot appear as actual
procedural or functional para meters. Actual procedural or functional
parameters are the names of procedures or functions declared in the
program. Standard procedures or functions cannot be actual parameters to
procedures or functions.
If a procedure or function that was passed as an actual parameter
accesses any entity non-locally upon activation, then the entity accessed
is one that is accessible to the procedure or function when it is passed
as a parameter. For example, suppose Procedure A uses the non-local
variable x. If A is then passed as an actual parameter to Procedure B,
it is still able to use x, even if x is not otherwise accessible from B.
The formal parameters, if any, of an actual procedural or functional
parameter must be congruent with the formal parameters of the formal
procedural or functional parameter.
Syntax
Procedure_statement:
Example
PROGRAM show_pstate(output);
PROCEDURE wow;
BEGIN
writeln('wow');
END;
PROCEDURE bow;
BEGIN
write('bow-');
wow;
END;
PROCEDURE outer (a: integer;
procedure proc_parm);
PROCEDURE inner;
BEGIN
bow;
END;
BEGIN {outer}
writeln('Hi');
inner;
proc_parm;
END; {outer}
BEGIN { show_pstate }
outer(30, bow);
END. { show_pstate }
Output:
Hi
bow-wow
bow-wow
MPE/iX 5.0 Documentation