Skip to content

Control Flow: Conditionals, Loops, and Methods

learnfrc.com
learnfrc.comAuthor
Veer Bajaj
Veer BajajMaintainer

By default a program runs top to bottom, one line after another. A robot has to react : drive only while a button is held, stop the arm when a limit switch trips, recompute output 50 times a second. Control flow is how you change what runs, and when.

An if runs a block only when a boolean is true:

if (joystickY > 0.1) {
driveForward();
} else if (joystickY < -0.1) {
driveBackward();
} else {
stop();
}

That 0.1 is a deadband — joysticks never rest at exactly zero, so you ignore tiny values to stop the robot from creeping when nobody’s touching the stick. When you’re checking one value against many fixed options, a switch reads cleaner than a long if/else chain.

A loop repeats a block.

  • for — when you know the count, e.g. setting up four swerve modules:

    for (int i = 0; i < 4; i++) { initModule(i); // i = 0, 1, 2, 3 }

  • while — repeat as long as a condition holds.

Read this twice: never write a while loop that waits on a robot condition inside your main robot code. WPILib already calls your code in a fast loop — about every 20 ms, 50 times a second. A blocking while freezes that cycle, so the robot stops reading the joystick and stops responding to the field. This is the #1 reason a beginner’s robot “hangs.” Let the framework do the looping; you describe one slice of behavior per cycle.

A method is a named chunk of code. It can take parameters (inputs) and return a value:

double applyDeadband(double value, double band) {
if (Math.abs(value) < band) {
return 0.0;
}
return value;
}

The parts: a return type (double, or void for nothing), a name in camelCase, parameters in parentheses, and a body where return sends a value back and exits. You call it by name: double clean = applyDeadband(rawInput, 0.1);.

Methods keep code DRY — write the logic once, reuse it everywhere. WPILib’s MathUtil.applyDeadband(...) does exactly this job (and rescales the rest of the range), so once you can read methods you’ll start reaching for the library’s tools instead of rewriting them.

Real robot code is conditionals and method calls layered inside the framework’s loop:

void teleopPeriodic() { // WPILib calls this ~50x per second
double speed = MathUtil.applyDeadband(driver.getLeftY(), 0.1);
speed *= turbo.getAsBoolean() ? 1.0 : 0.6;
drivetrain.drive(speed);
}

Conditionals, loops, and methods are enough to express almost any robot behavior.

  • if/else if/else and switch let your program choose what to do based on boolean conditions.
  • Use a for loop for a known count and a while loop for an open-ended condition.
  • Never use a blocking while loop in FRC periodic code — WPILib already loops you ~every 20 ms (50 Hz).
  • Methods bundle logic into reusable, named, testable units that keep code DRY.
  • A method signature is: return type, name, parameters, then a body that may return a value.

This lesson was adapted from learnfrc.com.