Last Updated:

Transfer of control procedures In Pascal

There are several standard procedures that change the sequence of execution of operators:

  • break— completes the execution of the loop within which it is recorded;
  • continue—Moves to the next iteration of the loop.
  • exit— exits the program or subroutine within which it is recorded;
  • halt— Terminates the program immediately.

In addition, the goto transition operator is used to transfer control.

 

control procedures In Pascal

Let's consider an example of the application of the control transfer procedure.

Example: A program for calculating the value of the functionCh x (hyperbolic cosine) using a Taylor infinite series with precision ε according to the formula:

Transfer of control procedures

This series converges on any argument values. When the number nis increased, the module of the member of the series Cn tends to zero. With some n, the inequality | Cn | =>ε stops performing and calculations stop.

The general algorithm is simple: set the initial value of the sum of the series, and then repeatedly calculate the next term of the series and add it to the previously found sum until the absolute value of the next term of the series becomes less than the specified accuracy.

Until the program is executed, it is impossible to predict how many members of the series will need to be summed up. In a cycle of this kind, there is a danger that it will never end. Therefore, for the reliability of the program, it is necessary to provide an emergency exit from the cycle with the printing of a warning message when a certain maximum allowable number of iterations is reached.

The direct calculation of a member of a series according to the above general formula, when x is raised to a power, a factorial is calculated, and then the numerator is divided by a denominator, has two drawbacks that make this method unsuitable: a large error of calculations and their low efficiency. When calculating the next term of the series, the previous one is already known, so you should use the recurrent formula for obtaining the next term of the series through the previous Cn + 1 = Cn * T, where T is some factor. Substituting Cn and Cn+1 into this formula, we get an expression to compute T:

Transfer of control procedures

The text of the program with comments is given in (example 2.4)..

program ch;

const MaxIter = 500; { maximum number of iterations }

var x, eps : double; { argument and precision }

c, y : double; { series member and its sum }

n : integer; { row member number }

done : boolean; { accuracy achievement sign }

begin

writeln('Enter argument and precision:');

readln(x, eps);

done := true;

c := 1; y := c; { first term of the series and the beginning value of the sum }

n := 0;

while abs(c) > eps do begin

c := c * sqr(x) /(2 * n + 1)/(2 * n + 2); { next member of the series }

y := y + c; { adding a member of a series to the sum }

inc(n);

if n > MaxIter then begin { emergency loopback }

writeln('The series diverges!');

done := false; break

end

end;

if done then

writeln('For the ', x, ' function value: ', y, #13#10,

'calculated with precision', eps, 'for ', n, 'iterations');

readln;

end.