View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Discussion 4 - Fisnik Shpuza

Reading Code Spring 2004

| anArray aString |
anArray := #('abc' 'def' 'ghi','jkl').
aString := ''.
anArray do: [:each |
aString := aString, each.].
Transcript show: aString.

a. What appears on the Transcript ?


Explanation: the program iterates through the array of strings, concatenating the present value of aString with the next element "each" in the array. After the iteration si done, the value of aString is printed as shown above.
The comma gets printed as an element of the array and does not serve as a concatenating parameter.

b. How many times did the do: loop get executed?

the loops executes 5 times, one for each element of the array (including the comma)

| 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 indefinitelly, making the word hello print for many many times. The following is a fix for this code if the intetion was to print the word only 9 times.
| test |
test := 1.
[test < 10] whileTrue: [Transcript show: 'hello'.
test = test -1.].

Links to this Page