© We Can Code IT, LLC
Looping means to repeat something. This another means of controlling flow within a program. if/else
and switch/case
statements focus on decision making. Looping statements focus on doing something repeatedly. We call doing something repeatedly iterating.
There are many practical examples of loops:
A while
loop continues to do what you've told it to do while some condition continues to be true.
Usually, we won't use them for counting, but it's simple to illustrate them this way. This loop will count from one to ten:
int count = 1;
while (count < 11) {
System.out.println("The count is " + count);
count++; // remember, this increases the value of count by 1
}
Try it!
We start with the while
keyword:
int count = 1;
while (count < 11) {
System.out.println("The count is " + count);
count++; // remember, this increases the value of count by 1
}
int count = 1;
while (count < 11) {
System.out.println("The count is " + count);
count++; // remember, this increases the value of count by 1
}
Within these parentheses, we find a condition. This evaluates to true or false (boolean
), just like our condition for an if
statement:
int count = 1;
while (count < 11) {
System.out.println("The count is " + count);
count++; // remember, this increases the value of count by 1
}
In this case, we will continue to iterate as long as the value of count
is less than eleven.
Each time our while
statement finds its condition to be true, it will execute the code inside the (wait for it…) code block that follows it, denoted by our friends the curly brackets:
int count = 1;
while (count < 11) {
System.out.println("The count is " + count);
count++; // remember, this increases the value of count by 1
}
The way we read this would be: "While count is less than eleven, we will print its value, then increase its value by one."
A while
statement checks its condition before it runs. A do/while
is similar, but it runs the code at least once before checking the condition. We don't see these as often, since it's always possible to use a while
loop instead, and they're generally easier to understand.
If I wanted to allow a user to ask for help regarding question options (this may remind you of a recent exercise), I may do something like this:
Scanner input = new Scanner(System.in);
String porridgeTemperature;
do {
System.out.println("Please specify porridge temperature.");
System.out.println("Type \"help\" to list options.");
porridgeTemperature = input.nextLine();
if(porridgeTemperature.equals("help")) {
System.out.println("Options: too hot, too cold, just right");
}
} while(porridgeTemperature.equals("help"));
input.close();
Note that since porridgeTemperature
is accessed outside of the curly brackets, we must define it before the curly brackets for the do/while
block.
Infinite loops aren't fun. You're sure to encounter one at some point. That's when you'll become more familiar with the Debug perspective in Eclipse, so that you can kill an errant loop.
These happen when the circumstances never arise to make the condition for your loop become false:
while(true) {
System.out.println("I will run forever!");
System.out.println("(Or until you figure out how to stop me!)");
}
Usually it's not that straightforward, but the concept is the same. If your condition is never false when it is checked by the loop, then the loop never ends.
Bad loops just need to be put down. Let's try it out!
For the following exercises, you'll need to figure out whether a while
or a do/while
loop is most appropriate:
ATM: Prompt the user for a PIN. Display an error message if the user gets it wrong, and prompt again. If the user gets it right, print a congratulatory message.
Sum: Ask the user for an integer. Calculate the sum of the numbers from one to the integer the user entered. Example: If the user enters 6, your program should print 21 (1 + 2 + 3 + 4 + 5 + 6).