Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2004 Prentice Hall, Inc. All rights reserved. Chapter 9 - JavaScript: Control Statements II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.

Similar presentations


Presentation on theme: " 2004 Prentice Hall, Inc. All rights reserved. Chapter 9 - JavaScript: Control Statements II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled."— Presentation transcript:

1  2004 Prentice Hall, Inc. All rights reserved. Chapter 9 - JavaScript: Control Statements II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled Repetition 9.3 for Repetition Statement 9.4 Examples Using the for Statement 9.5 switch Multiple-Selection Statement 9.6 do … while Repetition Statement 9.7 break and continue Statements 9.8 Labeled break and continue Statements 9.9 Logical Operators 9.10 Summary of Structured Programming 9.11Web Resources

2  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.

3  2004 Prentice Hall, Inc. All rights reserved. 9.1 Introduction Continuation of Chapter 8 –Theory and principles of structured programming

4  2004 Prentice Hall, Inc. All rights reserved. 9.2 Essentials of Counter-Controlled Repetition Counter-controlled repetition –Name of a control –Initial value –Increment or decrement –Final value

5  2004 Prentice Hall, Inc. All rights reserved. Outline WhileCounter.html (1 of 2)

6  2004 Prentice Hall, Inc. All rights reserved. Outline WhileCounter.html (2 of 2)

7  2004 Prentice Hall, Inc. All rights reserved. 9.3 for Repetition Statement for repetition statement –Handles all the details of counter-controlled repetition –for structure header The first line

8  2004 Prentice Hall, Inc. All rights reserved. Outline ForCounter.html (1 of 1)

9  2004 Prentice Hall, Inc. All rights reserved.

10 9.3 for Repetition Statement for (var counter =1; counter <=7; ++counter ) Initial value of control variable Increment of control variable Control variable name Final value of control variable for which the condition is true for keyword Loop-continuation condition Fig. 9.3 for statement header components.

11  2004 Prentice Hall, Inc. All rights reserved. 9.3 for Repetition Statement counter <=7 document.writeln( "<p style=\"font-size: " + counter + "ex\">XHTML font size " + counter +"ex " ); true false var counter =1 ++counter Establish initial value of control variable. Determine if final value of control variable has been reached. Body of loop (this may be many statements) Increment the control variable. Fig. 9.4 for repetition structure flowchart.

12  2004 Prentice Hall, Inc. All rights reserved. 9.4 Examples Using the for Statement Summation with for Compound interest calculation with for loop –Math object Method pow Method round

13  2004 Prentice Hall, Inc. All rights reserved. Outline Sum.html (1 of 1)

14  2004 Prentice Hall, Inc. All rights reserved.

15 Outline Interest.html (1 of 2)

16  2004 Prentice Hall, Inc. All rights reserved. Outline Interest.html (2 of 2)

17  2004 Prentice Hall, Inc. All rights reserved. 9.5 switch Multiple-Selection Statement Controlling expression Case labels Default case

18  2004 Prentice Hall, Inc. All rights reserved. Outline SwitchTest.html (1 of 3)

19  2004 Prentice Hall, Inc. All rights reserved. Outline SwitchTest.html (2 of 3)

20  2004 Prentice Hall, Inc. All rights reserved. Outline SwitchTest.html (3 of 3)

21  2004 Prentice Hall, Inc. All rights reserved.

22

23 9.5 switch Multiple-Selection Statement case a a action(s) true false... break case b action(s) break false case z z action(s) break default action(s) true case b

24  2004 Prentice Hall, Inc. All rights reserved. 9.6 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

25  2004 Prentice Hall, Inc. All rights reserved. Outline DoWhileTest.html (1 of 2)

26  2004 Prentice Hall, Inc. All rights reserved. Outline DoWhileTest.html (2 of 2)

27  2004 Prentice Hall, Inc. All rights reserved. 9.6 do…while Repetition Structure condition true action(s) false Fig. 9.10 do … while repetition statement flowchart.

28  2004 Prentice Hall, Inc. All rights reserved. 9.7 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

29  2004 Prentice Hall, Inc. All rights reserved. Outline BreakTest.html (1 of 2)

30  2004 Prentice Hall, Inc. All rights reserved. Outline BreakTest.html (2 of 2)

31  2004 Prentice Hall, Inc. All rights reserved. Outline ContinueTest.html (1 of 2)

32  2004 Prentice Hall, Inc. All rights reserved. Outline ContinueTest.html (2 of 2)

33  2004 Prentice Hall, Inc. All rights reserved. 9.8 Labeled break and continue Statements Labeled break statement –Break out of a nested set of structures –Immediate exit from that structure and enclosing repetition structures –Execution resumes with first statement after enclosing labeled statement Labeled continue statement –Skips the remaining statements in structure’s body and enclosing repetition structures –Proceeds with next iteration of enclosing labeled repetition structure –Loop-continuation test evaluates immediately after the continue statement executes

34  2004 Prentice Hall, Inc. All rights reserved. Outline BreakLabelTest.html (1 of 2)

35  2004 Prentice Hall, Inc. All rights reserved. Outline BreakLabelTest.html (2 of 2)

36  2004 Prentice Hall, Inc. All rights reserved. Outline ContinueLabelTest.html (1 of 2)

37  2004 Prentice Hall, Inc. All rights reserved. Outline ContinueLabelTest.html (2 of 2)

38  2004 Prentice Hall, Inc. All rights reserved. 9.9 Logical Operators More logical operators –Logical AND ( && ) –Logical OR ( || ) –Logical NOT ( ! )

39  2004 Prentice Hall, Inc. All rights reserved. 9.9 Logical Operators

40  2004 Prentice Hall, Inc. All rights reserved. 9.9 Logical Operators

41  2004 Prentice Hall, Inc. All rights reserved. Outline LogicalOperators.html (1 of 2)

42  2004 Prentice Hall, Inc. All rights reserved. Outline LogicalOperators.html (2 of 2)

43  2004 Prentice Hall, Inc. All rights reserved.

44 9.9 Logical Operators

45  2004 Prentice Hall, Inc. All rights reserved. 9.10 Summary of Structured Programming Flowcharts –Reveal the structured nature of programs Single-entry/single-exit control structures –Only one way to enter and one way to exit each control structure Control structure stacking –The exit point of one control structure is connected to the entry point of the next control structure

46  2004 Prentice Hall, Inc. All rights reserved. 9.10 Summary of Structured Programming T F w h i l e statement T F f o r T F d o … w h i l e R e p e t i t i o n Fig. 9.20Single-entry/single-exit sequence, selection and repetition structures. (1 of 3) statement

47  2004 Prentice Hall, Inc. All rights reserved. 9.10 Summary of Structured Programming b r e a k T F i f statement ( s i n g l e s e l e c t i o n ) T F i f … e l s e ( d o u b l e s e l e c t i o n ) T F s w i t c h ( m u l t i p l e s e l e c t i o n ) T F T F... S e l e c t i o n b r e a k b r e a k Fig. 9.20Single-entry/single-exit sequence, selection and repetition structures. (2 of 3) statement

48  2004 Prentice Hall, Inc. All rights reserved. 9.10 Summary of Structured Programming Fig. 9.20Single-entry/single-exit sequence, selection and repetition structures. (3 of 3)

49  2004 Prentice Hall, Inc. All rights reserved. 9.10 Summary of Structured Programming

50  2004 Prentice Hall, Inc. All rights reserved. 9.10 Summary of Structured Programming Fig. 9.22Simplest flowchart.

51  2004 Prentice Hall, Inc. All rights reserved. 9.10 Summary of Structured Programming Fig. 9.23Repeatedly applying rule 2 of Fig. 9.21 to the simplest flowchart.

52  2004 Prentice Hall, Inc. All rights reserved. 9.10 Summary of Structured Programming Fig. 9.24Applying rule 3 of Fig. 9.21 to the simplest flowchart.

53  2004 Prentice Hall, Inc. All rights reserved. 9.10 Summary of Structured Programming Stacked building blocksNested building blocks Overlapping building blocks (Illegal in structured programs) Fig. 9.25Stacked, nested and overlapped building blocks.

54  2004 Prentice Hall, Inc. All rights reserved. 9.10 Summary of Structured Programming Fig. 9.26Unstructured flowchart.


Download ppt " 2004 Prentice Hall, Inc. All rights reserved. Chapter 9 - JavaScript: Control Statements II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled."

Similar presentations


Ads by Google