Using Multiple If Statements

  1. Make a state of water () block that takes in a temperature and says whether water will be liquid, solid, or gas at that temperature. (You may decide which temperature measurements you will use: °F, °C, or °K.)
  2. Click on the image below to load stoplight costumes. Make a traffic signal () block that when given the words "red," "yellow," or "green," changes the sprite's costume to the correct color. For example, traffic signal ('green') should turn the light green. Decide what should happen if your traffic signal block receives something other than "red," "yellow," or "green" as input.
    Commands don't belong in Operators. :-)
    red, yellow, and green stoplights

Debugging code

  1. Load these three scripts.

    Only two of them work properly.

    Experiment with them and look at the code to see what they do. Explain what causes the buggy one to fail.

    When the sprite asks its question, you will see an "answer space" at the bottom of the stage. Answer space on stage

    Type your answer there and click the check box (or hit return).

    forever [ask (which direction do you want the sprite to move? Type up, down, right, or left and wait); if answer = up [go to x position, y position + 20]; if answer = down [go to x position y position - 20]; if answer = right [go to x position + 20 y position]; if answer = left [go to x position - 20 y position]; if answer = stop [stop all]; if not (answer = up) or (answer = down) or (answer = right) or (answer = left) or (answer = stop) [think (huh?!) for 2 seconds] ]

    Talk with Your Partner forever [ask (which direction do you want the sprite to move? Type up, down, right, or left and wait); if answer = up [go to x position, y position + 20] else if answer = down [go to x position y position - 20] else if answer = right [go to x position + 20 y position] else if answer = left [go to x position - 20 y position] else if answer = stop [stop all] else [think (huh?!) for 2 seconds] ]

    forever [ask (which direction do you want the sprite to move? Type up, down, right, or left and wait); if answer = up [go to x position, y position + 20]; if answer = down [go to x position y position - 20]; if answer = right [go to x position + 20 y position]; if answer = left [go to x position - 20 y position]; if answer = stop [stop all] else [think (huh?!) for 2 seconds] ]

Analyzing code

Load these blocks that use if and if else and try each one.

With your pair programmer figure out how each one does what it does.

Two of these scripts use the abs () block. In the Operators palette, this block shows up as sqrt (10).

To select abs (or any of several mathematical functions), click the drop-down menu.

The absolute value of a number is that number's distance from zero.

Number line from -5 to 5 showing that -4 and 4 are the same distance from 0.

As the number line shows, -3 and 3 are the same distance from 0, so abs(-3) and abs(3) are both 3. abs(-2.5) is 2.5.

In mathematical notation, we write abs(-3) as |-3|.

  1. Compare the first two mystery scripts:
    a mysterious block a mysterious block
  2. What does mystery3 do?
    a mysterious block
  1. How does mystery4 work?