Programming With Loops - Dutton-Christian-Robotics/Learning-to-Code GitHub Wiki
One way to implement the principle of "Don't Repeat Yourself" (DRY) is to find places in your code where you repeat the same lines over and over again and to replace that repetition with a loop.
In programming, a "loop" is a type of statement that repeats a block of code. Java has several different kinds of code. While they all do the same thing—repeat code—they differ in how they decide whether to keep repeating or not.
The For Loop
For loops work best when you have an exact number of times that your code should repeat. Here's an example:
for (int loopCount = 0; loopCount < 10; loopCount++) {
// code that you put here will repeat
}
You'll notice that within the parentheses of the for loop are three separate statements, with semicolons separating them:
- The first statement creates a variable and sets its value. The standard way to use this variable is to keep a count of how many times the loop has repeated so far. Because this statement is both a variable declaration and initialization, it needs to start with a variable type (int) and then include a starting value.
- The second statement is a condition that decides if the loop should repeat another time. Whatever this expression is should return a boolean value.
- A true value will cause the loop to repeat.
- A false value will end the loop.
- The final statement determines what happens to the counter variable at the end of the loop. The two plus signs after the variable name tell Java to increase the variable's value by 1.
The While Loop
Whiles loops and for loops accomplish the same thing: they repeat a section of code.
int loopCount = 0;
while (loopCount < 10) {
// code that you put here will repeat
loopCount = loopCount + 1;
}
All that's required to create a while loop is one expression that returns a boolean value. In the example here, we're using the while loop exactly like a for loop, by incrementing and checking a variable that holds a count of how many times we've been through the loop.
While loops look much simpler than for loops. This simplicity can be a problem. If you're using a while loop in this manner, you'll need to remember to both declare and initialize the counter variable, as well as to increase its value each time. Forgetting either will result in errors or unexpected behavior.
Because they're more flexible, there are other ways to use a while loop that go beyond simply counting times through the loop.
while (opModeIsActive()) {
// everything that should happen during your robot's OpMode goes here and will repeat
}
The only requirement for a while loop is checking an expression that returns a boolean value. You'll see a while loop like this in most FTC opmodes. opModeIsActive() is a method that FTC gives to every opmode class which allows us to check if the opmode should keep running, or if it has been stopped in any way. Using the return value of this method call, we can create a loop that will keep repeating an uncertain number of times—but which will end when it needs to.
The Do...While Loop
The do...while loop is very similar to the while loop. The while loop checks its condition before it repeats the body of the loop. The do...while loop checks that condition afterwards. Why is that important? We'll get to that in a moment.
int loopCount = 0;
do {
// code that you put here will repeat
} while (loopCount < 10);
As with while loops, if you're using it for simple counting you need to remember to declare and initialize the counter variable, as well as increment it.
How Many Times Will A Loop Repeat?
Part of choosing the right type of loop involves knowing how many times a particular type of loop is guaranteed to repeat.
- For loop: Because a for loop checks its boolean condition before it repeats anything, a for loop is not guaranteed to ever repeat. If the condition is false the very first time it checks, then the loop will repeat exactly 0 times.
- While loop: The situation here is exactly the same as with a for loop. Because the condition is checked before repeating, if it's false to begin with the loop may never repeat at all.
- Do...While loop: This type of loop repeats its block of code before it checks its condition. So even if the condition evaluates to false on its first run, the loop will have run exactly one time. Because of this, the do...while loop is the only kind of loop that is guaranteed to run at least one time.