Previous Next Contents Index Doc Set Home


Statements

3


This chapter describes Pascal statements in the following sections:

Standard Statements

page 47

Statements Specific to Pascal

page 47


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

Statement
Description

assert

Causes a boolean expression to be evaluated each time the statement is executed.

case

Accepts ranges of constants and an otherwise statement.

exit

Transfers program control to the first statement after the end of a for, while, or repeat loop.

goto

Accepts an identifier as the target of goto.

next

Causes the program to skip to the next iteration of the enclosing for, while, or repeat loop.

otherwise

An extension to the case statement. If the expression in a case statement does not match any of the case values, the compiler executes the statements under the otherwise statement.

return

Prematurely ends a procedure or a function.

with

An alternative format to the standard with statement.

assert Statement

The assert statement causes a boolean expression to be evaluated each time the statement is executed.

If your program contains an assert statement, you must compile it with the
-C option, which enables runtime tests. Otherwise, the compiler treats assert as a comment.

A runtime error results if the expression in the assert statement evaluates to false.

assert is a shorthand for using the if statement.

For example, the following code uses an assert statement to test whether num is greater than 0 and less than or equal to MAX_STUDENTS:

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, ': ');
readln(grades[i])
	end
end else begin
	writeln('Error message.');
	halt
end

The Pascal program, assert.p, which tests whether num is greater than 0 and less than or equal to MAX_STUDENTS before reading in the grades.

program assert_example;

const
    MAX_STUDENTS = 4;

var
    num: integer;
    i: integer;
    grades: array [1..MAX_STUDENTS] of char;

begin
    num := 6;
    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
end. { assert_example }

The commands to compile and execute assert.p without the -C option. The compiler treats assert as a comment.

hostname% pc assert.p
hostname% a.out
Enter grade for student   1:  A
Enter grade for student   2:  B
Enter grade for student   3:  C
Enter grade for student   4:  D
Enter grade for student   5:  F
Enter grade for student   6:  A

The result when you compile assert.p with the -C and -g option. The expression evaluates to false, so the compiler generates an error and halts.

hostname% pc -C assert.p
hostname% a.out

Assertion #1 failed
Trace/BPT trap (core dumped)

hostname% pc -C -g assert.p
hostname% a.out

Assertion #1 failed
Trace/BPT trap (core dumped)

case Statement

Pascal supports the standard case statement with extensions for an otherwise clause and ranges of constants.

If expression does not match any of the case values, the compiler executes the otherwise statement list. The reserved word otherwise is not a case label, so it is not followed by a colon (:). Also, the begin/end pair is optional in an otherwise statement.

You can use a range of constants instead of a single case value. A case range must be in ascending order.

The case statement operates differently when you compile your program with and without the -xl option. Without -xl, if the value of the expression is not equal to one of the case labels and you omit the otherwise statement, the program generates an error and halts.

If this situation occurs and you compile your program with -xl, the program falls through and does not generate an error; program execution continues with the statement immediately following the case statement.

The Pascal program, otherwise.p, which reads a character from the terminal. If the value of the character is not in the range 0 - 9, the compiler executes the statement in the otherwise statement. The program specifies all digits between 0 and 9 as the range '0'..'9'.

program otherwise_example(input, output);

{ This program demonstrates the otherwise
  clause and ranges in the case statement. }

var
    ch: char;

begin
    write('Please enter one character:  ');

{More than one character will produce erroneous results.}
    readln(ch);
    case ch of
      '0'..'9':
        writeln('The character you input is a digit.');
 otherwise
             writeln('The character you input is not a digit.')
    end
end. { otherwise_example }

The commands to compile and execute otherwise.p without
-xl. This example shows your output when you input the characters 3 and B.

hostname% pc otherwise.p
hostname% a.out
Please enter one character:  3
The character you input is a digit.
hostname% a.out
Please enter one character:  B
The character you input is not a digit.

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.

If used in a nested loop, exit only breaks out of the innermost loop.

You receive a compile-time error if you use this statement anywhere but in a for, while, or repeat loop.

The Pascal program, exit.p

program exit_example(input, output);

{ This program demonstrates the use of the
  exit statement in for, while, and repeat loops. }

const
    MAX = 10;

type
    integer_type = array [1..MAX] of integer16;

var
    i: integer16;
    i_array: integer_type := [1, 99, 13, 45, 69, 18, 32, -6];
    number: integer16;
    flag: boolean := false;

begin
    write('Enter a number:  ');
    readln(number);
    for i := 1 to MAX do begin
      if number = i_array[i] then begin
          flag := true;
          exit
      end
    end;
    if flag then 
      writeln('Number WAS found:  ', number)
    else 
      writeln('Number WAS NOT found:  ', number)
end. { exit_example }

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.

In Pascal, you can use an identifier as the target of a goto. Standard Pascal allows only integers as targets of gotos.

If you use a goto to jump out of the current block, Pascal closes all open files in the intervening blocks between the goto statement and the target of the goto.

The Pascal program, goto.p, which uses an identifier as a
target of a goto statement.

program goto_example;

{ This program uses an identifier as a target
  of a goto statement. }

label
    skip_subtotal;

const
    MAX_STUDENTS = 100;

var
    i: integer;
    grades: array [1..MAX_STUDENTS] of char;
    num: 1..MAX_STUDENTS;
    sum: real;
    points: real;

begin
    { Read in number of students and their grades. }
    write('Enter number of students:  ');
    readln(num);
    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;
    writeln;
 { Now calculate the average GPA for all students. }
    sum := 0;
    for i := 1 to num do begin
      if grades[i] = 'I' then begin
          goto skip_subtotal
      end else begin
          case grades[i] of
              'A': points := 4.0;
              'B': points := 3.0;
              'C': points := 2.0;
              'D': points := 1.0;
              'F': points := 0.0;

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)

You must compile goto.p with the -C option to execute the assert statement; otherwise, the compiler treats assert as a comment. This example returns the collective GPA of four students.

hostname% pc -C goto.p
hostname% a.out
Enter number of students:    4
Enter grade for student   1: B
Enter grade for student   2: B
Enter grade for student   3: C
Enter grade for student   4: A

GPA for all students is   3.00.

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 has the same effect as a goto to the end of the loop. If you use next in a for loop, Pascal increments the index variable as normal.

When you use next in a nested loop, it goes to the end of the innermost loop containing the next statement.

You receive a compile-time error if you use this statement anywhere but in a for, while, or repeat loop.

The Pascal program, next.p, which also uses the otherwise statement.

program next_example;

{ This program demonstrates the use of the next
  statement in for, while, and repeat loops. }

const
    MAX_STUDENTS = 100;

var
    i: integer;
    grades: array [1..MAX_STUDENTS] of char;
    num: 1..MAX_STUDENTS;
    sum: real;
    points: real;

begin
    { Read in number of students and their grades. }
    write('Enter number of students:  ');
    readln(num);
    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;
    writeln;

The next Statement (Screen 1 of 2)

    { Now calculate the average GPA for all students. }
    sum := 0;
    for i := 1 to num do begin
      if grades[i] = 'I' then begin
          next
      end else begin
          case grades[i] of
              'A': points := 4.0;
              'B': points := 3.0;
              'C': points := 2.0;
              'D': points := 1.0;
              'F': points := 0.0;
              otherwise
                  writeln('Unknown grade:  ', grades[i]);
                points := 0.0
     		end
      end;
      sum := sum + points
    end;
     writeln('GPA for all students is:  ', sum / num: 6: 2)
end. { next_example }

The next Statement (Screen 2 of 2)

You must compile next.p with the -C option to execute the assert statement; otherwise, the compiler treats assert as a comment. This example outputs the collective GPA of three students.

hostname% pc -C next.p
hostname% a.out
Enter number of students:  3
Enter grade for student   1:  A
Enter grade for student   2:  A
Enter grade for student   3:  C

GPA for all students is:    3.33

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.

Program control transfers to the calling routine. This has the same effect as a goto to the end of the routine. If used in the main program, return causes the program to terminate.

The Pascal program, return.p. The compiler prematurely returns from the procedure test if you input 1 or any integer from 4 through 99. The program also uses identifiers as the target of a goto.

program return_example;

{ This program demonstrates the use of the
  return statement in a procedure. }

var
    i: integer;

procedure test;
label
    error_negative_value, error_bad_values, error_value_too_big;
begin
    if i < 0 then 
      goto error_negative_value
    else if (i = 2) or (i = 3) then 
      goto error_bad_values
    else if i > 100 then 
      goto error_value_too_big;
    return;
error_negative_value:
    writeln('Value of i must be greater than 0.');
    return;
error_bad_values:
    writeln('Illegal value of i:  2 or 3.');
    return;
error_value_too_big:
    writeln('Value of i too large.');
    return
end; { test }

begin { main procedure }
    write('Enter value for i:  ');
    readln(i);
    test
end. { return_example }

The commands to compile and execute return.p

hostname% pc return.p
hostname% a.out
Enter value for i:  -1
Value of i must be greater than 0.
hostname% a.out
Enter value for i:  2
Illegal value of i:  2 or 3.
hostname% a.out
Enter value for i:  101
Value of i too large.
hostname% a.out
Enter value for i:  5

with Statement

Pascal supports the standard with statement plus an alternative format.

The following is an example that illustrates how to use a with statement in Pascal.

The Pascal program, with.p, which uses the alternate form of the with statement.

program with_example(output);

{ Sample program using the extension to the
  with statement. }

const
    MAX = 12;

type
    name_type = varying [MAX] of char;
    Patient = 
      record 
          LastName: name_type;
          FirstName: name_type;
          Sex: (Male, Female)
      end;

var
    new_patient: Patient;
    old_patient: Patient;

begin
    with new_patient: new, old_patient: old do begin
      new.LastName := 'Smith';
      new.FirstName := 'Abby';
      new.Sex := Female;

      old.LastName := 'Brown';
      old.FirstName := 'Henry';
      old.Sex := Male
    end;
    write('The new patient is ');
    write(new_patient.FirstName: 10);
    writeln(new_patient.LastName: 10, '.');
    write('The old patient is ');
    write(old_patient.FirstName: 10);
    writeln(old_patient.LastName: 10, '.')
end. { with_example }

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.


Previous Next Contents Index Doc Set Home