Java Script Control Statements II Outline 1 2

































- Slides: 33
Java. Script: Control Statements (II) Outline 1. 2. 3. 4. 5. 6. Essentials of Counter-Controlled Repetition for Repetition Statement Examples Using the for Statement switch Multiple-Selection Statement do…while Repetition Statement break and continue Statements 2004 Prentice Hall, Inc. All rights reserved.
Objectives • In this lesson, you will learn: – To be able to use the for and do…while repetition statements to execute statements in a program repeatedly. – To understand multiple selection using the switch selection statement. – To be able to use the break and continue program-control statements. – To be able to use the logical operators. 2004 Prentice Hall, Inc. All rights reserved.
1. Essentials of Counter-Controlled Repetition • Counter-controlled repetition – – Name of a control Initial value Increment or decrement Final value 2004 Prentice Hall, Inc. All rights reserved.
Outline While. Counter. html (1 of 2) 2004 Prentice Hall, Inc. All rights reserved.
Outline While. Counter. html (2 of 2) 2004 Prentice Hall, Inc. All rights reserved.
2. for Repetition Statement • for repetition statement – Handles all the details of counter-controlled repetition – for structure header • The first line 2004 Prentice Hall, Inc. All rights reserved.
Outline For. Counter. html (1 of 1) 2004 Prentice Hall, Inc. All rights reserved.
2004 Prentice Hall, Inc. All rights reserved.
for Repetition Statement for keyword Control variable name Final value of control variable for which the condition is true for ( var counter = 1; counter <= 7; ++counter ) Initial value of control variable Increment of control variable Loop-continuation condition Fig. 9. 3 for statement header components. 2004 Prentice Hall, Inc. All rights reserved.
for Repetition Statement Establish initial value of control variable. var counter = 1 counter <= 7 true false Body of loop (this may be many statements) Determine if final value of control variable has been reached. Fig. 9. 4 document. writeln( "<p style="font-size: " + counter + "ex">XHTML font size " + counter + "ex</p>" ); for repetition structure flowchart. 2004 Prentice Hall, Inc. All rights reserved. ++counter Increment the control variable.
3. Examples Using the for Statement • Summation with for • Compound interest calculation with for loop – Math object • Method pow • Method round 2004 Prentice Hall, Inc. All rights reserved.
12. 3 Math Object 2004 Prentice Hall, Inc. All rights reserved.
12. 3 Math Object 2004 Prentice Hall, Inc. All rights reserved.
Outline Sum. html (1 of 1) 2004 Prentice Hall, Inc. All rights reserved.
2004 Prentice Hall, Inc. All rights reserved.
Outline Interest. html (1 of 2) 2004 Prentice Hall, Inc. All rights reserved.
Outline Interest. html (2 of 2) 2004 Prentice Hall, Inc. All rights reserved.
4. switch Multiple-Selection Statement • Controlling expression • Case labels • Default case 2004 Prentice Hall, Inc. All rights reserved.
Outline Switch. Test. html (1 of 3) 2004 Prentice Hall, Inc. All rights reserved.
Outline Switch. Test. html (2 of 3) 2004 Prentice Hall, Inc. All rights reserved.
Outline Switch. Test. html (3 of 3) 2004 Prentice Hall, Inc. All rights reserved.
2004 Prentice Hall, Inc. All rights reserved.
2004 Prentice Hall, Inc. All rights reserved.
4. switch Multiple-Selection Statement case a true case a action(s) break case b action(s) break case z action(s) break false case b true false. . . case z false default action(s) 2004 Prentice Hall, Inc. All rights reserved. true
do…while Repetition Statement • Similar to the while statement • Tests the loop continuation condition after the loop body executes • Loop body always executes at least once 2004 Prentice Hall, Inc. All rights reserved.
Outline Do. While. Test. html (1 of 2) 2004 Prentice Hall, Inc. All rights reserved.
Outline Do. While. Test. html (2 of 2) 2004 Prentice Hall, Inc. All rights reserved.
5. do…while Repetition Structure action(s) condition false Fig. 9. 10 do…while repetition statement flowchart. 2004 Prentice Hall, Inc. All rights reserved. true
6. break and continue Statements • break – Immediate exit from the structure – Used to escape early from a loop – Skip the remainder of a switch statement • continue – Skips the remaining statements in the body of the structure – Proceeds with the next iteration of the loop 2004 Prentice Hall, Inc. All rights reserved.
Outline Break. Test. html (1 of 2) 2004 Prentice Hall, Inc. All rights reserved.
Outline Break. Test. html (2 of 2) 2004 Prentice Hall, Inc. All rights reserved.
Outline Continue. Test. html (1 of 2) 2004 Prentice Hall, Inc. All rights reserved.
Outline Continue. Test. html (2 of 2) 2004 Prentice Hall, Inc. All rights reserved.