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 - Nazli Dokuzoglu


Discussion 4
Nazli Dokuzoglu
902163737
gth795k


Summer 2003 Midterm Review: Reading Code


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

1. What appears on the Transcript ?
In the code, anArray (with five elements: abc def ghi , jkl) and aString (empty string) is created.
anArray do: [block] evaluates the block for each element of the array. In the block, the created aString is concatenated with all the elements in anArray. After finishing the loop, when we code prints out aString to the transcript. So;
abcdefghi,jkl
appears on the Transcript after executing the code.

2. How many times did the do: loop get executed?
do: loop is executed for each element of the array. The array has 5 elements (abc def ghi , jkl) so the do: loop is executed for 5 times.

 | i test |
i := 1.
test := (i < 10).
[test] whileTrue: [Transcript show: 'hello'.
	i := i + 1.].
3. How many times does hello get printed in this example? This is an infinite loop, so it is executed infinitely many times. The test is assigned to (i < 10) when i was 1. test remains true although i is incremented in the code because test is not assigned to i < 10 for the new incremented value of i. So throughout the code, although i is incremented, test remains true causing an infinite loop. The condition of the whileTrue block ( [test] ) should be changed to [i < 10] to print hello for 9 times to the transcript.
| i |
i := 1.
[i < 10] whileTrue: [Transcript show: 'hello'.
	i := i + 1.].


Links to this Page