if-statementmaxpascalturbo-pascal

if statement and the maximal value in Pascal


Looking at this code:

enter image description here

I thought that the program won't give the right maximal value if z>x and y>x, but, to my surprise, it did give the correct value. Why is that? Did the program compare Y and Z and gave the biggest value without me ordering it to do so?


Solution

  • After the first if statement, max holds the maximum of x and y. This maximum is then compared with z in the second if statement. You don't need to compere y and z directly due to the transitive quality of the > operator.