## A Web-Based Introduction to Programming## Essential Algorithms, Syntax, and Control Structures Using PHP, HTML, and MariaDB/MySQL, Fifth Edition | |

Always ask - do I really understand this? | |

HOME STUDENTS: 5th Edition 4th Edition 3rd Edition 2nd Edition 1st Edition INSTRUCTORS FEATURES TESTIMONIALS AUTHOR CONTACT |
## Chapter 9 Hints and Help
A good approach to solve these kinds of problems is called ```
$result:
```
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
```
The next statement in the loop is an IF structure: ```
if ($nextNum > $result)
```
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
```
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
```
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
```
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
```
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
```
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
```
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
```
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
```
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..
```
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
```
When $count increases to 6, the test $count <= 5 is no longer true so the loop does not repeat and the program moves on.
```
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
```
When $evenNumber increases to 16, the test $evenNumber <= 15 is no longer true so the loop does not repeat and the program moves on. |

Copyright © 2008 - 2024 Mike O'Kane All Rights Reserved |