Yorkville High School Computer Science Department
Yorkville High School Computer Science Department on Facebook  Yorkville High School Computer Science Department Twitter Feed  Yorkville High School Computer Science Department on Instagram

Yorkville High School Computer Science

ASSIGNMENTS: No Current Assignments

Computer Programming I :: Lessons :: Minecraft If Statements

If Statements

If Statements, also known as conditional statements, determine if a given condition is true or false. If the condition is true, it will run the code inside the if statement once. For example, the if statement below will increase the block variable by 1 if height is not equal to 0.

Minecraft If Statements

An if statement has an if/then structure. The part between if and then (height ≠ 0) is the condition. If the condition is true, the code between then and end (block = block + 1) will run a single time.

Else Statements

An else statement always follows an if statement and can be used to run code if the condition of an if statement is false. For example, the if statement below sets the slot to 2 (using the select slot command) if the height is equal to 2, else it sets the slot to 1.

Minecraft Else Statements

Minecraft Tower Program

With our new knowledge of if statements, and our previous knowledge of while loops, we can create a program to build a tower in Minecraft. We will keep the tower small by just making a 4x4 tower until we run out of blocks from a single slot. To make it more interesting, we will switch up the slot on the even-numbered floors. Below is the algorithm for our tower:

  1. Set the current height to 1 and the number of blocks used to 0.
  2. While the number of blocks used is less than 64 (the maximum you can store in a slot) repeat steps 3-10.
  3. Move the turtle up one block.
  4. If the current height is an even number, set the current slot to slot 2, otherwise set the current slot to slot 1.
  5. Put a block below the turtle and move forward.
  6. If we are currently taking blocks from slot 1, increase the number of blocks used by 1.
  7. Repeat steps 5 and 6 a total of 4 times.
  8. Turn right.
  9. Repeat steps 5-8 a total of 4 times.
  10. Increase the current height by 1.

The following image represents the entire tower program, which we will examine step by step.

Step 1: Set the current height to 1 and the number of blocks used to 0.

Minecraft Set Variables We need to keep track of the height and the number of blocks we use. We will start the height at 1 and the number of blocks used at 0. Remember that the Minecraft visual editor only shows the first two letters of a variable name. The full variable names are height and blocks.

Step 2: While the number of blocks used is less than 64 (the maximum you can store in a slot) repeat steps 3-10.

Minecraft While Loop We will use a while loop for this step that makes sure the number of blocks used is less than 64.

Step 3: Move the turtle up one block.

Minecraft Move Up Command This step is pretty simple. Just use the move up command to make the turtle move up a single block.

Step 4: If the current height is an even number, set the current slot to slot 2, otherwise set the current slot to slot 1.

Minecraft Repeat Loop This is probably the trickiest step of the tower program. There isn't an easy way to check if a number is even or odd in the visual editor (it would be easier in the code editor) so we need to keep resetting the height. If the height equals 2, then we are on an even floor and we change the slot to 2 and reset the height to 0. In the else statement, we just set the slot to slot 1.

Step 5: Put a block below the turtle and move forward.

Step 6: If we are currently taking blocks from slot 1, increase the number of blocks used by 1.

Step 7: Repeat steps 5 and 6 a total of 4 times.

Step 8: Turn right.

Step 9: Repeat steps 5-8 a total of 4 times.

Minecraft If Statement Step 5 is pretty straightforward. Just use the place block command to place a block below the turtle and then use the move forward command. We will use an if statement to check if the current height is not 0, which would mean we didn't just reset the height. If this is the case, we increase the number of blocks used by one since we are currently pulling blocks from slot 1. Finally, we want to repeat these steps 4 times to make the side four blocks long. We can use a repeat command to repeat a series of statements a certain number of times. A repeat command works like a while look, except instead of a condition it takes a number to indicate how many times to repeat the statements. We then have a turn right command that is in another repeat loop to create four walls.

Step 10: Increase the current height by 1.

Minecraft If Statement The final step increases the height variable by 1. If the height had been reset to 0 because it reached 2, that means the height will be 1. If the height was already 1 it will be increased to 2. We will switch back and forth between these two heights.

After running the program, the tower will look something like the tower below depending on the blocks used:

Minecraft Tower

Debugging in Minecraft

Minecraft Variable Inspector

Debugging a program means using tools and strategies to fix problems you encounter. The visual editor in Minecraft includes some basic debugging tools.

MinecraftEdu Step ProgramIf you are having trouble determining what is going wrong with your program, you can use the step button to run your program a line at a time. Often this will help you determine what is going wrong. A green border indicates the part of your program that is currently running.

While you are running your program or stepping through it, any active variables will appear in the variable inspector in the upper-right of the visual editor. You can see how these variables are changing, which could be another clue about what is wrong with your program.

Yorkville High School Computer Science Department on Facebook Yorkville High School Computer Science Department Twitter Feed Yorkville High School Computer Science Department on Instagram