If Statements

Now we will learn about if statements. We use these every day when we make decisions. If it is sunny outside, then I will play outside. If I am hungry, then I will get a snack. If x, then y is what these look like. In Rokit Brick we will use these, they are in the “Control” tab, towards the bottom of the list of blocks. We will use our knowledge of variables from last time to learn about if statements. Let’s create a variable (I will name mine Test) and have it so that when we press the green flag it sets Test to 5.


Then we will have an if statement check if it is less than a number (for now we will pick 7). We will start by dragging an if statement to the bottom. Then we will add the condition to it (the if x part). If we go to the green “Operators” tab we will see three comparisons (less than, equal to, and greater than) on the left side, part way down the list. We will check if our variable is less than 7, so we will use the first one of them. Drag it to the little diamond shape in the if statement. Then we will add the variable Test to the left side of the less than block by going to the “Variables” tab and dragging the rounded Test variable block to the white square on the left of less than block. Then for the other side of the comparison, we will type the number 7 in. This will now check if Test is less than 7.


Now we need to add in what to do if Test is less than 7. We will set Test to 1 if it is true. So we will drag a “set Test to 1” block to the top little nub just below the if. So now if Test is less than 7 it will set it to 1. Since Test starts at 5 (which is less than 7), it should end the program equal to 1. Try it out yourself.

Try it out: Try to make it so that after running the program Test is not equal to 1.

Answer: If you set Test to a value of at least 7 in the first set it should work since it would not be less than 7 so it would not get set to 1.

Next we will look at the block just below the if statement, it is the “if else” block. This block rather than just letting you say what will happen if something is true, will also let you set what will happen if it is false. So an example is if is “if i am hungry, then I will eat a snack, else I will not eat food.” In this case I specify what I will do if it is false. So we will take our code and replace the if with an if else block. Then we will tell it what to happen when Test is not less than 7. Under the else nub we will set Test to 0.


Try it out: Try your code with different numbers for Test and see if you can find some values to make Test equal to 0 and some others that make Test equal to 1 at the end of the program.

Challenge:  Create another if statement where if Test is greater than 14 it will set Test to 11 and if it is not greater than 14 it will set Test to 32.