1 / 10

JavaScript: Control Statements I

JavaScript: Control Statements I. 7.8 Counter-Controlled Repetition. Counter-controlled repetition Often called definite repetition, because the number of repetitions is known before the loop begins executing A total is a variable in which a script accumulates the sum of a series of values

spadilla
Download Presentation

JavaScript: Control Statements I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JavaScript: Control Statements I

  2. 7.8 Counter-Controlled Repetition • Counter-controlled repetition • Often called definite repetition, because the number of repetitions is known before the loop begins executing • A total is a variable in which a script accumulates the sum of a series of values • Variables that store totals should normally be initialized to zero before they are used in a program • A counter is a variable a script uses to count—typically in a repetition statement

  3. Set total to zeroSet grade counter to oneWhile grade counter is less than or equal to ten Input the next grade Add the grade into the total Add one to the grade counterSet the class average to the total divided by tenPrint the class average

  4. Fig. 7.7 | Counter-controlled repetition to calculate a class average (Part 1 of 3). Stores the sum of grades Sets total to 0 Sets gradeCounter to 1 in preparation for the loop Continues the cycle until gradeCounter is greater than 10

  5. Fig. 7.7 | Counter-controlled repetition to calculate a class average (Part 2 of 3). Increments gradeCounter by 1 after each iteration of the loop

  6. Fig. 7.7 | Counter-controlled repetition to calculate a class average (Part 3 of 3).

  7. 7.9 Sentinel-Controlled Repetition • Sentinel-controlled repetition • Special value called a sentinel value (also called a signal value, a dummy value or a flag value) indicates the end of data entry • Often is called indefinite repetition, because the number of repetitions is not known in advance

  8. Fig. 7.9 | Sentinel-controlled repetition to calculate a class average (Part 1 of 3). Set gradeCounter to 0 in preparation for the loop

  9. Executes until gradeValue equals -1 Fig. 7.9 | Sentinel-controlled repetition to calculate a class average (Part 2 of 3). Increments gradeCounter by 1 after each iteration of the loop Begin new control structure Execute if the condition in the if statement is not satisfied

  10. Fig. 7.9 | Sentinel-controlled repetition to calculate a class average (Part 3 of 3).

More Related