s LearnSqueak.com

ABSOLUTE BEGINNER

Your first variable

Give a number a name, use it, and change its value.

5 minutes · 5 small steps · Squeak 6.1

STEP 1

Open a Workspace

In Squeak 6.1, click an empty part of the desktop. Choose open… then workspace.

A Workspace is a small window for trying code. A variable is a name that holds a value. Let's call ours score.

STEP 2

Give a number a name

Paste all three lines below. Drag across the WHOLE block to select it. Right-click the selection and choose print it (p). Some setups use middle-click for this menu.

The bars in | score | declare a temporary variable called score. The := puts 7 into it: type a colon followed by an equals sign. The period ends that instruction; the last line reads score.

Replace all text, select the whole block → print it (p)
| score |
score := 7.
score
NATIVE SQUEAK CAPTURE
Native Squeak Workspace assigning 7 to score and printing the selected answer 7.
score now holds 7, so the answer is 7.

STEP 3

Use the value

Replace ALL the Workspace text, including the old answer, with this block. Select the whole block and choose print it (p).

score * 2 multiplies its value by 2. The answer is 14. The temporary name lasts only while its block runs, so every example declares it again.

Replace all text, select the whole block → print it (p)
| score |
score := 7.
score * 2
NATIVE SQUEAK CAPTURE
Native Squeak Workspace multiplying score by 2 and printing the selected answer 14.
Using score in a calculation gives 14.

STEP 4

Change the value

Replace ALL the Workspace text, including the old answer, with this block. Select the whole block and choose print it (p).

score := score + 1 reads 7, adds 1, and puts 8 back into score. The last line shows the new value.

Replace all text, select the whole block → print it (p)
| score |
score := 7.
score := score + 1.
score
NATIVE SQUEAK CAPTURE
Native Squeak Workspace increasing score from 7 to 8 and printing the selected answer 8.
Assigning again changes score. Its new value is 8.

STEP 5

Choose your starting number

Replace ALL the Workspace text, including the old answer, with this block. Select the whole block and choose print it (p).

This time score starts at 20. Adding 1 changes it to 21. Try a different starting number and run the whole block again, without the old printed answer.

Replace all text, select the whole block → print it (p)
| score |
score := 20.
score := score + 1.
score
NATIVE SQUEAK CAPTURE
Native Squeak Workspace starting score at 20, increasing it by 1, and printing the selected answer 21.
The same instructions work with a different starting value: 20 becomes 21.

YOU DID IT

A name for a changing value.

Declare a temporary name between bars, give it a value with :=, then use that name in your code.

Each block works on its own. Keep replacing the old code and answer, then selecting the whole new block before Print it.

Optional code download: open it as text and copy one example into Workspace.

Variable examples (.st)

Try your first list · Explore Smalltalk