This is a detailed explanation of the Question 10 on CompSci 4 Test 1 from Fall 2006. The question is talking about 2 example runs of the world.Mystery code. Each part, part "A" and part "B" runs the same code, but gives it a different input. So, all you have to do for a question like this is put the numbers into the code and *pretend that you're the computer*. Basically, you just run through the code yourself - *line by line, one at a time* - until you get a result. The first line of code says something like: "if either (num1 > num2) or (num1 > num3) or both" ...so just evaluate if this statement is TRUE or FALSE given the values for the 3 parameters: num1, num2, num3. Look at the first part: num1 > num2. In part "A" we have the values: num1 = 1 num2 = 2 num3 = 5 So, "num1 > num2" is equivalent to: 1 > 2... that's false! (Remember, we just replace "num1" with it's value "1" and num2 with its value "2". So now look at the second part: "num1 > num3" is equivalent to "1 > 5"... that's false! So now what about the whole statement: "if either (num1 > num2) or (num1 > num3) or both" is the same as: "if either (1 > 2) or (1 > 5) or both" is the same as: "if either FALSE or FALSE or both" is the same as: "if FALSE" So, this whole statement evaluated to "FALSE". If this statement was true, then we would execute the code right under this if-statement line. Since it's false, then we move to the code under the "else" statement instead. In this case, that's a single line of code: "Return num2" Remember that "num2" is a parameter. Thus, we look at it's value num2 = 2. So, in this case the function returns the value of 2. Now, if we run the function on a different set of inputs then we'll probably get a different outcome. The second time through the function we have the inputs as: num1 = 4 num2 = 5 num3 = 3 So, now what happens when we execute the first line of the function? We get the following: "if either (num1 > num2) or (num1 > num3) or both" is the same as: "if either (4 > 5) or (4 > 3) or both" is the same as: "if either FALSE or TRUE or both" is the same as: "if TRUE" Note that all I'm doing is replacing the parameters - num1, num2, and num3 - with the values that they were given. In this case, one of our comparisons between the numbers came out TRUE so we move to the code just under the if-statement line. That would be the code line: "if both num2 != 5 and num3 > 0" What do we do with this? (You know this one.) ... ... ... we replace the parameters with the values they were given! "if both num2 != 5 and num3 > 0" is the same as: "if both 5 != 5 and 3 > 0" is the same as: "if both FALSE and TRUE" is the same as: "if FALSE" Notice that this statement had "if both ... and ... " instead of "if either ... or ... ". This time one evaluation was "FALSE" and one was "TRUE"... which means the whole thing was FALSE. It's just like English. If you have a "both ... and ... " you need both parts to be true. If you have an "either ... or ... " then you're happy if either portion is true. In this case we had a "both ... and ... " so the total statement was FALSE. Thus, we move to the code under the "else" statement. In this case that's the line: "Return 0". Notice that it's a different else statement than what we used in the first example from question part *A*. Each if-statement line has its own unique matching else-statement line. You can tell which matches up with which one pretty easily by just looking at the box borders in the picture. The if-statement and else-statement lines will be in the same box. So now you've got a couple examples worked out for you. Try running this code (in your head) with a couple more examples just to make sure you have everything figured out. To get you started, how about: (C) Mystery num1 = 1, num2 = 2, num3 = 3 and (D) Mystery num1 = 4, num2 = 10, num3 = 9 ------------------------------------------------------------------------ Written By: Sam Slee 10-1-07