What's a Predicate?

In this lab, you will develop tools to help solve word puzzles by searching for words that match specific characteristics.

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

AAP-2.H.1

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
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)
    AAP-2.E part a
    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
AAP-2.E.2
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: AAP-2.G.1; sequencing, selection, iteration: AAP-2.A.4

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).