If Statements and Loop Flow
Use decisions and loops to make robot behavior conditional and safe.
Use decisions and loops to make robot behavior conditional and safe.
In this lesson, you will:
Control flow is how robot code chooses behavior. If statements decide whether slow mode is active, whether an arm may move, whether autonomous should advance, or whether a sensor reading is safe enough to trust.
A robot loop is a repeated conversation: read the world, decide, command hardware, report telemetry. Students should avoid thinking of the loop as a place to paste random snippets. Every decision should have an input, a reason, and an observable result.
Walk through one decision at a time. Start with a slow-mode condition, then add a safety stop, then print which branch was selected. Avoid nested conditions until the first branch can be explained by a driver.
IfStatementsandLoopFlow.javaJava
String driveMode = "normal";
double scale = 0.85;
if (gamepad1.left_bumper) {
driveMode = "slow";
scale = 0.35;
}
if (gamepad1.b) {
driveMode = "emergency stop";
scale = 0.0;
}
telemetry.addData("drive mode", driveMode);Decision bugs often come from overlapping conditions or missing else branches. If two branches can both command the same motor, the last one wins. Print mode names so students can see which branch is active.
Write a drivetrain snippet with normal drive, slow mode, and an emergency stop button. Telemetry must show the selected mode.
Check your understanding before moving on.
0 of 2 answered