s LearnSqueak.com

ABSOLUTE BEGINNER

Your first between: ask whether a number fits

Check a number against two limits in five tiny visual Smalltalk steps. The ends count too.

5 minutes · 5 small steps · Squeak 6.1

STEP 1

Ask one clear question

Open a Workspace from the World menu: open… then workspace. Type this line, select it all, and choose Print it. between:and: asks whether a number is inside two limits.

Select the whole expression, then Print it
7 between: 1 and: 10
NATIVE SQUEAK CAPTURE
Native Squeak between:and: checkpoint 1
Actual native Squeak Workspace Print it result.
You should see

true means yes: 7 is between 1 and 10.

STEP 2

Include the ends

The lower and upper limits are included. You do not need to be strictly inside.

Select the whole expression, then Print it
10 between: 1 and: 10
NATIVE SQUEAK CAPTURE
Native Squeak between:and: checkpoint 2
Actual native Squeak Workspace Print it result.
You should see

The answer is true. Change 10 at the start to 1: that end counts too.

STEP 3

Spot an outside value

A number below the lower limit or above the upper limit answers false.

Select the whole expression, then Print it
11 between: 1 and: 10
NATIVE SQUEAK CAPTURE
Native Squeak between:and: checkpoint 3
Actual native Squeak Workspace Print it result.
You should see

false means no. Try 0 next; it is outside too.

STEP 4

Use a variable

A variable gives a value a name. Paste this complete example, including the vertical bars. The last line asks about the named value.

Select the whole expression, then Print it
| temperature |
temperature := 18.
temperature between: 15 and: 22
NATIVE SQUEAK CAPTURE
Native Squeak between:and: checkpoint 4
Actual native Squeak Workspace Print it result.
You should see

18 fits the interval. Try 25 instead and Print it again.

STEP 5

Keep the limits in order

Write the smaller limit first and the larger one second. This message does not swap them for you.

Select the whole expression, then Print it
7 between: 10 and: 1
NATIVE SQUEAK CAPTURE
Native Squeak between:and: checkpoint 5
Actual native Squeak Workspace Print it result.
You should see

false. Fix the limits to 1 and 10 to get true again.

YOU DID IT

A small step. A real result.

between:and: answers true when the receiver is inside the interval, including its endpoints. Put the lower limit first. Outside values answer false.

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

Download the five examples; copy one at a time

Your first list · More Smalltalk messages