Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Syntax of the REPEAT - UNTIL

REPEAT statement(s) UNTIL boolean condition is TRUE
The statement could be a single statement or a series of statements. The REPEAT - UNTIL could be translated into the traffic light analogy as the body of the REPEAT being `repeat the question UNTIL the colour is 'orange' or 'green' and the crosswalk is clear'. The actual code would take the form:

Program 4

PROGRAM repeat_until(INPUT,OUTPUT); { a demonstration program for the repeat - until loop } VAR condition, colour : string[10]; BEGIN REPEAT WRITE('Enter light colour : '); READLN(colour); WRITE('Is crosswalk occupied or clear : '); READLN(condition); UNTIL ((colour = 'green') and (condition = 'clear')) or ((colour = 'orange') and (condition = 'clear')); WRITELN('You can go now'); END. { program repeat_until }


The use of the double parentheses (curved brackets) shows how difficult it can be to code a particular condition. One pair of conditions on either side of the or must be true, before the loop can be exited from. It could be either, but obviously not both. The next program is the one which was used to write out the ASCII table in the back of this book. Don't worry about the statements contained within the REPEAT - UNTIL loop, CHR is one of the standard Pascal functions which are covered in the next chapter.

Program 5

PROGRAM ascii(INPUT,OUTPUT); { the program which wrote out the ASCII table in the back of this book } VAR I : INTEGER; BEGIN I := 32; WRITE(' '); WRITELN('ASCII TABLE'); WRITELN; REPEAT WRITE(' ',I:3,' = ',CHR(I),' '); WRITE(I+1:3,' = ',CHR(I+1),' '); WRITE(I+2:3,' = ',CHR(I+2),' '); WRITE(I+3:3,' = ',CHR(I+3),' '); WRITELN(I+4:3,' = ',CHR(I+4),' '); I := I+5; WRITELN; UNTIL I = 127; END. { program ascii }


This is a pretty interesting program, by using the line

I := I+5;


I increment the the counter variable I by five on each pass through the loop. This means that, in the output printed on the screen, the first number on each line is five more than the first number on the previous line. This is a simple program, which can be used at the head of any program to provide some sort of password checking, for security purposes.

Program 6

PROGRAM password(INPUT,OUTPUT); { a password demonstration program } CONST good_password = '!42a'; { can be anything } VAR pass_word : string[4]; BEGIN WRITELN; WRITELN(' This is a restricted use machine'); WRITELN('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'); WRITELN; REPEAT WRITE('Please enter PASSWORD : '); READLN(pass_word); IF pass_word <> good_password THEN WRITELN('access DENIED, try again'); UNTIL pass_word = good_password; WRITELN; WRITELN('Password ACCEPTED, continue'); WRITELN; WRITELN(' Welcome to the World Bank Data Base'); WRITELN('-------------------------------------'); END. { program password }


Again, the reserved word CONST is used to initialise the identifier good_password. When the password is read in, it is put into the string pass_word. Then the program enters the REPEAT - UNTIL loop, and stays there, repeating itself until pass_word is the same as good_password. When this is done, the program moves onto the next stage. An ELSE is not required in this program, since the condition on the REPEAT - UNTIL loop takes care of all the other alternatives. The comparison operator '<>' in Pascal means does not equal. The REPEAT - UNTIL loops are used when the number of loops to be performed by the program is not known. If the number of loops is known, then a FOR loop should be used.



5. Looping : FOR - TO - DO

The FOR loop is another very powerful control structure in any programming language. What a FOR loop does is to execute a statement or group of statements a known number of times.


Date: 2016-03-03; view: 750


<== previous page | next page ==>
Comparison Operators | Syntax of the FOR loop using TO
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.005 sec.)