The order of the comparisons in "their code" is done for a reason. The 4 cases to consider, as you know, are:
1) guess == number
2) guess < number
3) guess > number
4) guess == "quit"/"exit".
Lets trace through an example. Say number is 5 and we guess 4. 1) isnt true, so we check the next condition. Now, since guess is not equal to number, either guess is less than number or greater than number. That is, there is no other possibility! Since 4 < 5 is true, it asks for another number. Lets say we input 6. Then 1) is false, 2) is false which means there is only one possibility, and that is that 3) is true, so it asks for another number. Now, say we enter "exit" or "quit". 1) is obviously not true, so we check 2). Depending on how strings are compared to numbers in Perl (or their order in ASCII) either 2) or 3) will be true. That is, control never reaches 4).
To reiterate, since the expected input is a number, there are always only 3 possibilities: ==, <, >, and exactly one of them will be true. So, again, it is not possible for control to reach the if statement in 4). So the order of checking is important. You must check conditions 1) and 4) first (so either 1) then 4), or 4) then 1), it shouldnt matter). The order of conditions 2) and 3) do not matter.
|