Unit 2: Java Basics
Loops are fundamental control structures that allow you to execute a block of code repeatedly. They are essential for tasks that involve iteration, such as processing sensor data, performing calculations multiple times, or running a mechanism until a condition is met.
Robot code runs in a continuous cycle, typically 50 times per second. This entire process is a loop! Within that main loop, we use smaller loops to handle tasks like processing all the wheels in our drivetrain or waiting for a sensor to trip. Without loops, our code would be incredibly long, repetitive, and inefficient.
The `for` loop is ideal when you know in advance how many times you want to iterate. It combines the initialization, condition, and increment/decrement steps into a single, compact line.
// A for loop has three parts:
// for (initialization; condition; update)
// FRC Example: Print the numbers 1 through 5.
for (int i = 1; i <= 5; i++) {
System.out.println("Current count: " + i);
}
// FRC Example: Using an enhanced for loop to check sensor statuses.
boolean[] sensorOK = {true, true, false, true};
for (boolean status : sensorOK) {
if (!status) {
System.out.println("Warning: A sensor is offline!");
}
}
The `while` loop executes a block of code as long as a specified condition remains `true`. The condition is checked *before* each iteration. If the condition is initially `false`, the loop will never run.
// FRC Example: Run an intake motor until a sensor detects a game piece.
boolean haveGamePiece = false; // This would come from a sensor
while (haveGamePiece == false) {
// This code will run repeatedly until the sensor is tripped.
intakeMotor.set(0.8);
// In real code, you would update haveGamePiece based on the sensor reading here.
// haveGamePiece = intakeSensor.isTripped();
}
// Once the loop finishes, stop the motor.
intakeMotor.stop();
It is crucial to ensure the condition in a `while` loop will eventually become `false`. If the condition always stays `true`, you will create an infinite loop, which can freeze your robot's code.
The `do-while` loop is similar to the `while` loop, but with one key difference: the condition is checked *after* the code block is executed. This guarantees that the loop will run at least once, even if the condition is initially `false`.
// FRC Example: Running a diagnostic check at least once.
int systemCheckCount = 0;
boolean allSystemsGo = false;
do {
System.out.println("Running system check #" + (systemCheckCount + 1));
// Code to check sensors, motors, etc.
// allSystemsGo = runDiagnostics();
systemCheckCount++;
} while (allSystemsGo == false && systemCheckCount < 3); // Retry up to 3 times.
This loop is less common in FRC, but can be useful in specific situations like user menus or when you need to ensure an action is performed before its condition is evaluated.
Question: You need to write an autonomous command to run an intake motor until a beam break sensor is tripped. You do not know how long this will take. Which loop is the most appropriate for this task?