Please note - all script results will be this color
Task 1:
Taking the examples from the lecture for the for and while loops, type them into the faith code, and run them.
What's the difference in the display between the for and while loops?
The FOR loop output begins at 0 and ends at 2. The WHILE loop output begins at 1 and ends at 3.
Why is there a difference?
The FOR loop evaluates the CONDITION expression. If the CONDITION is TRUE the rest of the statements in the loop are evaluated and then the ADJUSTment is made. The FOR loop CONDITION begins with 0 (TRUE)(output). 0 is then iterated to 1. The new CONDITION is 1(TRUE)(output). 1 is then iterated to 2. The new CONDITION is 2(TRUE)(output). 2 is iterated to 3. The new CONDITION is 3(FALSE) -- the loop is DONE!
The WHILE loop evaluates the CONDITION expression. As long as the CONDITION is true - the rest of the statement is evaluated. The WHILE loop CONDITION begins with 0 (TRUE) and is therefore iterated to 1 (the output). This places 1 (TRUE) as the CONDITION in the next iteraton of the loop, which evaluates to 2 (the output). The new CONDITION is 2 (TRUE) which will iterate to 3(the output). The new CONDITION is 3 (FALSE) -- the loop is DONE!
Task 2:
Make up some numeric condition you can test for and write an if/else statement that checks for that condition and displays a result. Use document.write() in your statement and have your program write two different messages: One message if the condition is met and another if it is not. To do this, you will need two if/else statements. One to display result if the condition is met (true), and one to display the result if the condition is not met (false).
Task 3:
Using document.write() to display the results, write a for loop that will display all odd numbers that are greater than 0 and less than 10 in ascending order.
Then: write a for loop that will display all odd numbers that are less than 10 and greater than 0 and use document.write() to display the results in descending (reverse) order.
Task 4:
Write a while loop that will display all the even numbers that are greater than 0 and less than 11 in ascending order.
Then: write a while loop that will display all even numbers that are less than 11 and greater than 0 and use document.write() to display the results in descending (reverse) order.
Task 5 - Review Questions:
1 - Please tell me in your own words what the difference is between a for loop and a while loop.
A FOR loop contains a compound argument that contains a CONDITION expression and an ADJUST expression. If the CONDITION is true, the ADJUST is performed. The loop will iterate as long as the CONDITION evaluates to TRUE. Once the CONDITION is FALSE the loop stops. FOR loops work well for iterations that have a set or known number. A WHILE loop works better for iterations that are of a unknown number. The WHILE loop contains an expression in a simple argument. As long as that expression/CONDITION is TRUE the loop continues. Once the expression/CONDITION returns as FALSE, the loop stops.
2 - I want to know if the water is ready to steep my tea. Would it be preferable to use a for loop or a while loop to check the temperature of the water?
Since you are not sure of how long the water will take to reach temperature (it all depends on initial water temperature, atmospheric pressure, the molecular makeup of the vessel containing the water and it's heat loss gradient, yadda yadda yadda --) it would be better to use a WHILE loop.
3 - To fix Ben a gourmet Indian dinner, I know that I'll need 457 different items, 53 pans and Emeril Lagassi. Well, I know I can't afford Emeril Lagassi and I don't have 53 pans. But there are 457 items on my list. Would it be preferable to use a for loop or a while loop to check whether I have each item on my list?
You would use a FOR loop - probably in a form that would generate a list - which is not something that I would be too excited to code. Now if you get the list down to a more manageable number - say 20 - 30.......
My sister-in-law can whip up a pretty decent Indian meal - maybe you could hire her :).
Bonus Task:
Use several of the comparison operators to test for a condition and display the results.