Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007
Discussion 4 - Peter Kinnaird
Spring 2004 Midterm Review Reading Code
| anArray aString |
anArray := #('abc' 'def' 'ghi','jkl').
aString := ''.
anArray do: [:each |
aString := aString, each.].
Transcript show: aString.
a. What appears on the Transcript?
The Program iterates through the array, concatenating the value of aString with the next element in the array. Then aString gets printed. The comma is interpreted as another element in the array, rather than a concatenater.
b. How many times did the do: loop get executed?
The loops executes 5 times, oncee for each element in the array.
| i test |
i := 1.
test := (i < 10).
[test] whileTrue: [Transcript show: 'hello'.
i := i + 1.].
c. How many times does hello get printed in this example?
The loop will run until the computer runs out of memory, printing hello world each time it is run. The intent was probably to run it a limited number of times, say, 10 times. The following code would do that
| test |
test := 1.
[test < 10] whileTrue: [Transcript show: 'hello'.
test = test -1.].
Links to this Page