0 index
1 Loops
2 Sentry variables and Initialization
3 Math Quiz
4 Preventing Endless Loops
5 Example
6 Endless Loops
7 The For Loop
8 For Loop
9 Ten Lap Race
10 Backwards Race
11 Mad lib

outline
created using slideshow.cgi by Andy Harris















CSCI N301 Fundamental CS Concepts: n301/cs19loops
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/cs19loops
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/cs19loops
3. Math Quiz
  • Ask the user a simple math question. Continue asking the question until the user gets the answer correct.



































CSCI N301 Fundamental CS Concepts: n301/cs19loops
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/cs19loops
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/cs19loops
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/cs19loops
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/cs19loops
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/cs19loops
9. Ten Lap Race
  • Simulate a race with a program that says "now on lap: " for ten laps.



































CSCI N301 Fundamental CS Concepts: n301/cs19loops
10. Backwards Race
  • Write a program that goes from ten to one



































CSCI N301 Fundamental CS Concepts: n301/cs19loops
11. Mad lib
  • Try this one on your own.
  • Ask the user for a number of words (favorite color, name of a person, adjective). Use these words in a story.



































outline

Loops

Sentry variables and Initialization

Math Quiz

Preventing Endless Loops

Example

Endless Loops

The For Loop

For Loop

Ten Lap Race

Backwards Race

Mad lib