Page 1: What’s a Predicate?

Unit 2, Lab 3, Page 1

On this page, you will review predicates and build a few that you can use in other projects.

As you know, predicates are reporter blocks (functions) that always report a Boolean value (they report only the values true or false). In Snap!, predicates are represented by hexagonal blocks. They compute the condition used by conditionals (such as if, if else, or repeat until) to decide when to do something.

So, the input type of a conditional is Booleans, and the output type of a predicate is also Booleans.

: Domain and Range

  • The input type (often called domain by programmers) of a function is the type of data that it accepts as input.
  • The output type (often called range) of a function is the type of data that it reports as output.

Along with “abstraction,” these two ideas are among the most important in computer science. If you get in the habit of using them in your thinking, you’ll have many fewer bugs in your programs, because you’ll automatically double-check that the output type of a reporter matches the input type of the block in which you’re trying to use it.

Predicates ask a true/false question such as “Is the random number 3?” or “Is this sprite touching the sprite called ‘Leader’?”

Every if else block has two scripts inside of it, exactly one of which will be run depending on the value that the predicate reports. Then the computer continues with whatever comes after the if else block.
if (pick random (1) to (4)) = (3) {
report (join (who) (', who') (does what) ( ) (who) (,))
} else {
report (who)
} when green flag clicked:
repeat until (touching (Leader)?)
{
    point towards (Leader)
    move (1) steps
}

  1. “U2L3-Predicates”Create a new project called U2L3-Predicates

  2. In Unit 1 Lab 5, you made a script to get the sprite to follow the mouse.
    go to (mouse-pointer)

    Now use one or more of the following relational operators to create a script that lets you use your mouse to write on the stage in two colors depending on the mouse’s position on the stage.
    less than, equal to, and greater than predicate blocks

You may see these five relational operators:

=, >, <, ≥, ≤

as well as a sixth:

, which means “not-equal” and reports false if the two inputs are equal and otherwise reports true (if they are not equal). When you write the () not equal () block, it will work like this:
(3) not equal (4) reporting true (3) not equal (3) reporting false

These six relational operators all report a Boolean value (true or false).

  1. draggable box checked Make the sprite draw only if the mouse button is down, so that you can draw disconnected shapes. You’ll need to uncheck the “draggable” box above the scripting area (shown right) before you try this (so that Snap! doesn’t think you are trying to drag the sprite when you click).

    You’ll probably want to use the mouse down? predicate block block, which you can find in the Sensing palette.

    bicolor printed hello

: Sequencing, Selection, Iteration

Selection means deciding (selecting) which part of an algorithm to run based on whether a condition is true or false.

Every algorithm can be constructed using sequencing (following steps in order), selection (deciding), and iteration (repeating).