Use an if Statement to Make Decisions

An if statement lets you make decisions in code and optionally run a piece of code depending on whether a condition is true. This is how you make a computer “think.”

It’s just like the real world. We run on “if” statements all the time. If your age is >= 16, you are allowed to drive. If the door is unlocked, you can open it, or else you cannot. If the command given to Minecraft is equal to "hello", then send a message. If your health drops to zero, you’re dead.

This is what it looks like in Java:

 
if​ (something) {
 
// run this code...
 
}

The part in parentheses (something) can be anything that turns out true or false; in other words, a Boolean condition (more on that in just a second).

If you ...

Get Learn to Program with Minecraft Plugins, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.