Skip to content

Conditionals

In everyday life, we make decisions based on different situations. For example, “If we are hungry, then we get a snack” or “If we are tired, we go to bed.” Robots often need to make different decisions based on different situations. For example, “If the timer starts, drive the robot for 5 seconds” or “If the timer is 0, stop driving.” To add decisions to our code, we use conditionals.

This section will cover three types of conditionals which are:

  • if
  • else
  • else if
Note

There is also another type of conditional called a switch statement. They aren’t common in FRC and therefore will not be covered. To learn more about switch statements, you can read about them here.

An if statement runs code only if a specified condition is true. The syntax for an if statement is as follows:

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

Let’s break down what this means:

  • if is a keyword
  • ( ) holds the condition that is either true or false. Example: (6 > 2) is a condition and it is true because 6 is greater than 2
  • { an open curly braces is used to denote the opening of the conditional
  • } a closed curly braces is used to denote the end of the conditional

The code in between the opening and closing curly braces is called a “code block”. A code block a code block is a sequence of code.

Tip

All conditionals follow a very similar syntax. They all start with a keyword, most are followed by parentheses, and they all have an opening and closing curly braces.

if statements can make decisions based on the condition inside the parentheses. If condition is true, the code inside the if block will run. If condition is false, the code inside the if block will be skipped.

Now let’s look at an example. In this example, this code will move forward only when distance is smaller then 10.

double distance = 6;
if (distance < 10) {
System.out.println("Motors are spinning");
drivetrain.setThrottle(1); // runs the motor at full speed
}

This works because the condition is distance < 10. We know distance is equal to 6, therefore we can think of the condition saying 6 < 10. 6 is less than 10 which makes the condition true. This means the code inside the if block runs, which prints “Motors are spinning” and the spins the motors at full speed.

When making decisions, sometimes there isn’t a need for precise logic. Sometimes you don’t want to (or can’t) determine the other possibilities that can be true and it would be better to encompass everything in a “catch all” type statement. For example: The robot keeps driving until distance is at 10, then stop. This is one example where we could use an else statement.

The syntax for an else statement is as follows:

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 statements do not have parentheses because they do not need a condition to decide if they should run or not. Here’s how this works: if theif statement’s condition is true, then the code inside will run. If the if statement’s condition is false, the if block will be skipped, and the code inside the else block will run.

In this example, we now have an else statement that sets the motor speed to 0 if distance is greater than 20. Since our distance is set to 21, what would the code print out?

distance = 21;
if (distance < 10) {
System.out.println("Motors are at half speed");
drivetrain.setThrottle(0.5); // runs the motors at half speed
} else {
System.out.println("Motors are not spinning");
drivetrain.setThrottle(0); // stops the motors
}

We check the first conditional distance < 10. 21 is not less than 10 so we skip that conditional and go to else. The else runs and it prints out “Motors are not spinning”, and the motors are set to 0.

if and else lets you choose between two options, but what if you wanted to choose between even more options? An else if statement let you choose between as many conditions as you like.

The syntax for an else if statement goes as follows:

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

The syntax is similar to an if statement but now it has else if at the end. Let’s break down what this new addition means. else if is a keyword that allows the conditional to run a different task if the if statement is false.

If conditionA is true then the code inside the if block will run. If conditionA if false, then it will go to the else if statement. If conditionB is true, the code inside the else if block will run. If conditionB if false, then it will exit the statement and none of the code will run.

In the example, we now have an else if statement that sets the motor to half speed if the distance from an object is less than 10cm. Our distance is now 15cm. With the new addition, what would the code print out?

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

The distance is 15cm away. The first condition is distance < 10. 15 is not less than 10, which means that condition is false. Therefore we go to the next part of the conditional which is the else if. The else if has the condition distance > 10. 15 is greater than 10 so the code inside this else if block runs. This means the motors are set to drive at full speed and “Motors are at full speed” gets printed to the terminal.

The previous code has the robot drive at full speed if the distance is greater than 10cm, and drive at half speed if the distance is less than 10cm, but it doesn’t stop if it reaches its target distance. You could add another else if, or you could put everything that we previously learned together and add an else conditional.

distance = 21;
if (distance < 10) {
System.out.println("Motors are at half speed");
drivetrain.setThrottle(0.5); // runs the motors at half speed
} else if (distance <= 20) {
System.out.println("Motors are at full speed");
drivetrain.setThrottle(1); // runs the motors at full speed
} else {
System.out.println("Motors are not spinning");
drivetrain.setThrottle(0); // stops the motors
}

We check the first conditional distance < 10. 21 is not less than 10 so we skip that conditional and go to the else if. This conditional has distance <= 20, we know 21 is not less than or equal to 20 so we skip this conditional. Since the two conditionals were false, this means the else runs and it prints out “Motors are not spinning”, and the motors are set to 0.

Exercise

WIP