while

Figure 182. A while loop Slide presentation
final int repetitions = scan.nextInt(); ❶     
int loopCount = 0; ❷

while (loopCount < repetitions ❸) {            
   IO.println("Do not copy!"); ❹
   loopCount++; ❺
}
Do not copy!
Do not copy!
Do not copy!
A while loop

❶

The code block will be repeated this number of times.

❷

Helper variable keeping track of repetitions.

❸

Condition to be checked prior each execution.

❹

Code block of statement(s) to be repeated.

❺

Helper variable being incremented during each iteration.


Figure 183. Combining increment and termination condition Slide presentation
Code Execution
IO.print("Enter repetitions: ");
final int repetitions = scan.nextInt();
int loopCounter = 0;

while (loopCounter++ < repetitions) {
   IO.println("Do not copy!");
}
Enter repetitions: 3   
Do not copy!
Do not copy!
Do not copy!

Figure 184. while syntax Slide presentation
while (booleanExpression)
   (block | statement)
This closely resembles:
if (booleanExpression)
   (block | statement)

Figure 185. Empty while body Slide presentation
int threeSeries = 1;

while ((threeSeries *=3 ) < 100);

IO.println(threeSeries);

Exercise:

  • Find the block or statement mentioned in the previous syntax diagram.

  • What output do you expect?


exercise No. 75

Generating square numbers

Q:

Write an application printing the first ten square numbers. The output should look like:

The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
The square of 6 is 36
The square of 7 is 49
The square of 8 is 64
The square of 9 is 81
The square of 10 is 100

A:

int counter = 0;

while (counter++ < 10) {
  IO.println("The square of " + counter + " is " + counter * counter);
}

exercise No. 76

Calculating factorial

Q:

The factorial n ! of a given integer n is being defined as the following product:

n ! = n × ( n - 1 ) × ⋯ × 2 × 1

In addition the factorial of zero is being defined as:

0 ! = 1

Implement a int factorial (int value) {...} method computing factorials, e.g.:

Code
IO.println(factorial(0));
IO.println(factorial(1));
IO.println(factorial(2));
IO.println(factorial(3));
IO.println(factorial(4));
Result
1
1
2
6
24

A:

int factorial (int value) {
    int product = 1;
    while (1 < value) {
        product *= value;
        value--;
    }
    return product;
}

exercise No. 77

Mitigating factorial overflow problems

Q:

The solution of Calculating factorial yields:

Code
IO.println("10: " + factorial(10));
IO.println("11: " + factorial(11));
IO.println("12: " + factorial(12));
IO.println("13: " + factorial(13));
IO.println("14: " + factorial(14));
IO.println("15: " + factorial(15));
IO.println("16: " + factorial(16));
IO.println("17: " + factorial(17));
Result
10: 3628800
11: 39916800
12: 479001600
13: 1932053504
14: 1278945280
15: 2004310016
16: 2004189184
17: -288522240

Actually 12! is the last correct value here. 13! = 13 × 12! equals 6227020800 but we see just 1932053504. Whats wrong here? Provide an enhanced solution.

A:

This is (again) an overflow problem. The largest possible int value is 2 31 - 1 or 2,147,483,647. Thus 479,001,600 or 13! still fits in nicely but 6,227,020,800 corresponding to 14! does not. We may use long instead:

long factorial (int value) {
    long product = 1;
    while (1 < value) {
        product *= value;
        value--;
    }
    return product;
}

This works well until including 20! and fails again for long overflow reasons starting from 21! :

18: 6402373705728000
19: 121645100408832000
20: 2432902008176640000
21: -4249290049419214848