Skip navigation.
Always ask - do I really understand this?

Chapter 9 Hints and Help

QUESTION: I'm having trouble with the Chapter Review Questions. How do I figure out the answers to questions 9, 10 and 11?

ANSWER: Review Questions 9, 10 and 11 are important because they test your understanding of the way control structures (repetition and selection structures) actually work. It's important to step through them carefully and follow the logic exactly as written rather than making assumptions about what the code does. This will help you find logical errors in your code or the code of other programmers.

A good approach to solve these kinds of problems is called "desk-checking". Let's see how this works for Question 9. Take a piece of paper and write the names of each variable:

   $result:
   $count:
   $nextNum:

Now step through the code as if you were the computer running the program and assign or change the value for each variable as required. So first assign 0 to $result since that's what the first statement requires. Then assign 1 to $count since that's the first value that the loop assigns. Inside the loop the first statement is:

   $nextNum= trim(fgets($someFile));

Which means read the first line of the file and assign this number to $nextNum. This could be any number, so let's say it is 5. Your variables now look like this:

   $result: 0
   $count: 1
   $nextNum: 5

The next statement in the loop is an IF structure:

   if ($nextNum > $result)
      $result = $nextNum;

Since $result is 0 and $nextNum is 5, this test is TRUE so we assign $nextNum to $result. So now our variables look like this:

   $result: 5
   $count: 1
   $nextNum: 5

Now we come back to the top of the loop and $count is incremented to 2. So the loop repeats and we read the next number from the file into $nextNum. Let's say this number is 2. Our variables look like this:

   $result: 5
   $count: 2
   $nextNum: 2

We come to the IF test again. This time $nextNum (which is 2) is NOT greater than $result (which is 5) so we leave $result unchanged (we only change $result if the test is TRUE).

We come back to the top of the loop and $count is incremented to 3. So the loop repeats and we read the next number from the file into $nextNum. Let's say this number is 10. Our variables look like this:

   $result: 5
   $count: 3
   $nextNum: 10

We come to the IF test again. This time $nextNum (which is 10) IS greater than $result (which is 5) so we assign the value of $nextNum to $result. Our variables look like this:

   $result: 10
   $count: 3
   $nextNum: 10

We come back to the top of the loop and $count is incremented to 4. So the loop repeats and we read the next number from the file into $nextNum. Let's say this number is 20. Our variables look like this:

   $result: 10
   $count: 4
   $nextNum: 20

We come to the IF test again. This time $nextNum (which is 20) IS greater than $result (which is 10) so we assign the value of $nextNum to $result. Our variables look like this:

   $result: 20
   $count: 4
   $nextNum: 20

We come back to the top of the loop and $count is incremented to 5. So the loop repeats and we read the next number from the file into $nextNum. Let's say this number is 15. Our variables look like this:

   $result: 20
   $count: 5
   $nextNum: 15

We come to the IF test again. This time $nextNum (which is 15) is NOT greater than $result (which is 30) so we leave $result unchanged. Our variables look like this:

   $result: 20
   $count: 5
   $nextNum: 15

We come back to the top of the loop and $count is incremented to 6. But the loop test specified that the loop only repeats while $count <= 5 so the loop ends. The file is closes and the program prints $result which is 20.

So the question is, what did the program do? Did it add the five numbers in the file, find the highest value, find the lowest value, find the value of the first number in the file, or find the value of the first number in the file? The code is a little different in Questions 9, 10 and 11 so your job is to trace through the code in each question to see what it actually does..

QUESTION: How do you know how many times a FOR loop will repeat?

ANSWER: A FOR loop contains a heading that controls the loop, for example:

   for ($count = 1; $count <= 5; $count = $count + 1)

That means the first time the loop repeats $count has the value 1, the loop repeats as long as $count <= 5, and each time the loop executes 1 is added to $count.

In other words the $count variable is used to control the loop. Since it starts at 1 and since the loop runs until $count has a value above 5, and since 1 is added to $count each time the loop executes, this loop will repeat 5 times:

   $count contains 1 so the code in the loop is executed
   Now 1 is added to $count
   $count contains 2 so the code in the loop is executed
   Now 1 is added to $count
   $count contains 3 so the code in the loop is executed
   Now 1 is added to $count
   $count contains 4 so the code in the loop is executed
   Now 1 is added to $count
   $count contains 5 and the code in the loop is executed
   Now 1 is added to $count

When $count increases to 6, the test $count <= 5 is no longer true so the loop does not repeat and the program moves on.

As a second example, note that when you create a FOR loop you can set it up any way you want depending on what you need the loop to do, for example:

   for ($evenNumber=2; $evenNumber <= 15; $evenNumber=$evenNumber + 2)

In this case the loop is controlled by a variable named $evenNumber which begins with the value 2. The loop will run as long as $evenNumber <= 15, and each time the loop executes, 2 is added to $evenNumber. So in this case the loop will repeat 7 times:

   $evenNumber contains 2 so the code in the loop is executed
   Now 2 is added to $evenNumber
   $evenNumber contains 4 so the code in the loop is executed
   Now 2 is added to $evenNumber
   $evenNumber contains 6 so the code in the loop is executed
   Now 2 is added to $evenNumber
   $evenNumber contains 8 so the code in the loop is executed
   Now 2 is added to $evenNumber
   $evenNumber contains 10 so the code in the loop is executed
   Now 2 is added to $evenNumber
   $evenNumber contains 12 so the code in the loop is executed
   Now 2 is added to $evenNumber
   $evenNumber contains 14 so the code in the loop is executed
   Now 2 is added to $evenNumber

When $evenNumber increases to 16, the test $evenNumber <= 15 is no longer true so the loop does not repeat and the program moves on.