Statements |
3 |
![]() |
Standard Statements
Pascal supports all standard statements. Pascal also supports extensions to:
assert |
next |
case |
otherwise |
exit |
return |
goto |
with |
Statements Specific to Pascal
Table 3-1 summarizes the nonstandard Pascal statements and standard statements with nonstandard features. Detailed descriptions and examples of each statement follow.
Table 3-1 Nonstandard Pascal Statements
assert Statement
The assert statement causes a boolean expression to be evaluated each time the statement is executed.
-C option, which enables runtime tests. Otherwise, the compiler treats assert as a comment.
assert((num > 0) and (num <= MAX_STUDENTS)); for i := 1 to num do begin write('Enter grade for student ', i: 3, ': '); readln(grades[i]) end. |
The following if statement is equivalent to the assert statement in the preceding program:
if (num > 0) and (num <= MAX_STUDENTS) then begin for i := 1 to num do begin write('Enter grade for student ', i: 3, ': '); end end else begin writeln('Error message.'); halt end |
case Statement
Pascal supports the standard case statement with extensions for an otherwise clause and ranges of constants.
exit Statement
The exit statement, which you can use in a for, while, or repeat loop, transfers program control to the first statement after the end of the current loop.
The commands to compile and execute exit.p. This example shows the program output when you input the number 13.
|
hostname% pc exit.p hostname% a.out Enter a number: 13 Number WAS found: 13 |
goto Statement
Pascal supports the standard format of the goto statement with two extensions.
Identifiers as Targets (Screen 1 of 2)
otherwise writeln('Unknown grade: ', grades[i]); points := 0.0 end end; sum := sum + points; skip_subtotal: end; writeln('GPA for all students is ', sum / num: 6: 2, '.') end. { goto_example } |
Identifiers as Targets (Screen 2 of 2)
next Statement
The next statement, which you can only use in a for, while, or repeat loop, causes the program to skip to the next iteration of the current loop, thus skipping the rest of the statements in the loop.
The next Statement (Screen 1 of 2)
The next Statement (Screen 2 of 2)
otherwise Statement
The otherwise statement is a Pascal extension to the standard Pascal case statement. If specified, otherwise must be at the end of the case statement. See the listing in "case Statement" on page 51 for additional information.
return Statement
The return statement prematurely ends a procedure or a function.
with Statement
Pascal supports the standard with statement plus an alternative format.
The commands to compile and execute with.p
|
hostname% pc with.p hostname% a.out The new patient is Abby Smith. The old patient is Henry Brown. |