0 index
1 Loops
2 Sentry variables and Initialization
3 Example
4 Preventing Endless Loops
5 Example
6 Endless Loops
7 The For Loop
8 For Loop
9 Example

outline
created using slideshow.cgi by Andy Harris















CSCI N301 Fundamental CS Concepts: n301/loops
1. Loops

  • Allows certain parts of code to repeat based on the status of a condition

    The while statement.
    The while statement checks the condition and repeats the code if the condition is TRUE.



































CSCI N301 Fundamental CS Concepts: n301/loops
2. Sentry variables and Initialization
  • Start by creating a variable
  • Giving the variable an initial value is initializing the variable
  • Variables used in loops are often known as sentry variables



































CSCI N301 Fundamental CS Concepts: n301/loops
3. Example
  • //Note that I initialized stillRacing incorrectly
      var stillRacing = "N";

    //This condition will only be checked once, and it will be false
      while (stillRacing == "Y"){
       //the body of this loop will NEVER be activated
       alert ("zooming around the track.");
       //see if we're still on
       stillRacing = prompt("Is the race still on? (Y/N)");
      }//end while

    alert ("Nice race!!");



































CSCI N301 Fundamental CS Concepts: n301/loops
4. Preventing Endless Loops
  • Inside a loop, we MUST ensure that there is some way of exiting the loop
  • We have to make it possible for the sentry variable to change values



































CSCI N301 Fundamental CS Concepts: n301/loops
5. Example
  •  var stillRacing = "Y";

     while (stillRacing == "Y"){
       alert("zooming around the track");

       //forgot to check if we are still racing!!!

     }// end while
     alert ("Nice race!!");
  • This program will go on FOREVER!!!



































CSCI N301 Fundamental CS Concepts: n301/loops
6. Endless Loops
  • The condition statement checks to see if the condition is TRUE
  • In the previous example, the condition is true
  • Because the condition is true, the body of the loop still thinks we are "zooming around the track"
  • Whenever you make a loop, it is your obligation to ensure that the sentry variable is changed so it is possible to exit the loop



































CSCI N301 Fundamental CS Concepts: n301/loops
7. The For Loop
  • Variation of the loop we have already used
  • Difference - we have added a counter
  • Needs four pieces of information:
     - name of counting variable
     - starting value of the variable
     - ending value of the variable
     - how to increment the variable



































CSCI N301 Fundamental CS Concepts: n301/loops
8. For Loop
  • The for statement needs three parts:
     1.  initialize the variable
     2.  a condition
     3.  how to increment the variable
  • Once the loop is set up, we don't have to worry about it in the body - it automatically increments



































CSCI N301 Fundamental CS Concepts: n301/loops
9. Example
  • var lap = 1;

    for (lap = 1; lap <= 10; lap ++){
       alert ("Now on lap " + lap);
    }// end for

    alert ("Good race!!");



































outline

Loops

Sentry variables and Initialization

Example

Preventing Endless Loops

Example

Endless Loops

The For Loop

For Loop

Example