Skip to content

Loops

In the previous lesson, we used conditionals to decide whether a block of code should run. In this lesson, we’re going to learn about loops, which let a block of code run more than once.

Programs often need to do the same thing several times in a row. In everyday life, if you know your grades are about to be released, you might check for them over and over until they come out. A robot does the same kind of thing: it might keep driving forward until it’s close enough to a wall, or run the launch sequence for every ball in its hopper. Writing the same line of code once per repetition would be tedious, and it wouldn’t work at all if you don’t know ahead of time how many repetitions you need.

Conditionals and loops both control which code runs, but a conditional runs its code block at most once, while a loop can run its code block many times. We’ll cover the two main kinds of loops: while loops and for loops.

Note

Java also has a do-while loop, which isn’t covered here because it’s rarely used in FRC. If you want to learn more about it, see w3schools.

A while loop runs its code block over and over, for as long as its condition is true:

while (condition) {
// code to run when condition is true
}

A while loop looks a lot like the if statement from the previous lesson. It has the while keyword, a condition in parentheses that is either true or false, and a code block in curly braces.

The difference is what happens after the code block finishes. An if statement moves on to the code after it, but a while loop checks its condition again. If the condition is still true, the code block runs again, and this repeats until the condition is false. If the condition is false the first time it’s checked, the code block never runs. Unlike an if statement, a while loop can’t have an else attached.

Here’s a loop that prints the numbers from 0 to 5:

int i = 0;
while (i < 6) {
System.out.println(i); // prints 0, 1, 2, 3, 4, 5
i++;
}

i starts at 0. Each time through the loop, the code prints i, adds 1 to it with i++, and then checks i < 6 again. Does i++ look familiar? It’s the increment operator from the Operators lesson. Once i reaches 6, the condition is false and the loop ends, so the last number printed is 5.

Each pass through a loop’s code block is called an iteration, so this loop runs for six iterations.

A loop’s code block can contain any code, including an if statement. This loop tells the robot to drive forward for a few iterations, then stop:

int loopCount = 0;
while (loopCount <= 5) {
if (loopCount < 5) {
System.out.println("Drive Forward");
} else {
System.out.println("Stop Driving");
}
loopCount++;
}

Without running the code, what does it print?

Answer

It prints Drive Forward five times, then Stop Driving once.

The loop runs while loopCount <= 5, so it runs once for each value of loopCount from 0 to 5. That’s six iterations. In the first five, loopCount < 5 is true, so the if block runs. In the last one, loopCount is 5, so the else block runs instead. Then loopCount becomes 6, the loop’s condition is false, and the loop ends.

Many loops follow the same pattern: create a counter variable, check it in the condition, and update it at the end of each iteration. Here’s that pattern written as a while loop:

int i = 0;
while (i < 6) {
System.out.println("Hi!");
i++;
}

This loop depends on three pieces: int i = 0, i < 6, and i++. A for loop puts all three in one place, at the top of the loop:

for (int i = 0; i < 6; i++) {
System.out.println("Hi!");
}

The parentheses after for hold three parts, separated by semicolons:

  • Initialization (int i = 0) runs once, before the loop starts, and usually creates the counter variable.
  • Condition (i < 6) is checked before every iteration, and the loop keeps going while it’s true.
  • Update (i++) runs at the end of every iteration.

These two loops do the same thing, but the for loop is easier to read. Where the counter starts, when the loop stops, and how the counter changes are all on one line, instead of spread across the loop.

Tip

All three parts of a for loop are optional, but the two semicolons are always required. The loops in this course always use all three parts.

Without running the code, what does this loop print?

for (int i = 0; i < 5; i++){
System.out.println(i);
}
Answer

It prints the numbers 0, 1, 2, 3, and 4, one per line. i starts at 0 and increases by 1 every iteration. When i reaches 5, the condition i < 5 is false, so the loop ends before 5 is printed.

A loop only stops when its condition becomes false. If nothing inside the loop ever changes the condition, it stays true forever, and the loop never stops. This is called an infinite loop.

On a robot, an infinite loop is dangerous. The drivetrain might never stop driving, or an arm might keep moving until it hits something. This loop is meant to drive forward for 7 iterations:

int loopCount = 0;
while (loopCount < 7) {
drivetrain.setThrottle(1); // sets drive motors to full speed
}

However, nothing inside the loop changes loopCount. It’s always 0, so loopCount < 7 is always true, and the robot never stops driving!

To fix it, update loopCount inside the loop so the condition eventually becomes false:

int loopCount = 0;
while (loopCount < 7) {
drivetrain.setThrottle(1); // sets drive motors to full speed
loopCount++;
}

Now loopCount increases by 1 every iteration, and the loop stops after 7 iterations.

Note

When you start writing FRC robot code, you’ll find that some code is meant to keep running for as long as the robot is on, such as commands. We’ll cover how that works in Stage 1.

Exercise

Now, you can dive straight into Loops.java! Follow the comments and write the appropriate code to achieve the objectives.