• Keine Ergebnisse gefunden

REPETITIVE STATEMENTS

Im Dokument Alpha PASCAL (Seite 127-132)

BEGIN { MAX }

9.8 REPETITIVE STATEMENTS

It is often the case that one section of a program must be performed repetitively, based on a certain condition. AlphaPascal provides a number of repetitive statements: WHILE-DO, REPEAT-UNTIL, and FOR-DO. It is important that you decide which of these statements is exactly correct for your application, since each differs somewhat in the way that it handles final values.

9.8.1 WHILE-DO

The WHILE-DO statement takes the form:

WHILE Boolean expression DO statement

where the Boolean expression evaLuates to a TRUE or FALSE, and the statement may consist of a compound statement. For exampLe:

PROGRAM;

VAR Counter, Number, Average, Sum REAL;

BEGIN { Main Program}

Number := 1 { InitiaLize Number to > O. } Average := Counter := 0;

WHILE Number > 0 DO BEGIN

WRITELN('Average: ',Average);

Counter += 1;

WRITE('Enter number: I ) ; READLN(Number);

Sum += Number;

Average := Sum/Counter;

END;

END { Main Program }.

In effect, you tell Pascal, "While the following condition is TRUE, execute the following statements." As soon as the condition becomes FALSE, the program finishes executing the entire WHILE loop, and then goes on to the next program statement. It is possible that a WHILE Loop wiLL never be executed if the initial condition is not true and never becomes true.

9.8.2 REPEAT-UNTIL

The REPEAT-UNTIL statement takes this form:

REPEAT statement-list UNTIL Boolean expression

where statement-List may be series of statements separated by semicoLons, and expression evaLuates to TRUE or FALSE. For exampLe:

PROGRAM;

VAR Number Error

INTEGER;

BOOLEAN;

BEGIN { Main program } Error := FALSE;

REPEAT

WRITE('Enter an integer divisibLe by 3: I);

READLN(Number);

IF (Number MOD 3)

=

0 THEN

WRITELN(ICorrect. Try another.l ) ELSE Error := TRUE UNTIL Error

WRITELN(IIncorrect. End of exercise.l ) END { Main Program }.

Because the REPEAT-UNTIL keywords appear at the beginning and end of the Loop (making it cLear where the beginning and end of the Loop are), we do not have to incLude the BEGIN-END keywords after the REPEAT keyword (however, you may do so if you wish). A REPEAT Loop wiLL aLways be executed at Least once.

9.8.3 FOR-DO

The FOR-DO statement allows you to execute a given statement or group of statements a specific number of times. A FOR-DO loop is executed for every vaLue of the "control variable" from some starting vaLue up to and including some terminaL value. A control variable must not be of type REAL. The FOR-DO statement takes this form:

FOR Variable-identifier := expression TO expression DO statement For example:

PROGRAM;

VAR Counter: INTEGER;

BEGIN { Main Program}

WRITELN('The square roots of the integers 1 to 10 are :1);

WRITELN;

FOR Counter := 1 TO 10 DO WRITELN(ISquare root: I,SQRT(Counter»

END { Main Program }.

Each time the statement incremented by one.

integers from 1 to 10.

after the DO The program

keyword is executed, Counter is above prints the square roots of the

A variant of the FOR~DO Loop exists that aLLows you to decrement the controL variabLe. It takes the form:

FOR VariabLe-identifier := expression DOWNTO expression DO statement

Each time the statement after the DO keyword is executed, the controL variabLe is decremented by one. Note that it is possibLe that a FOR-DO Loop may not be executed at aLL, if the initiaL and terminaL vaLues of the controL variabLe are not in the proper range. (For exampLe, the statement FOR I := 5 TO 1 ••• wiLL not be executed, but FOR I := 5 DOWNTO 1 ••• wiLL be executed.)

9.9 WITH-DO

The WITH-DO statement were simpLe variabLes.

aLLows you to access fieLds of a record as if they The WITH-DO statement takes the form:

WITH VariabLe-identifier1 ••• ,VariabLe-identifierN DO statement The WITH-DO statement simpLy gives you a shorthand way of accessing record fieLds without specifying the name of the record structure for each access.

(See Section 7.2.7, "RECORDS," for information on records.) For exampLe, suppose you have a record made up of the foLLowing fieLds:

CarInfo.ModeL CarInfo.Year CarInfo.CoLor

CarInfo.SeriaLNumber

You have 100 cars on your car Lot, and you want to know how many of them are red. The records may be set up this way:

TYPE CarInfo

=

RECORD

VAR

ModeL STRING[3];

Year INTEGER;

CoLor STRING[3];

SeriaLNumber INTEGER;

END { record };

Counter,CarNumber Carlot

INTEGER;

ARRAY [1 •• 100] .Q£. CarInfo;

Now you can process them. Without using a WITH-DO statement, you wouLd have to do something Like this:

)

Counter := 0;

FOR CarNumber := 1 TO 100 DO BEGIN

IF (Carlot[CarNumberJ.ModeL='X20') --AND (Carlot[CarNumberJ.CoLor='red')

--rHEN Counter += 1;

WRITElN('Number of red X20s is: ',Counter) END;

A more convenient way is to use the WITH-DO statement:

Counter : = 0;

FOR CarNumber := 1 TO 100 DO BEGIN

WITH Carlot[CarNumberJ DO

----IF (ModeL='X20') AND~CoLor='red') THEN Counter += 1;

WRITElN('Number of rea-x20s is: ',Counterr-END;

By specifying more than one variabLe-identifier, you can use the WITH-DO statement to access fieLds that occur within record fieLds. For exampLe, to access data in the record Carlot.Make.ModeL, you could write something Like this:

WITH Carlot,Make DO

~deL := 'HatchBack';

This is equivaLent to:

WITH Carlot DO

- -

WITH Make DO

--~deL :=-rHatchBack';

INPUT/OUTPUT FUNCTIONS AND PROCEDURES

The functions and procedures discussed in this chapter are used to transfer data between your programs and the users of those programs, and between programs and fiLes. The routines we describe in the first part of the chapter, "Basic Functions and Procedures," are routines that users of standard PascaL wiLL probabLy be famiLiar with. The Last part of the chapter, "SpeciaL Functions and Procedures for FiLe I/O," contains descriptions of functions and procedures that are particuLarLy for use with the AMOS file structure.

NOTE: You wilL notice that we use the term "fiLe-identifier" when discussing a fi Le variabLe, rather than the, simpLe term "fi Le" (sometimes used by other PascaL books). This is to heLp avoid confusing the fiLe-identifier with the

"file specification," which is the specification of the actual AMOS disk fiLe that is associated with the fiLe variabLe. Using an AMOS fiLe requires that you first declare the fiLe-identifier and then associate it with the fi Le specification of an AMOS disk fi Le. See Section 10.2, "Special Functions and Procedures for File I/O," for more information on using AMOS disk fi Les, especially Section 10.2.12, "OPEN.")

Im Dokument Alpha PASCAL (Seite 127-132)