Unit 2: Java Basics
Conditional statements are the brain of your program. They allow your robot to make decisions and execute different actions based on whether a certain condition is true or false, bringing intelligence and adaptability to its behavior.
At the heart of every conditional statement is a boolean expression—a question that can only be answered with `true` or `false`. These expressions are built using the comparison and logical operators you've already learned. Your code will evaluate this expression and use the result to decide which path to take.
The `if` statement is the most basic form of decision-making. It executes a block of code only if the specified condition is `true`. If the condition is `false`, the code block is skipped entirely.
// FRC Example: Only run the intake if the driver presses a button.
boolean intakeButtonPressed = true; // Assume this comes from a controller
if (intakeButtonPressed == true) {
// This code will only run if the button is pressed.
intakeMotor.set(0.8);
}
// The program continues from here regardless.The `if-else` statement provides an alternative path. If the condition is `true`, the `if` block is executed. If the condition is `false`, the `else` block is executed. One of the two blocks is guaranteed to run.
// FRC Example: Checking if a game piece is detected.
boolean gamePieceSensorTripped = false;
if (gamePieceSensorTripped == true) {
// Action if we have a piece
intakeMotor.stop();
indicatorLight.set(Color.Green);
} else {
// Action if we DON'T have a piece
intakeMotor.set(0.8);
indicatorLight.set(Color.Red);
}When you have multiple conditions to check in sequence, you use an `if-else if-else` ladder. The conditions are evaluated from top to bottom. As soon as one is found to be `true`, its code block is executed, and the rest of the ladder is skipped. The final `else` block is a catch-all that runs if none of the preceding conditions were true.
// FRC Example: Setting arm position based on driver input.
int desiredPosition = 2; // 1 for Intake, 2 for Amp, 3 for Stow
if (desiredPosition == 1) {
arm.setAngle(110.0); // Go to intake position
} else if (desiredPosition == 2) {
arm.setAngle(85.0); // Go to Amp scoring position
} else if (desiredPosition == 3) {
arm.setAngle(0.0); // Go to stowed position
} else {
// If an invalid number is given, do nothing or log an error.
System.out.println("Invalid arm position requested!");
}A `switch` statement is a cleaner alternative to a long `if-else if-else` ladder when you are checking a single variable against multiple possible constant values. It can make your code more organized and easier to read.
// FRC Example: Choosing an autonomous routine.
String autoSelected = "Two Piece Center";
switch (autoSelected) {
case "Two Piece Center":
// Run the commands for this auto
break; // The 'break' is crucial!
case "Leave Community":
// Run the commands for this auto
break;
default:
// Run a default, safe auto if no match is found
break;
}The `break;` statement is essential inside a `switch`. It tells the program to exit the `switch` block once a match is found. If you forget it, the code will "fall through" and execute the code for the next case as well, which is almost never what you want.
Question: You need to write code for a robot arm that has three distinct target positions: `INTAKE`, `SCORE`, and `CLIMB`. Which conditional structure would be the cleanest and most appropriate for choosing which angle to move the arm to?