Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Syntax of the FOR loop using TO

FOR counter_variable := initial_value TO termination_value DO instruction(s)
Note: The first thing to follow the~ reserved word FOR must be a variable The function of this variable is to keep count of the number of loops that have been done. It is usual for these variables to be called I, J, K etc. In the first loop the counter_variable `takes the value' of the initial_value, this is usually set at 1. This value is then compared to the termination_value, if they are the same value the loop will exit, otherwise a loop will be made. The counter_variable will be incremented by 1, and the comparison done again. The last pass through the loop will happen when the counter_variable and the termination_value are equal. The initial_value can be any INTEGER or expression which yields an INTEGER. The same applies to the termination_value. After the loop exits, the program moves onto the next instruction. The crux of this is that if you have a piece of code like this:

FOR I := 1 TO 5 DO WRITELN('This is a for loop');


You will get the output:

This is a for loop This is a for loop This is a for loop This is a for loop This is a for loop


The FOR loop is known as a count controlled loop. This means that the loop exit condition is determined by a counter, and when that counter reaches a pre-determined value the looping terminates. Other loops such as the REPEAT - UNTIL loop may be either count controlled or condition controlled. This means that the loop exit condition can be either by an INTEGER variable reaching a certain value, (see program ascii), or by a condition being either true of false, (see program password).

Program 7

PROGRAM for_demo(INPUT,OUTPUT); { this program demonstrates the simple use of a FOR loop } VAR I : INTEGER; BEGIN FOR I := 1 TO 2 DO WRITELN('The value of I is ',I:0); END. { for_demo }


After you have run this you can change both or either the initial_value or the termination_value. Try changing the 2 to a 5. Run the program then change the 1 to a 2. This will then give you a screen output like:

The value of I is 2 The value of I is 3 The value of I is 4 The value of I is 5


If the two values are the same, or the first is greater than the second, the compiler will not produce code for the FOR loop, effectively ignoring that section of program. Next is a simple demonstration of a useful way to incorporate the FOR loop into a program. This program will prompt the user for an INTEGER, then it will write the multiplication table for that INTEGER. Remember the INTEGER could be anything up to and including MAXINT.

Program 8

PROGRAM times_table(INPUT,OUTPUT); { a simple multiplication table program which writes out the specified multiplication table on the screen } VAR I, { for loop counter } times : INTEGER; BEGIN WRITE('Please enter the times table you require : '); READLN(times); WRITELN('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'); FOR I := 1 TO 12 DO WRITELN(' ',times:2,' * ',I:2,' = ',times*I:2); END. { program times_table }


You could of course change either or both the initial value and the termination value in the FOR loop, to give yourself a different screen output. So if you changed the 12 to 50, and entered '3' at the prompt, the last few lines on the screen would read:



3 * 45 = 135 3 * 46 = 138 3 * 47 = 141 3 * 48 = 144 3 * 49 = 147 3 * 50 = 150


Notice that the result has a field width specifier of 2, but the actual result takes as much space as it requires. (Experiment!) Next is a program that incorporates both the FOR loop and the IF statement. The essence is that the program prompts the user for five numbers, then it returns the one with the lowest value.

Program 9

PROGRAM find_least(INPUT,OUTPUT); { program finds the lowest value from input } VAR I, { for loop counter } number, least : INTEGER; BEGIN least := MAXINT; { initialisation of least to the maximum possible integer value } FOR I := 1 TO 5 DO BEGIN WRITE('Enter any integer : '); READLN(number); IF (number < least) THEN least := number; END; WRITELN; WRITELN('The smallest integer was : ',least:0); END. { program find_least }


The zero, for the field width specifier, allows the result again to take only as much room on the screen as it requires. Now you should have a pretty good idea of how the FOR loop works. I will now go back again to some options of FOR loop syntax. The reserved word TO can be replaced by the reserved word DOWNTO.


Date: 2016-03-03; view: 634


<== previous page | next page ==>
Syntax of the REPEAT - UNTIL | Syntax of the FOR loop using DOWNTO
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.006 sec.)