1 / 13

JavaScript: Control Statements II

Learn the essentials of counter-controlled repetition in JavaScript, including the control variable, initial value, increment/decrement, and loop condition. Explore examples using the for statement and the do...while statement.

cletab
Download Presentation

JavaScript: Control Statements II

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 II

  2. 8.2 Essentials of Counter-Controlled Repetition • Counter-controlled repetition requires • name of a control variable • initial value of the control variable • the increment (or decrement) by which the control variable is modified each time through the loop • the condition that tests for the final value of the control variable to determine whether looping should continue

  3. Fig. 8.1 | Counter-controlled repetition (Part 1 of 2). Initializes counter Precedes the “ with a \ to create an escape sequence so that it can be used in the string Condition to be fulfilled with every iteration Incrementing statement

  4. Fig. 8.1 | Counter-controlled repetition (Part 2 of 2).

  5. 8.3 for Repetition Statement • for statement • Cpecifies each of the items needed for counter-controlled repetition with a control variable • Can use a block to put multiple statements into the body • for statement takes three expressions • Initialization • Condition • Increment Expression

  6. Fig. 8.2 | Counter-controlled repetition with the for statement (Part 1 of 2). Initial value of the control variable Condition to test whether looping should continue Increment to occur after each iteration of the loop Statement inside the for loop

  7. Fig. 8.2 | Counter-controlled repetition with the for statement (Part 2 of 2).

  8. 8.4 Examples Using the for Statement • JavaScript does not include an exponentiation operator • Math object’s pow method for this purpose. Math.pow(x, y) calculates the value of x raised to the yth power. • Floating-point numbers can cause trouble as a result of rounding errors

  9. Fig. 8.6 | Compound interest calculation with a for loop (Part 1 of 2).

  10. Fig. 8.6 | Compound interest calculation with a for loop (Part 2 of 2). Control variable year begins with a value of 1 Continue to execute the loop while year is less than or equal to 10 After each loop iteration, increase the value of year by 1

  11. 8.6 do…while Repetition Statement • do…while statement • tests the loop-continuation condition after the loop body executes • The loop body always executes at least once

  12. Fig. 8.9 | Using the dowhile repetition statement (Part 1 of 2). Perform the following actions… Then check to see if counter <= 6. If it is, iterate through the loop again.

  13. Fig. 8.9 | Using the dowhile repetition statement (Part 2 of 2).

More Related