Decision Tree
One way that a computer program (and a human!) can make a decision is by using a decision tree. Here's an example of a decision tree for going out to play:
The decision making process starts at the top. The program checks the outlook (sunny, overcast, or rainy) and follows the path corresponding to each value. If it's sunny, it then checks the humidity level. A high humidity (> 70) results in the decision of staying in, while a lower humidity results in the decision of playing. All of the paths eventually result in either "play!" or "stay in!", so this program will always result in a decision.
In this lab, you'll code this decision tree, learn about testing your code, and then come up with your own custom decision tree.
Coding the Tree
We want to turn that decision tree into a block that returns either true or false, like this:
Block parameters
- To get started, click "Make a block" in the sidebar. In the pop-up, type a name like "go play outside?" and select "predicate". A predicate block is a special kind of reporter block that returns either true or false (not text or numbers).
-
Now add an input parameter by clicking the + sign in the block header. Type "outlook" as the name. There should only be three possible values for that parameter, and we can actually make it into a dropdown by customizing the parameter. Click the arrow in that popup to see more customization options, then click the gear in the bottom right corner for even more options. Select "options" in that gear pop-up. Click for an image.
-
Type the three options in the popup, separated by new lines. Click for an image.
-
Add another input parameter and type "humidity" as the name. Click the right arrow again for options, and check the "Number" box in the left column to specify that it's a numeric parameter. Click for an image.
-
Add one more input parameter and type "windy" as the name. Click the right arrow again for options, and check the "Boolean" box in the right column to specify that it should be either true/false. Click for an image.
-
Close the block editor and check out your new block.

It doesn't do anything yet, but it does have some fancy input parameter types! Specifying the input types makes it easier for users to give the block valid input, so it's always a good thing to do if you can.
Block body
-
Now let's actually define the block. Right click on the block and click "edit" to go back into the block editor. Here's a reminder of the decision tree that we need to represent in code:

In a decision tree diagram, each diamond represents a question with multiple paths flowing from it. In a Snap program, a diamond can be coded using if/else blocks.
We'll guide you through coding the first flow, through the sunny part of the tree.
-
Drag the if/else block from the Control palette and snap it under the block header.
-
All of the arrows coming from the top diagram represent possible values for the outlook parameter. The first arrow represents the outlook being "sunny", so that's what should go in the if condition. Drag the = operator block into the diamond slot next to the if, then drag the outlook parameter into one side and type "sunny" on the other side.
-
What goes inside the
if
? Well, in the diagram, that arrow leads to another diamond! That means another if/else block. Drag another one into that block.
-
The next diamond asks the question of humidity level, with different paths depending on whether it's greater than 70 or not. Drag the > operator into that diamond slot, then drag the humidity parameter into one side and type 70 in the other side.
-
The arrows from the humidity diamond lead to rectangles, indicating the actual decision, either play (true) or stay in (false). Snap a report block into each of the if/else slots, then drag true/false blocks into each report block and set them to the appropriate value.
-
Okay, our block can now make a decision, as long as it's sunny outside! 🌞🌞🌞 If you want, you could exit the block editor now and do a few checks to see if the sunny-day decisions make sense.
-
Now see if you can code the rest of the decision tree, using the same sort of blocks we used already.
Here's a hint to get you started: you'll want to use another if/else inside the currently empty else, since you still have two more outlooks to handle.
Testing the Tree
At this point, you've probably done a few checks to see that the block is reporting true/false appropriately. Testing a program for correctness is a very important part of computer programming, since it's very easy to accidentally write buggy code. In fact, most programmers use dedicated tools for testing.
We've made two reporter blocks to make it even easier for you to check your decision tree block, inspired by typical program testing tools.
-
The first block checks that the value of the first parameter is the same as the value of the second parameter. To use it, drag an already filled out
go play outside?
block into the first slot, then drag a true/false block into the second slot. That second slot should be true if you expect the result to be true or false otherwise.
For example, when it's sunny and humid outside, it should be false. If the code produces the expected value, you'll see a "Test passed!".

Here's an example of a test for when it's sunny and not that humid. This time, we expect it to be true.
- Now go make test blocks for lots more situations. What combinations of parameters should you check for? Where is it likely for your code to have bugs? You should have at least 5 test blocks, but probably even more than that.
- If you discover a test block that says "Test failed!", first check your test case. Did you really input the parameters you thought you did? Then check your block's code, especially near where it should have made the decision. Did you use the right comparison operator in the if condition? Did you report the correct value?
All together now
- Now that you have a bunch of passing tests, it'd be nice if you could run them all at once instead of having to click a billion times. That's the purpose of the check all tests block! Just drag a test block into the first slot. Then click the right arrow to add a slot for each of your additional tests and drag the rests of your tests in.
At any point, you can click the block to confirm that all the tests are passing.
Here's the awesome thing about testing: if you realize there's a better way to code the logic of the block, you can edit the block definition, change the code, and then just click this block to make sure all the tests pass.
Testing is a great way to make sure that a computer program is actually doing what it's meant to be doing. Many programs have more tests than code, in fact, since it's so important that they work as expected. Consider the software used on a Boeing 747 airplane or on a space shuttle: you wouldn't want to ride on a plane running untested code, right? *Shudder*
Create your own decision tree
-
This time, you're going to make your own decision tree. What sort of decisions do you want a computer to make for you?
Here are some ideas to kickstart your brainstorming:
- Deciding on the best move to make in a sport. For example, deciding in baseball whether to run to third base depending on parameters about how the ball was hit and whether the bases are full.
- Deciding whether to go out to a restaurant for dinner or stay inside, considering parameters like cost, how empty the fridge is, hunger level, etc.
- Deciding whether to wear a jacket outside (using similar parameters as the tree you coded, most likely).
You can also have decision trees return a set of values, not just true or false. Consider:
- Recommending a sport for someone to play, based on whether they like solo/group activities, how risk averse they are, what weather they like, how much exercise they want, how competitive they are, etc.
- Choosing what to eat for breakfast, based on preferences for cold/hot breakfast, how many minutes you have preparation, whether you want to eat healthy, etc.
-
Work with a partner and brainstorm ideas on paper. Write them out as flowcharts just like the one we showed here. Every path should eventually end in a decision.
-
Once you've drawn it on paper, share it with another pair and ask for feedback. Is anything unclear? Do they suggest a different flow? Make a new iteration if you find their feedback useful.
Coding your tree
Now you'll code your own decision tree, just like you coded the one earlier. If it's similar, you can even duplicate the earlier block and start from that, but you can also start from scratch. Feel free to scroll up to see the steps we used for the first block.
Follow the testing steps too, so that your custom block is just as well-tested.
Once you're done, share it with the whole class!