Search requires an index. Run npm run build once, then restart dev.
Search needs internet. Offline lessons: use the sidebar or download a track under Reading preferences (Aa).
Conditional Statements
beginner75 min
Conditional statements
When a condition is true, Java runs one block of code; when it is false, it can run another. That is how programs react to sensor values, gamepad input, and mode flags.
int robotPosition = 45;if (robotPosition > 50) { System.out.println("Robot is in the forward zone");} else { System.out.println("Robot is in the back zone");}// Another exampledouble batteryVoltage = 11.5;if (batteryVoltage >= 12.0) { System.out.println("Battery voltage is good!");} else { System.out.println("Battery needs charging!");}
else if
Chain else if to test several conditions in order. The first true branch runs; the rest are skipped.
Multiple conditions
int gameScore = 85;if (gameScore == 100) { System.out.println("Perfect score!");} else if (gameScore >= 90) { System.out.println("Excellent performance!");} else if (gameScore >= 70) { System.out.println("Good job!");} else if (gameScore >= 50) { System.out.println("Keep practicing!");} else { System.out.println("Need more work!");}
Order of execution
Conditions are checked from top to bottom.
Only the first true branch runs.
Put more specific tests before general ones (see FizzBuzz example below).
Execution order example
int number = 15;// This works correctlyif (number % 3 == 0 && number % 5 == 0) { System.out.println("FizzBuzz"); // Divisible by both 3 and 5} else if (number % 3 == 0) { System.out.println("Fizz"); // Divisible by 3 only} else if (number % 5 == 0) { System.out.println("Buzz"); // Divisible by 5 only} else { System.out.println(number); // Not divisible by 3 or 5}// This would NOT work correctly:// if (number % 3 == 0) would catch 15 first!
Comparing Strings
For String values, use .equals() to compare characters. == checks whether two references point to the same object, which is not what you want for user input or telemetry text.
String comparison
String robotStatus = "active";String userInput = "ACTIVE";// Correct way to compare stringsif (robotStatus.equals("active")) { System.out.println("Robot is running");}// Case-insensitive comparisonif (userInput.equalsIgnoreCase("active")) { System.out.println("User wants robot active");}// Comparing two string variablesString command1 = "forward";String command2 = "backward";if (command1.equals(command2)) { System.out.println("Commands are the same");} else { System.out.println("Commands are different");}// Wrong way (don't do this)if (robotStatus == "active") { // This may not work as expected!}
Logical Operators
Use && (AND), || (OR), and ! (NOT) to combine boolean expressions.
Logical operators
&& - AND (both conditions must be true)
|| - OR (at least one condition must be true)
! - NOT (reverses the boolean value)
Examples:
// AND operator - both must be trueint speed = 50;int temperature = 60;if (speed >= 30 && temperature < 80) { System.out.println("Safe to operate");}// OR operator - at least one must be trueboolean buttonA = true;boolean buttonB = false;if (buttonA || buttonB) { System.out.println("A button was pressed");}// NOT operator - reverses the conditionboolean emergencyStop = false;if (!emergencyStop) { System.out.println("Normal operation");}