Skip to content

Conditionals

In the previous lesson, we used comparison and logical operators to produce true or false values. In this lesson, we’re going to use those values to make decisions with conditionals, which run a block of code only when a condition is true.

In everyday life, we make decisions all the time, like “If I’m hungry, I get a snack.” Robots make decisions too, like “If the robot is far from the wall, drive forward,” or “If the match timer reaches 0, stop driving.” So far, every line of code we’ve written runs every time. Conditionals let us choose which code runs based on values like a sensor reading or the time left in the match.

We’ll cover three kinds of conditionals: if, else, and else if.

Note

Java also has a switch statement, which isn’t covered here because it’s rarely used in FRC. If you want to learn more about it, see w3schools.

An if statement runs a block of code only if its condition is true:

if (condition) {
// code to run when condition is true
}

An if statement has three parts:

  • the if keyword
  • a condition in parentheses, which is either true or false, like 6 > 2
  • a code block, which is the code between the curly braces { and }

If the condition is true, the code block runs. If it’s false, the code block is skipped, and the program moves on to the code after it.

Here’s an example that drives the robot forward only if it’s more than 10 centimeters from a wall:

double distance = 60; // distance to the wall, in centimeters
if (distance > 10) {
System.out.println("Driving at full speed");
drivetrain.setThrottle(1); // runs the motors at full speed
}

distance is 60, so the condition distance > 10 is true. The code block runs, printing Driving at full speed and setting the drivetrain to full speed. If distance were 5 instead, the condition would be false, and nothing would be printed.

Tip

Every conditional in this lesson follows the same pattern: a keyword, usually a condition in parentheses, and a code block in curly braces. You’ll see the same pattern again in the next lesson, on loops.

An if statement on its own does nothing when its condition is false. Often, though, we want to do something else in that case, like stopping the robot once it’s close to the wall. An else block runs only when the if statement’s condition is false:

if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}

else has no condition of its own. It catches every case the if didn’t. Exactly one of the two code blocks runs, never both.

Now the robot drives when it’s far from the wall and stops when it’s close:

distance = 6;
if (distance > 10) {
System.out.println("Driving at full speed");
drivetrain.setThrottle(1); // runs the motors at full speed
} else {
System.out.println("Stopped");
drivetrain.setThrottle(0); // stops the motors
}

Without running the code, what does it print?

Answer

It prints Stopped. distance is 6, so distance > 10 is false. The if block is skipped, and the else block runs instead, stopping the motors.

if and else choose between two options, but sometimes we need more. An else if adds another condition, which is only checked if every condition before it was false:

if (conditionA) {
// code to run when conditionA is true
} else if (conditionB) {
// code to run when conditionA is false and conditionB is true
}

Java checks the conditions from top to bottom, and runs the code block for the first one that’s true. Once one code block runs, the rest are skipped, even if their conditions are also true. If none of the conditions are true, none of the code blocks run.

Let’s use else if to slow the robot down as it gets closer to the wall:

distance = 30;
if (distance > 50) {
System.out.println("Driving at full speed");
drivetrain.setThrottle(1); // runs the motors at full speed
} else if (distance > 10) {
System.out.println("Driving at half speed");
drivetrain.setThrottle(0.5); // runs the motors at half speed
}

distance is 30, so the first condition, distance > 50, is false, and Java moves on to the else if. distance > 10 is true, so the robot drives at half speed.

The order of the conditions matters here. A distance of 60 is greater than both 50 and 10, but only the first matching block runs, so the robot drives at full speed. If we had checked distance > 10 first, the robot would drive at half speed even when it’s far from the wall.

The previous example doesn’t do anything once the robot is within 10 centimeters of the wall, so the motors keep running at whatever speed they were last set to. We can fix this by adding an else at the end, which catches every remaining case:

distance = 10;
if (distance > 50) {
System.out.println("Driving at full speed");
drivetrain.setThrottle(1); // runs the motors at full speed
} else if (distance > 10) {
System.out.println("Driving at half speed");
drivetrain.setThrottle(0.5); // runs the motors at half speed
} else {
System.out.println("Stopped");
drivetrain.setThrottle(0); // stops the motors
}

Without running the code, what does it print? Careful, this one’s a little sneaky.

Answer

It prints Stopped. distance is exactly 10. distance > 50 is false, and distance > 10 is also false, since 10 is not greater than 10. Neither condition is true, so the else block runs and stops the motors.

You can chain as many else ifs as you need between the if and the else, and the else at the end is optional.

Exercise

Now, you can dive straight into Conditionals.java! Follow the comments and write the appropriate code to achieve the objectives.