Search requires an index. Run npm run build once, then restart dev.
Control Structures
beginner40 min
Making Decisions with if/else
Control structures help your program make decisions and repeat actions based on conditions.
If/Else Statements:
int batteryLevel = 75;if (batteryLevel > 80) { System.out.println("Battery level is good");} else if (batteryLevel > 50) { System.out.println("Battery level is okay");} else if (batteryLevel > 20) { System.out.println("Battery level is low");} else { System.out.println("Battery critically low!");}
Comparison Operators:
Common Operators:
== - Equal to
!= - Not equal to
> - Greater than
< - Less than
>= - Greater than or equal
<= - Less than or equal
Loops - For Loop:
// For loop - when you know how many times to repeatfor (int i = 1; i <= 5; i++) { System.out.println("Count: " + i);}// For loop with arraysint[] motorPowers = {50, 75, 100, 25};for (int i = 0; i < motorPowers.length; i++) { System.out.println("Motor " + i + " power: " + motorPowers[i]);}
Loops - While Loop:
// While loop - repeat based on a conditionint attempts = 0;boolean sensorDetected = false;while (!sensorDetected && attempts < 10) { sensorDetected = checkSensor(); attempts++; System.out.println("Attempt " + attempts);}
Logical Operators:
Combining Conditions:
&& - AND (both conditions must be true)
|| - OR (at least one condition must be true)
! - NOT (reverses the condition)
Examples:
// AND exampleif (batteryLevel > 20 && motorTemperature < 80) { robot.startDriving();}// OR example if (gamepad1.a || gamepad1.b) { robot.activateClaw();}
Try It Yourself:
Exercise 1: Control Flow
An if/else chain that sets motor speed based on distance to target
A for loop that checks multiple sensors
A while loop that runs until a button is pressed
Combine conditions using logical operators
Write control structures for robot behavior:
An if/else chain that sets motor speed based on distance to target
An if/else chain that sets motor speed based on distance to target
public void setMotorSpeedByDistance(double distanceToTarget) {
double motorSpeed;
if (distanceToTarget > 50) {
motorSpeed = 1.0; // Full speed for far distances
System.out.println("Far from target - full speed");
} else if (distanceToTarget > 20) {
motorSpeed = 0.6; // Medium speed
System.out.println("Medium distance - reduced speed");
} else if (distanceToTarget > 5) {
motorSpeed = 0.3; // Slow speed for precision
System.out.println("Close to target - slow speed");
} else {
motorSpeed = 0.0; // Stop when very close
System.out.println("At target - stopping");
}
setDriveMotors(motorSpeed);
}
A for loop that checks multiple sensors
A for loop that checks multiple sensors
public void checkAllSensors() {
String[] sensorNames = {"colorSensor", "distanceSensor", "touchSensor", "gyroSensor"};
boolean[] sensorStatus = new boolean[4];
System.out.println("=== Sensor Check ===");
for (int i = 0; i < sensorNames.length; i++) {
// Simulate sensor check (replace with actual sensor code)
sensorStatus[i] = Math.random() > 0.2; // 80% chance of working
if (sensorStatus[i]) {
System.out.println(sensorNames[i] + ": [OK] Working");
} else {
System.out.println(sensorNames[i] + ": [ERROR] Error");
}
}
}
A while loop that runs until a button is pressed
A while loop that runs until a button is pressed
public void waitForButtonPress() {
System.out.println("Waiting for button press to continue...");
while (!gamepad1.a && !gamepad1.b) {
// Keep checking for button press
telemetry.addData("Status", "Press A or B to continue");
telemetry.update();
// Small delay to prevent excessive CPU usage
try {
Thread.sleep(50);
} catch (InterruptedException e) {
break;
}
}
if (gamepad1.a) {
System.out.println("Button A pressed - continuing");
} else if (gamepad1.b) {
System.out.println("Button B pressed - continuing");
}
}
Combine conditions using logical operators
Combine conditions using logical operators
public void robotSafetyCheck() {
double batteryLevel = 75;
double motorTemp = 45;
boolean emergencyStop = false;
boolean sensorError = false;
// AND operator - both conditions must be true
if (batteryLevel > 20 && motorTemp < 80) {
System.out.println("Robot ready to operate");
}
// OR operator - at least one condition must be true
if (emergencyStop || sensorError || batteryLevel < 10) {
System.out.println("[WARNING] STOPPING ROBOT - Safety issue detected");
stopAllMotors();
}
// NOT operator - reverse the condition
if (!emergencyStop && !sensorError) {
System.out.println("All systems normal");
}
// Complex combination
if ((batteryLevel > 50 && motorTemp < 70) ||
(batteryLevel > 30 && motorTemp < 60)) {
System.out.println("Safe to run autonomous mode");
}
}