Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 201
Printing Elements in Reverse
ОглавлениеLet's say you wanted to print the same first five numbers from zero as we did in the previous section, but this time in reverse order. The goal then is to print 4 3 2 1 0
.
How would you do that? An initial implementation might look like the following:
for (var counter = 5; counter> 0; counter--) { System.out.print(counter + " "); }
While this snippet does output five distinct values, and it resembles our first for
loop example, it does not output the same five values. Instead, this is the output:
5 4 3 2 1
Wait, that's not what we wanted! We wanted 4 3 2 1 0
. It starts with 5
, because that is the first value assigned to it. Let's fix that by starting with 4
instead:
for (var counter = 4; counter> 0; counter--) { System.out.print(counter + " "); }
What does this print now? It prints the following:
4 3 2 1
So close! The problem is that it ends with 1
, not 0
, because we told it to exit as soon as the value was not strictly greater than 0
. If we want to print the same 0
through 4
as our first example, we need to update the termination condition, like this:
for (var counter = 4; counter>= 0; counter--) { System.out.print(counter + " "); }
Finally! We have code that now prints 4 3 2 1 0
and matches the reverse of our for
loop example in the previous section. We could have instead used counter > -1
as the loop termination condition in this example, although counter >= 0
tends to be more readable.
For the exam, you are going to have to know how to read forward and backward for
loops. When you see a for
loop on the exam, pay close attention to the loop variable and operations if the decrement operator, --
, is used. While incrementing from 0
in a for
loop is often straightforward, decrementing tends to be less intuitive. In fact, if you do see a for
loop with a decrement operator on the exam, you should assume they are trying to test your knowledge of loop operations.