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.
isEnabled && hasTarget — only fires if both are true.buttonA || buttonB — fires if either is pressed.!isRunning is true when isRunning is false. Used constantly for guard conditions.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.
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.
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
boolean canShoot = isAtSpeed hasTarget;
(speed ) { }
case 1: doSomething(); ;
Knowledge Check
Coding Challenge
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.