home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.barnyard.co.uk
/
2015.02.ftp.barnyard.co.uk.tar
/
ftp.barnyard.co.uk
/
cpm
/
walnut-creek-CDROM
/
ENTERPRS
/
CPM
/
UTILS
/
S
/
TPTUTOR1.ARC
/
TL10.TXT
< prev
Wrap
Text File
|
1985-06-07
|
6KB
|
248 lines
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 38
TURBO-LESSON 10: WHILE STATEMENT
OBJECTIVES - In lesson 10 you will learn about:
1. CONSTant declaration
2. WHILE statement
3. Delay timing loop
1. CONSTant declaration.
In the declaration section of the Pascal programs in previous
lessons, only VAR and PROGRAM have appeared.
CONST is used to declare constants and assign values to them.
The form is:
CONST Constant_Name_1 = Value_1;
Constant_Name_2 = Value_2;
.
.
.
Constant_Name_n = Value_n;
Some examples:
CONST PI = 3.14159;
Fahr_Freeze = 32;
Cels_Freeze = 0;
Message_1 = 'This is a string constant';
##### DO:
Run PROG10 a time or two to see what it does.
##### DO:
Edit the constant declaration, Wobble_Size = 5;
to Wobble_Size = 10;
Run the program. What effect did you see from the change?
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 39
##### DO:
Change the value of Left_Edge to 20 and run the program.
To understand how Left_Edge and Wobble_Size work, look at the
WriteLn statement:
WriteLn(': ':Left_Edge, First:Index, Last:2, Message);
(1) ': ' is printed in the right 2 columns of a field whose
width is determined by the value of Left_Edge.
(2) First initial is printed in the rightmost column of a field
of width, Index. Note that Index is changing in the FOR
loop, so the width of this field changes.
(3) Last initial is printed next in the rightmost column of a
2-character wide field.
(4) The string constant, Message, is printed immediately to the
right of Last initial.
##### DO:
Run the program several times with the values of the constants
reset to various values. (LIVE DANGEROUSLY - try some wild
values: 50 for Wobble_Size or 70 for Left_Edge. Your CRT won't
explode! At least mine hasn't - yet!)
2. WHILE statement.
The three Pascal REPETITION Structures are:
(1) WHILE
(2) REPEAT
(3) FOR
The form of the WHILE statement is:
WHILE condition DO statement;
Note that the statement is often a block statement and appears:
WHILE condition DO
BEGIN
Statement_1;
Statement_2;
.
.
.
Statement_n;
END;
The WHILE is executed "while" the condition is true.
Contrast the REPEAT which is executed while the condition is
false, UNTIL the condition becomes true.
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 40
##### DO:
Run PROG10 again, using 0 as the number "less than 8".
How many times was the loop executed?
You could substitute REPEAT for WHILE in PROG10.
##### DO:
Replace: WHILE Count > 0 DO
with: REPEAT
add: UNTIL Count = 0; after: END; {WHILE}
(Notice that you don't have to remove the BEGIN END when you
change the loop from WHILE to REPEAT. They are not needed in
the REPEAT statement, but a block statement is always acceptable
wherever a simple statement can be used.)
Run the program several times to verify that it still works the
same as with the WHILE loop.
##### DO:
Run the program with 0 for the "number less than 8".
How many times was the loop executed?
Notice that the WHILE loop may be executed 0 times, but the
REPEAT loop is always executed at least once, since the condition
is not checked until the end of the loop.
NOTE: YOU SHOULD BE ON THE LOOKOUT FOR SUCH CONTRASTS BETWEEN
"INTERCHANGEABLE" STATEMENTS. CHOOSING THE BEST STATEMENT
TO USE IN EACH CASE IS BASED ON THIS KIND OF AWARENESS.
##### DO:
Change the REPEAT loop back to the original WHILE loop.
(An easy way would be to load a fresh copy of the original
PROG10.)
Change the WHILE statement to accept only Last initials less than
'G'.
WHILE Last < 'G' DO
Run the program. How did it work?
The WHILE loop didn't terminate at all - you had to abort the
program, right? (If it is still executing, use Ctrl-C or Ctrl-
Scroll-Lock.)
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 41
One of the things needed in controlling loops: something in the
loop must be related to the condition.
In the original WHILE loop, the Count was being decreased in the
loop.
When the condition was changed to (Last < 'G'), the loop no
longer has any effect on the condition.
Either the loop will not be executed at all, if Last is = or >
'G', or it will execute indefinitely if Last is < 'G'.
##### DO:
Change the WHILE statement to:
WHILE (Last < 'G') AND (Count > 0) DO
Run the program several times using last initials before and
after G in the alphabet.
##### DO:
Run the program with last initial of 'a' (lower-case a).
How many times did the loop execute?
Why?
The ASCII value of 'G' is 71. The ASCII value of 'a' is 91. So
'a' is > 'G'. (More on how to handle this problem in a later
lesson.)
3. Delay timing loop.
You may have noticed the "wobble" proceeds faster in one
direction than the other. This is due to a timing delay in the
first WHILE loop.
FOR Delay := 1 to 500 DO; {Do nothing, except loop}
This looks a little strange? There should be a statement after
the DO?
Pascal has a "null" statement for just such times as this. When
a statement is required, but none is present, Pascal just
substitutes its "null" or "do-nothing" statement.
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 42
##### DO:
Change the delay in the loop from 500 to 2000 and run the
program.
##### DO:
Add a timing loop to the other WHILE loop and run the program.
How did it work?
You didn't forget to add BEGIN END when you added the delay
statement, did you?
One last little twist on timing loops:
##### DO:
Change the Wobble_Size to 10.
Change both delay statements to:
FOR Delay := 1 to 500 * Index DO;
Run the program. Note that delays don't have to be constant.
This delay varies as the FOR loop progresses!
NOTE: I HOPE YOU HAVE ENJOYED THESE FIRST 10 TURBO-LESSONS. YOUR
COMMENTS AND SUGGESTIONS WOULD BE WELCOME!