program u32;
var max,a,b:integer;
begin
write('the first of the two numbers of which to take the maximum is ');
read(a);
write('the second of the two numbers of which to take the maximum is ');
read(b);
if a<=b then begin
max:=b;
end
else
if a>=b then begin
max:=a;
end;
write('the maximum is ',max);
writeln;
end.

Unit 3.2 simple if's the maximum of two numbers

PROG

take the maximum of two numbers--read in from screen

PED

to see if statements in real PASCAL

CONCEPT



Unit 3.2 simple if's the maximum of two numbers

We have already seen all the syntactic forms that if statements can take in the previous unit.

In this unit, we will see a simple example that will work with a simple if with two conditions. It takes the maximum of two numbers.

It will read in two numbers, put the maximum in the variable max, and finally it will print out max.

There are two significant alternatives

1.   the first number is smaller than the second
2.   the first number is larger than the second 
In the first alternative, we have to assign the second number to max. In the other case, the first number will have to be assigned to max.

Lines 4 to 7 read in a and b which will contain the first and second number, respectively.

Then line 8 will check for alternative 1, the first number is smaller than the second. If that is true, then maximum should be set equal to the second number. We do that in line 9.

Line 12 checks the second alternative, the first number is larger than the second. In that case, we assign the first number, in a, to max. The assignment is in line 13.

Lines 15 and 16 do the final output.

Ah, what about, the case where both numbers are equal. In that case, we don't care which number we assign to maximum. In this special case, either one can be assigned to maximum giving the same result. So, we put "<=" and ">=" in lines 8 and 12 to guarantee that at least something will be assigned to maximum when a and b are equal. (We could have made either one of them, but not both, a strict inequality without the equal sign, and everything would be OK.)