Home / Summer / Week 2
Summer · Week 2 of 8

Logic & Control Flow

Booleans, if/else, switch statements, and logical operators.

Booleans & Logical Operators

A boolean holds exactly one of two values: true or false. Robot code is basically one giant decision tree — is the button pressed? Is the sensor triggered? Is the robot in auto? All booleans.

AND
&&
Both sides must be true. isEnabled && hasTarget — only fires if both are true.
OR
||
At least one side must be true. buttonA || buttonB — fires if either is pressed.
NOT
!
Flips the value. !isRunning is true when isRunning is false. Used constantly for guard conditions.
Comparison
==  !=  <  >
These return a boolean. speed > 0.5 evaluates to true or false.

If / Else

Run code only when a condition is true. The most fundamental control structure in any language.

java
double distanceInches = 14.5;

if (distanceInches < 12.0) {
    System.out.println("Too close — stop!");
} else if (distanceInches < 24.0) {
    System.out.println("In range — score!");
} else {
    System.out.println("Too far — drive closer");
}

FRC connection: Almost every autonomous decision in robot code is an if/else chain. "If the sensor reads X, do Y. Otherwise do Z." That's it. This is the core logic pattern.

Switch Statements

When you have one variable that could be many specific values, a switch is cleaner than a long if-else chain. In FRC we use these constantly for game states, robot modes, and enum-based control.

java
int buttonID = 2;

switch (buttonID) {
    case 1:
        System.out.println("Run intake");
        break;
    case 2:
        System.out.println("Spin up shooter");
        break;
    case 3:
        System.out.println("Deploy climber");
        break;
    default:
        System.out.println("Unknown button");
}

Don't forget break: Without it, Java "falls through" into the next case and keeps executing. Sometimes intentional, almost always a bug if you forgot it.

Fill in the Blanks

// True only if both speed is high AND target is locked
boolean canShoot = isAtSpeed hasTarget;
// Run this block if speed is greater than 0.5
(speed ) { }
// Prevent fall-through in a switch case
case 1: doSomething(); ;

Knowledge Check

Coding Challenge

Robot State Checker
Use if/else and booleans in an FRC scenario

Given: boolean isEnabled = true, boolean hasGamePiece = false, double distanceToTarget = 18.5 (inches).

Write an if/else chain that prints: "Intake" if no game piece, "Drive closer" if piece held but distance > 24, "Shoot!" if piece held and distance ≤ 24, and "Robot disabled" if not enabled. Check isEnabled first.