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
or
). 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.
:
Input Type,
Output Type,
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.
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

block, it will work like this:
These six relational operators all report a Boolean value (true
or false
).
-
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

block, which you can find in the Sensing palette.
:
Sequencing,
Selection, and
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).
-
AAP-2.E.4B
Consider the following code segment.

If a user enters "password123," what will be the output?
Access granted!
"password123" does not match "secret" so the if condition is true.
Access denied!
Correct! Since "password123" does not equal "secret," the if condition is true, and "Access denied!" is displayed.
No output; there will be an error message.
This code will run without errors and always produces output.
No output; the code does not provide an output for this situation.
The code handles all inputs and always says either "Access granted!" or "Access denied!"
-
AAP-2.L.1D
Do the following code segments produce the same outcome?
No, wherever one code segment sets the pen color to blue, the other sets the pen color to yellow (and vice versa).
In most cases, the two code segments will set the pen color to the same color. Notice the two code segments have different inequality signs (one is less than, the other is greater than).
No, they have different outcomes when the x position of the sprite is 100.
Correct! For "if x position less than 100," the else condition will run when x = 100 (and the pen color will be yellow). Similarly, for "if x position greater than 100," the else condition will run when x = 100 (and the pen color will be blue).
Yes, the outcome for both segments is no change; the pen color will never change.
In both code segments, the pen color will change based on the sprite's x position.
Yes, wherever one code segment sets the pen color to blue, the other does too (and likewise for yellow).
This is true except when the sprite's x position is exactly 100.