the video version
The same lesson as a narrated video. Watch it on YouTube
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.
Conditionals: Making Decisions#
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.
Loops: Repeating Work#
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.
Methods: Reusable Named Actions#
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.
How It Fits Together#
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.
the part worth keeping
Key takeaways
- `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.
Programming, Controls & SensorsProgramming Foundations: A Java Primerlesson 2 of 4
Keep going
Take the quiz+10 XP with an accountMore in Programming Foundations: A Java Primer
verifiedchecked against primary sources on 9 Sept 2026
where this came from
Sources and corrections
This lesson is AI-assisted: drafted from primary sources, then reviewed and edited by hand. Errors still get through. When one is reported we fix it and write down what changed, in public, in the corrections log.
sources and further reading
- docs.oracle.comOracle Java Tutorials: Control Flow Statements
- docs.oracle.comOracle Java Tutorials: Defining Methods
- dev.javadev.java: Control Flow Statements
- docs.wpilib.orgWPILib Docs: Command-Based Programming
clipped to this lesson
Articles that go further on this
The lesson gets you through the topic. These go wider on it, and they read in one sitting.
- 20 min readPlaying Defense in FRC: Legal Defense, Positioning, and Picklist ValuePlaying defense in FRC done right: when it pays off, legal vs. illegal defense and pinning rules, driving technique, counter-defense, and picklist value./blogread it
- 20 min readHow to Program FRC Swerve Drive with WPILibA practical guide to programming FRC swerve drive with WPILib: kinematics, SwerveModuleState, field-relative control, odometry, and where PathPlanner fits./blogread it
- 20 min readInstalling WPILib and VS Code for FRC (Step by Step)Learn how to install WPILib and VS Code for FRC step by step: download the installer, create a robot project, add vendordeps, and deploy to the roboRIO./blogread it
answer sheet
Lesson quiz
All 3 right completes the lesson. Miss one and only that question comes back, anything you already answered correctly stays banked.
0 of 3 answered
01How often does WPILib call your robot's periodic methods by default?
02Which loop is best when you know exactly how many times to repeat?
03What does the `return` keyword do inside a method?
Answer every question to submit.
All 51 lessons in Programming, Controls & Sensorsopenclose
01 / prerequisites
02 / foundations-tools-and-first-program
03 / robot-program-and-command-based
04 / motors-and-control
05 / autonomous-trajectories-simulation
06 / sensing-fundamentals
07 / encoders
08 / gyros-imus-orientation
09 / closed-loop-control
10 / vision-pose-estimation
11 / worked-examples-mini-projects
- Not read yet:Mini-Project: A Closed-Loop Elevator with Motion Magic
- Not read yet:Mini-Project: A Velocity-Controlled Shooter on REVLib
- Not read yet:Mini-Project: A Teleop Swerve Drive Subsystem
- Not read yet:Mini-Project: An Autonomous Routine with PathPlanner
- Not read yet:Mini-Project: Vision-Aligned Scoring with Limelight
12 / common-mistakes-troubleshooting
13 / advanced-techniques-case-studies
- Not read yet:State-Space Control and Kalman Filtering
- Not read yet:Log Replay Architecture with AdvantageKit
- Not read yet:Advanced Pose Estimation: Multi-Tag Fusion and Standard Deviations
- Not read yet:Robot Coordination, Alerts, and Operator Feedback
- Not read yet:Case Study: Hardening Software Before an Event