Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Syntax of the FOR loop using DOWNTO

FOR counter_variable := initial_value DOWNTO termination_value DO instruction
The same conditions apply to the use of DOWNTO in a FOR loop, but obviously, the initial_value should be greater than the termination_value, or the statement will not make much sense. In this case the compiler will just ignore the FOR loop, and that will leave you with a bug in your program, which may prove very difficult to spot. So be careful out there! The other option in controlling a FOR loop is to use ASCII characters. Any of the ASCII characters can be used to count in a FOR loop, as the next program shows.

Program 10

PROGRAM char_list(INPUT,OUTPUT); { program writes out list of ASCII characters } VAR I : CHAR; { for loop counter } BEGIN FOR I := '2' DOWNTO '%' DO WRITELN('The character is : ',I); END. { program char_list }


In this instance, '2' is a character. No calculations can be done with it. The output from this program will be a list of the ASCII characters between '2' and '%'. Notice that , in the source code the characters must be enclosed between single apostrophes. I have shown you that compound statements contained within control structures may be bracketed by the reserved words BEGIN and END. There is a fairly important point to make about this at this point. If the control structure is followed by a single statement then there is no need to use the reserved words BEGIN and END.
Compare,

WRITE('Enter the number : '); READLN(the_number); . . IF (the_number < 15) THEN count := count + 1; WRITELN('Count has increased by one');


with,

WRITE('Enter the number : '); READLN(the_number); . . IF (the_number < 15) THEN BEGIN count := count + 1; WRITELN('Count has increased by one'); END;


With the first piece of code, the line

WRITELN('Count has increased by one');


will always be executed, whether the_number is less than fifteen or not. In the second piece of code, the BEGIN and END tie the two lines between them into one compound statement which will then only be executed if the condition is true. You should really give this a try. Write a small test program (see program test_1) which contains a similar piece of code and try both versions, and see for yourself. This is really the best way to remember this rule.

6. Selection : CASE - OF

The CASE statement is an interesting variation on the selection process performed by the IF statement. It is a little more difficult to grasp, but it makes a whole bunch of IF - THEN - ELSE statements redundant, and makes for a much tidier program. A general rule used to decide whether to use a CASE statement is: if you need more than one IF, then use CASE. This next program, is a simple CASE statement demonstration program. The VAR date is called, the case index, and the integers 1 - 28 are called, labels. It should be fairly clear to see that, when date equals, any of the label, the course of action decided on by the CASE statement is to write out the message, associated with that label.

Program 11

PROGRAM date(INPUT,OUTPUT); { a simple date program using case statement } VAR date : INTEGER; BEGIN WRITE('Enter the date : '); READLN(date); CASE date OF 7, 14, 21, 28 : WRITELN('That day in February, 1994 is a Monday'); 1, 8, 15, 22 : WRITELN('That day in February, 1994 is a Tuesday'); 2, 9, 16, 23 : WRITELN('That day in February, 1994 is a Wednesday'); 3, 10, 17, 24 : WRITELN('That day in February, 1994 is a Thursday'); 4, 11, 18, 25 : WRITELN('That day in February, 1994 is a Friday'); 5, 12, 19, 26 : WRITELN('That day in February, 1994 is a Saturday'); 6, 13, 20, 27 : WRITELN('That day in February, 1994 is a Sunday'); END; { end of case statement } END. { program date }

Date: 2016-03-03; view: 635


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