






Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007
Sp2000 Midterm Review: Reading Code
Review at Midterm Review - Sp2000.
Michael Emard and Michelle Burnett bring you the following answer:
(a) abcdefghijkl
(b) four times
(c) nine times (Todd Matthews says it is ten times)
i think:
hello is printed indefinitely. the variable 'test' is assigned a value which is NOT re-evaluated within the loop. thus it is always true (since 1 is less than 10) and the loop never terminates. — oh, easy moops :)
Corrected by Jim Holland
a) abcdefghi,jkl
b) 5 times
Why five times? And how would you fix the loop to get it to end (yes, it's infinite). Mark Guzdial
Five times because the "size" of anArray is 5. There are four string elements and one $, element.
To fix the loop to get it to end, simply make test hold a block object instead of the result of a single conditional test. Then, send the message whileTrue: [] to test. (Basically, change () to [] when test is created, and remove the [] from when test is referred to later. Obviously, you could put the later reference to test inside (). Here's code that shows 'hello' nine times:
| i test |
i := 1.
test := [i < 10].
test whileTrue: [Transcript show: 'hello'.
i := i + 1.].
Matthew Wolenetz
Link to this Page