Next: Loop scheme for characteristic Up: Unit 06 Previous: Loop scheme for characteristic

## Loop scheme for characteristic values in a set: maximum of a non-empty set

Let us consider the problem of finding the maximum of a set of reals in input. Assume that:

• the set of reals contains at least one value, but
• we do not know the interval over which the reals range, in the sense that in the program we cannot denote the extremes of the interval.

```String s;       // current string in input
double r;       // current real
double max;     // current maximum

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

System.out.println("massimo = " + max);
```

Note: Since we know that the set of integers cannot be empty, we can read the first real and use it to initialize the maximum value with it before we start the loop.

Next: Loop scheme for characteristic Up: Unit 06 Previous: Loop scheme for characteristic