Next: Other loop statements Up: Unit 06 Previous: Loop scheme for characteristic

## Loop scheme for characteristic values in a set: maximum in the general case

Let us consider again the problem of finding the maximum of a set of reals in input. This time we make no assumption, i.e.,:

• the set of reals could be empty, and
• we do not know the interval over which the reals range.

In this case, a possible solution is the following.

```String s;          // current string in input
double r;          // current real
double max = 0;    // current maximum
boolean found;     // indicates whether at least one value was input

found = false;
s = JOptionPane.showInputDialog("Input a real");
while (s != null) {
r = Double.parseDouble(s);
if (!found || (r > max)) {
max = r;
found = true;
}
s = JOptionPane.showInputDialog("Input a real");
}

if (found)
System.out.println("maximum = " + max);
else
System.out.println("empty set of values");
```

Note:

• If the set of reals contains at least one value, then the body of the while loop is executed at least once. At the first iteration, since the value of found is equal to false, the condition of the if statement is true and the variable max is initialized to the value of the first real that has been input. In the subsequent iterations of the loop, since found is then equal to true (and hence !found is equal to false), the condition of the if statement will be true only if the current value of r is greater than max.
• If the set of input reals is empty, then the body of the loop will not be executed at all, and found keeps its value false.
• The initialization max = 0; would not be necessary for a correct execution of the program. Indeed, the expression (f > max) is evaluated only in the case where found is equal to true, and this happens only if the statement max = f;, which initializes max, has been executed. However, the Java compiler, which performs some checks on the initialization of variables, is not able to detect such a condition, and hence requires that max be initialized before evaluating the condition of the if statement.

There are other means for determining the maximum in the general case discussed here. For example, we could exploit the fact that the wrapper class Double provides the constant MAX_VALUE holding the maximum value a double can have, and initialize the maximum to -MAX_VALUE. We would anyway need a boolean variable to distinguish the case where the user has input no number at all from the case where the user has input just -MAX_VALUE.

```String s;          // current string in input
double r;          // current real
double max;        // current maximum
boolean found;     // indicates whether at least one value was input

found = false;
max = -Double.MAX_VALUE;

s = JOptionPane.showInputDialog("Input a real");
while (s != null) {
r = Double.parseDouble(s);
found = true;
if (r > max) max = r;
s = JOptionPane.showInputDialog("Input a real");
}

if (found)
System.out.println("maximum = " + max);
else
System.out.println("empty set of values");
```

Next: Other loop statements Up: Unit 06 Previous: Loop scheme for characteristic