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 - Vishal Patel

Summer 2001 Midterm Review

Reading Code

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

Because this loop takes each element in the array an and concatinates it to aString which is '' the Transcript will show: 'abcdefghi,jkl' (without the quotes of course).

The body of the loop gets executed a total of 5 times; 1 for 'abc, 2 for 'def', 3 for 'ghi', 4 for ',', and finally 5 for 'jkl'.

| i test |
i := 1.
test := (i < 10).
[test] whileTrue: [Transcript show: 'hello'.
i := i + 1.].

This will print the hello infinitly becuase test was intialized as true and it never gets checks to see if i 10. This is like writing [true] whileTrue:[...].

Links to this Page