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.
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.
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.
=, >, <, ≥, ≤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
).
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).