Professional Documents
Culture Documents
Week 10
Week 10
Introduction to Computer
Programming
Violetta Cavalli-Sforza
Week 10
TODAY
• Finish loops (or almost)
• Homework #5 was posted on Monday
Don’t wait to start on it!!!
Equivalent Statements
• FOR loop
FOR Count := StartValue TO StopValue DO
BEGIN ... END {FOR}
• Equivalent WHILE
Count := StartValue;
WHILE Count <= StopValue DO
BEGIN ... ; Count := Count + 1; END {WHILE}
• Equivalent REPEAT
Count := StartValue;
IF StartValue <= StopValue THEN
REPEAT
...
Count := Count + 1;
UNTIL Count > StopValue
Simple Applications of FOR Lopps
• Printing patterns
– E.g. diagonal band
– E.g. tree
DigitRead := false;
WHILE ( not DigitRead ) DO
BEGIN
write ('Enter another data
character >');
readln (NextChar);
DigitRead := ('0'<=NextChar) AND
(NextChar<='9')
END {WHILE}
Task: Dropping Objects
• Problem definition:
– Your physics professor wants you to write a
program that displays the effect of gravity on
a free-falling object. You should build a table
showing the height from a tower to every
second.
Analysis: Dropping Objects
• Observations:
– Assume
• time of free fall = t
– At t = 0.0 {initialization}
• object height = tower height
– While falling {updating-loop
body}
• object height =
tower height - distance traveled
– Free fall ends {testing -exit}
• when height <= 0.0
Program Segment:
Dropping Objects
writeln ('Time' :10:2, 'Height' 10:2);
{initialization}
T := 0.0;
Height := Tower;
WHILE Height > 0.0 DO
BEGIN {body}
writeln (T : 10:2, Height :10:2);
T := T + DeltaT;
Height := Tower - 0.5 * G * Sqr(T)
END {WHILE}
writeln;
writeln ('SPLATT!!!');
Example: Dropping Objects
• Observations
– Before the loop is entered, a message
displaying the heading is printed.
– The number of lines in the table depends on
the time interval between lines (DeltaT) and
the tower height (Tower).
– During each loop iteration, the current
elapsed time, t, and the current object height
are displayed and new values are assigned to
these variables.
Simple Loops Summary
• Use FOR loop as a counting loop. The loop control
variable must belong to an ordinal type (not real).
• REPEAT and WHILE-loops are conditional loops. Their
number of iteration depends on whether the value of a
condition is true or false.
• WHILE loop is repeated as long as its loop repetition
condition is true.
• REPEAT loop is repeated until its loop-termination
condition becomes true.
• Usually a REPEAT loop can be written as a WHILE loop
by complementing the condition.
• Not all WHILE loops can be written as REPEAT loops,
because REPEAT loops executes at least once, whereas
a WHILE loop body may be skipped entirely.
• WHILE loop is preferred over a REPEAT loop unless you
are certain that at least one loop iteration must always
be performed.
Exercises
• Write a while loop that counts the number of
digits entered in a stream of characters. Exit
when the character ‘X’ is entered. Make sure
you don’t count the X
• Write a while loop that reads negative or positive
numbers, keeping track of the largest in absolute
terms. Exit when a zero is entered.
• Write a while loop that reads lines of text, and
counts the characters entered. Exit when the
string ‘Quit’ is entered. (Don’t count the
characters in ‘Quit’).
Nested Loops
• It is possible to nest loops
• Each time the outer loop is repeated, the inner
loops are reentered, their loop-control
parameters are reevaluated, and all required
iterations are performed
• Very useful in processing multi-dimensional
arrays
Task: Draw Isosceles Triangle
*
***
*****
*******
*********
Program: Isosceles Triangle
PROGRAM Triangle;
{Draw an isosceles triangle}
CONST
NumLines = 5; {number of rows in
triangle}
Blank = ' '; {output character}
Star = '*'; {output character}
VAR
Row, {control for outer loop}
LeadBlanks, {control for first inner loop}
CountStars: integer; {control for 2nd inner loop}
{Continued}
Nested Loops Example
Isosceles Triangle
BEGIN {Triangle}
FOR Row := 1 TO NumLines DO {outer loop}
BEGIN {Draw each row}
{first inner loop}
FOR LeadBlanks := NumLines - Row DOWNTO 1 DO
write (Blank); {Print leading blanks}
{second inner loop}
FOR CountStars := 1 TO 2 * Row - 1 DO
write (Star); {Print asterisks}
writeln;
END {for Row}
end. {Triangle}