Statements & Loops
Variables store data and operators work with it, but how do you make your robot decide what to do? Control flow statements let your program make choices and repeat actions based on conditions.
If Statements
The most basic way to make decisions. If a condition is true, do something.
int batteryLevel = 85;
if (batteryLevel > 50) {
// This code runs if the condition is true
telemetry.addLine("Battery is good!");
}
If-Else
What if you want to do something different when the condition is false?
if (batteryLevel > 50) {
telemetry.addLine("Battery is good!");
} else {
telemetry.addLine("Low battery warning!");
}
If-Else-If
Need to check multiple conditions? Chain them together:
if (batteryLevel > 75) {
telemetry.addLine("Battery excellent");
} else if (batteryLevel > 50) {
telemetry.addLine("Battery good");
} else if (batteryLevel > 25) {
telemetry.addLine("Battery low");
} else {
telemetry.addLine("Battery critical!");
}
Java checks conditions from top to bottom and stops at the first true one.
Switch Statements
When you need to compare one variable against many possible values, switch is cleaner than lots of if-else statements:
int gameMode = 2;
switch (gameMode) {
case 1:
telemetry.addLine("Autonomous mode");
break;
case 2:
telemetry.addLine("TeleOp mode");
break;
case 3:
telemetry.addLine("Test mode");
break;
default:
telemetry.addLine("Unknown mode");
break;
}
Don't forget the break statements! Without them, the code will "fall through" to the next case.
While Loops
Repeat code while a condition stays true:
int countdown = 5;
while (countdown > 0) {
telemetry.addLine("T-minus " + countdown);
countdown--; // Don't forget to change the condition!
}
telemetry.addLine("Blast off!");
Do-While Loops
Like a while loop, but it always runs at least once:
boolean sensorDetected = false;
do {
// Check sensor reading
sensorDetected = checkSensor();
telemetry.addLine("Scanning...");
} while (!sensorDetected);
For Loops
Perfect when you know exactly how many times to repeat something:
// Count from 0 to 9
for (int i = 0; i < 10; i++) {
telemetry.addLine("Loop iteration: " + i);
}
The for loop has three parts:
- Initialize:
int i = 0(start value) - Condition:
i < 10(when to stop) - Update:
i++(what to do each time)
Practical Example
// Flash LED 5 times
for (int flash = 1; flash <= 5; flash++) {
turnOnLED();
sleep(500); // Wait half a second
turnOffLED();
sleep(500);
}
Loop Control
Sometimes you need to change how loops behave:
Break
Exit the loop immediately:
for (int i = 0; i < 100; i++) {
if (emergencyStop) {
break; // Exit the loop right now
}
// Keep running normally
}
Continue
Skip the rest of this iteration and start the next one:
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
telemetry.addLine(i); // Only prints odd numbers
}
Quick Reference
| Statement | When to Use | Example |
|---|---|---|
| if | Single condition | if (x > 5) { ... } |
| if-else | Two possibilities | if (x > 5) { ... } else { ... } |
| if-else-if | Multiple conditions | if (x > 10) { ... } else if (x > 5) { ... } |
| switch | Many exact values | switch (mode) { case 1: ... } |
| while | Repeat while true | while (running) { ... } |
| do-while | At least once, then while true | do { ... } while (condition) |
| for | Exact number of times | for (int i = 0; i < 10; i++) { ... } |