FIT100: Fluency With Information Technology

Lab 10  Iteration Exercises

 

Reference

This lab assumes you are familiar with Chapter 20, “Once Is Not Enough” of FIT

Goal

The goal of this lab is to practice writing iteration statements and become familiar with how the for-statement works.  We will also place images, which should be helpful on Project 2.

Preparation

Review the main features of the for-statement from Chapter 20, including the details of the World Famous Iteration.

 

Also, set up a web page in which to practice your JavaScript statements. The page should have the usual HTML plus the JavaScript tags in it.

 

Exercises

 

  1. Declare an iteration variable (most programmers will pick i and that is what the examples assume). Write a loop that iterates 5 times, repeating the statement:

 

document.write("The iteration variable "

 + "has the value " + i + "<br>");

 

If the loop’s initialization sets i to 0 and limits the iteration to numbers less than 5 and increments by 1 each time, then the result should be

 

 

  1. Change the loop’s <continuation> test to be i<10. Show the result.  Change the <next> field of the for-statement to increment by 2. Show the result.
  2. Change the for-statement to count down from 10 to 1 by 1. The <next> calculation must subtract 1 on each cycle. Show the result. 
  3. Save the following GIF on your desktop and change it’s name to star.

  1. Revise your iteration so that it just loops five times. (It doesn’t matter where you start, stop or the amount you change the iteration variable by, as long as the loop iterates only five times. Make the following statement be the statement that is repeated:

 

document.write("<img src='star.gif'>");

 

The result should look like the following

 

 

 

  1. Think about how this loop works and what it is placing in the HTML file of the web page.  What is happening?  Ask your TA if you are not sure.

 

These exercises are useful for the last parts of Project 2. Now use what you have learned to work on Project 2.