Skip to main content

Lesson 5.2: Using else if Chains to Pick One Robot Action at a Time


Technical Context

Using else if chaining prevents conflicting hardware commands within the same execution cycle. For example, if a programmer uses two independent if statements to control an arm (one for "up" and one for "down"), pressing both buttons simultaneously could send rapid, conflicting voltage signals to the Control Hub, causing jitter and mechanical strain on the motor gears.


Why else if Chains Prevent Conflicting Commands

else if and else clauses allow for the creation of a priority ladder. When Java encounters an if/else if chain, it evaluates conditions from top to bottom and executes only the first block that is true. The final else block acts as a catch-all fail-safe, which is critical for ensuring hardware returns to a "neutral" or "zero-power" state when no inputs are active. This structure ensures that the DcMotor's setPower() method receives exactly one unambiguous command per loop.


Annotated Code

@Override
public void loop() {
// Priority 1: Move Forward
if (gamepad1.dpad_up) {
driveMotor.setPower(0.5);
}
// Priority 2: Move Backward (Only checked if dpad_up is false)
else if (gamepad1.dpad_down) {
driveMotor.setPower(-0.5);
}
// Fail-safe: Stop (Executed only if both inputs are false)
else {
driveMotor.setPower(0.0);
}
}

Fill-in-the-Blank Practice

  1. To handle a condition that is only checked if the previous if statement was false, we use the __________ keywords.
  2. The __________ block is used to execute code when all other preceding conditions in the chain have evaluated to false.
  3. Chaining logical decisions is essential to prevent sending __________ commands to hardware in the same loop cycle.
Show answers
  1. else if
  2. else
  3. conflicting

Template Challenge

Robot Scenario: Control a Servo named "grabber". If Button A is pressed, set the position to 1.0 (closed). If Button B is pressed, set it to 0.0 (open). Otherwise, the servo should stay at 0.5 (ready position).

Unit 5 simulator
Supports gamepad-driven `if`, `else if`, and `else` logic with live branch visualization.
Includes truth-table and condition-inspector views, plus mock distance and color sensor inputs when needed.
Best for practicing decision logic, thresholds, and simple actuator commands.
Show answer
@Override
public void loop() {
if (gamepad1.a) {
grabber.setPosition(1.0);
} else if (gamepad1.b) {
grabber.setPosition(0.0);
} else {
grabber.setPosition(0.5);
}
}

Ready to move on?

Sign in with Google to save your progress with Telemark, or continue without saving.