6.1 while Loops
Key terms: compound assignment operator, increment/decrement operator, syntactic sugar
Loops are programming structures that enable a block of statements to be executed repeatedly as long as a specified condition is satisfied. There are three looping structures in Java. They are equivalent in the sense that whatever you can do with one you can do with another, but one may be more convenient than another depending on the situation. Loops are essential for almost all real-world programming problems.
The programs in this section use several shorthand operators in loop bodies. These are introduced first for clarity.
6.1.1 Syntactic Sugar
Java’s arithmetic compound assignment operators, illustrated in the code fragment below, provide
a convenient syntax for combining an arithmetic operation with an assignment. The += operator is
pronounced plus gets, -= is pronounced minus gets, and so on.
x += 8; // same as x = x + 8;
x -= 8; // same as x = x - 8;
x *= 8; // same as x = x * 8;
x /= 8; // same as x = x / 8;
x %= 8; // same as x = x % 8;
The += operator can also be used to combine string concatenation and assignment:
String animal = "ant";
animal += "elope";
System.out.println(animal); // antelope
Incrementing or decrementing an int variable by one is common, especially in loops. Compound
assignment operators can be used for this, but Java provides an even simpler syntax with
the increment and decrement operators (++ and --):
int x = 3;
int y = 8;
x++; // same as x += 1;
y--; // same as y -= 1;
System.out.println(x + " " + y); // 4 7
The increment and decrement operators can be written in two forms. The postfix form is shown
above. In prefix form, the operator precedes the operand (as in ++x and --y). When a statement
consists of an increment or decrement operation alone, it makes no difference which form is used,
but when combined with other actions in a single statement, the choice matters. Consider, for
example, the following:
int x = 3;
int y = x++;
System.out.println(x + " " + y); // 4 3
int z = 8;
System.out.println(--z); // 7
In the second line, two things are happening: x is being incremented and y is being assigned the
value of x. But which happens first? With postfix form, the increment operation follows the
assignment. In prefix form, the increment operation would precede the assignment, in which case y
would get 4 instead of 3.
In the last line, the prefix form of the decrement operator is applied to z, so the variable is
decremented before its value is passed to println.
The increment/decrement and compound assignment operators are examples of what programmers call syntactic sugar: they do not add any new capabilities, but they do provide a convenient shorthand for existing syntax.
6.1.2 while Loops
A while loop resembles an if statement. For the sake of comparison, the general syntax is shown
for both below.
if (E) {
// conditionally executed code
}
while (E) {
// conditionally executed code
}
The if statement either executes the body or it does not, depending on the Boolean expression E.
With the loop, however, execution continues after this initial step by returning to the evaluation
of E for another possible execution of the body.
Listing 6.1.2a uses a while loop to calculate the sum of positive integers up to a user-specified
limit.
Listing 6.1.2a - Summer.java
package chap06.sect1;
import java.util.Scanner;
/**
* Calculates the sum of consecutive positive integers up to a user-specified limit.
*
* @author Drue Coles
*/
public class Summer {
public static void main(String[] args) {
System.out.print("Enter a positive integer: ");
Scanner in = new Scanner(System.in);
final int limit = in.nextInt();
int sum = 0;
int next = 1;
while (next <= limit) {
sum += next;
next++;
}
System.out.printf("1 + 2 + 3 + ... + %,d = %,d %n", limit, sum);
}
}
Output 6.1.2a
Enter a positive integer: 100
1 + 2 + 3 + ... + 100 = 5050
Listing 6.1.2b uses a while loop to simulate the Gambler's Ruin, a statistical concept concerning
a simple coin-flipping game. The details are explained in the class documentation.
Listing 6.1.2b - GamblersRuin.java
package chap06.sect1;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
/**
* Prompts the user for an initial balance and simulates the gambler's ruin: the gambler wins a
* dollar for heads and loses a dollar for tails, and continues flipping until going broke. The
* program outputs the number of coin flips and the maximum balance.
*
* @author Drue Coles
*/
public class GamblersRuin {
public static void main(String[] args) {
System.out.print("Enter initial balance: $");
Scanner in = new Scanner(System.in);
int balance = in.nextInt();
int max = balance;
int coinFlips = 0;
// simulate coin flips until balance reaches 0
ThreadLocalRandom rand = ThreadLocalRandom.current();
while (balance > 0) {
if (rand.nextBoolean()) {
balance++;
} else {
balance--;
}
coinFlips++;
if (balance > max) {
max = balance;
}
}
System.out.printf("Number of flips: %,d %n", coinFlips);
System.out.printf("Maximum balance: $%,d %n", max);
}
}
Output 6.1.2b
Enter initial balance: $100
Number of flips: 4,068
Maximum balance: $130
Results from the program will vary since the game is a random process.
6.1.3 do-while Loops
A do-while loop is a variant of the while loop with syntax as shown below.
do {
// conditionally executed code
} while (E);
The body of a do-while loop is executed before the condition to continue (E) is evaluated,
but otherwise do-while behaves like while. It is convenient when it is necessary to ensure
that the body is executed at least once.