498
Control Lab FRC Programming Curriculum
Fundamentals · L04 of 8

Conditionals

Prereqs: fundamentals-03
Objectives 0 / 3

Hook

Your intake runs the moment the robot enables. It doesn’t care whether there’s a game piece in front of it or not — it just spins. When there’s already a piece inside, it keeps trying to pull in another one and jams.

The motor is doing exactly what you told it. You never told it to stop.

The fix requires a decision: “if there’s no piece, spin; otherwise, stop.” That’s a conditional.

Core concept

Key Concept

A conditional executes a block of code only when a condition is true. The program evaluates the condition, picks one branch, skips the rest, and continues.

Walk-through

The basic form in Java:

if (condition) {
    // runs when condition is true
} else if (otherCondition) {
    // runs when first is false AND this is true
} else {
    // runs when all conditions above are false
}

For the intake example:

void periodic() {
    if (!sensor.hasGamePiece()) {
        motor.set(0.8);    // spin to intake
    } else {
        motor.set(0.0);    // stop — piece already inside
    }
}

The else branch is the one most beginners forget. Any time you write if, ask yourself: “what should happen when this is false?” The answer might be “nothing” — but make that explicit.

Comparison operators you’ll use constantly:

SymbolMeaning
==equal to
!=not equal to
< / >less / greater than
<= / >=less-or-equal / greater-or-equal
&&AND (both must be true)
||OR (either can be true)
⚠ Heads up

= assigns a value. == compares two values. Writing if (x = 5) is almost always a bug.

Interactive demo

An arm controller with three zones. Step through to see which branch runs for each value of angle:

Code Tracer
01if angle < 88:// 95 < 88? false
02 motor = 'up'// skipped
03else if angle > 92:// 95 > 92? true
04 motor = 'down'// runs!
05else: motor = 'hold'// skipped
State
angle95
motor?

Step through to see values update.

Initial state

Try it yourself

⚡ Try it yourself

Same arm controller, but now angle = 74. Trace through and predict which branch runs.

if (angle < 88) {
    motor = "up";
} else if (angle > 92) {
    motor = "down";
} else {
    motor = "hold";
}
⚡ Check your understanding

When angle = 74, what does motor get set to?

⚡ Check your understanding

When angle = 90, what does motor get set to?

Key takeaways

  • A conditional evaluates its condition and runs exactly one branch.
  • Once a true branch is found, all remaining else-if and else branches are skipped.
  • Always consider what should happen in the false case — even if the answer is “nothing.”
  • = assigns; == compares. Mixing them up is one of the most common bugs in any language.

Common confusions

“I have three if statements in a row, isn’t that the same as if/else-if?” No. Three separate if statements all evaluate independently. If the first one changes a variable, the second one sees the new value. else if short-circuits — once a true branch is found, the rest don’t run.

“My condition is always true no matter what I do.” Check for = vs ==. Also check whether you’re comparing the right variables — printing intermediate values helps.

Challenge

⚡ Try it yourself

Write a batteryStatus method that classifies a battery voltage reading. Use it to print the status for three readings.

Rules: voltage ≥ 12.0 → "OK", 9.0 ≤ voltage < 12.0 → "LOW", voltage < 9.0 → "CRITICAL"

Code EditorJavaCtrl+Enter to run
Stuck? Show hint

Use an if/else-if/else chain. The order matters — check the highest threshold first.

What’s next

In Lesson 04, we’ll look at loops — including why periodic() is the most important loop in your robot program and what happens when you accidentally block it.