Home Loops
On looping
Java lets you "loop" through values in several different ways. Though many of these looping constructs are, to a certain extent, mechanically interchangeable, they each have a different meaning and use. Which looping construct you use will imply multiple things about your code. Use the one that best fits your overarching intent.
In CSE 142, we learn about for
loops and while
loops. Note that
other kinds of loops such as foreach
loops are first introduced in CSE 143,
count as advanced material in CSE 142, and so should not be used.
For loops
Use a for
loops when you know exactly how many times you need to loop
before entering the loop. Do not use loops when you end up always looping only once.
Another way of thinking about for loops is to consider them as finite or definite loops – you know precisely how many loops you need.
For example, say you wanted to print out a square of side length n
out to the
console. This is a perfect example of when to use a for loop:
public static void printSquare(int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print('*'); } System.out.println(); } }
We know before starting the loop exactly how many times we need to repeat the operation:
exactly n
times.
Contrast this to the behavior of while loops, which is covered in the following subsection.
While loops
Use a while
loops when you do NOT know exactly how many times you need
to loop, and can only find out in the middle of looping – when you need a potentially
infinite or indefinite loop.
An example of when you would use a while loop is if you want to perform some operation on every line in a file. Because we have no way of knowing how many lines are in a file and therefore how many times we need to loop before opening it, you should use a while loop:
public static void printLines(Scanner input) { int lineNumber = 0; while (input.hasNextLine()) { lineNumber += 1; String line = input.nextLine(); System.out.println("Line " + lineNumber + ": " + line); } }
Do note that we could theoretically abuse for loops and accomplish the exact same thing as the above using for loops in Java:
public static void printLines(Scanner input) { for (int lineNumber = 1; input.hasNextLine(); lineNumber++) { String line = input.nextLine(); System.out.println("Line " + lineNumber + ": " + line); } }
Although this is slightly shorter, it's not good style because it uses for loops for something it was not semantically meant to do, which confuses the underlying intent of your code. The only reason you could do this is because Java decided to design their for loop syntax in such a way that it gives you a little too much flexibility. For loops do not necessarily work in the same way in other programming languages, so it's good to not get into the habit of relying on this particular quirk.
Do-while loops
We do not expect you to know about or use do-while
loops in CSE 14x –
consider them optional material. You may use do-while
loops once we have taught
you about regular while
loops, if you wish.
Due to time constraints, we typically do not have time to teach you what a
do-while
loop is, or how it works. If you're curious to learn more, chapter
5.1 in your textbook should contain more information.
Because a do-while
loop shares many similarities with a regular
while
loop, we do permit you to use them once we have formally introduced what
a while
loop is.