Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaScript: Control Structures September 27, 2005 Slides modified from Internet & World Wide Web: How to Program. 2004 (3rd) edition. By Deitel, Deitel,

Similar presentations


Presentation on theme: "JavaScript: Control Structures September 27, 2005 Slides modified from Internet & World Wide Web: How to Program. 2004 (3rd) edition. By Deitel, Deitel,"— Presentation transcript:

1 JavaScript: Control Structures September 27, 2005 Slides modified from Internet & World Wide Web: How to Program. 2004 (3rd) edition. By Deitel, Deitel, and Goldberg. Published by Prentice Hall. ISBN 0-13-145091-3

2 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

3 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.

4 9.2 Essentials of Counter- Controlled Repetition Counter-controlled repetition –Name of a control –Initial value –Increment or decrement –Final value

5 WhileCounter.html (1 of 2)

6 9.3 for Repetition Statement for repetition statement –Handles all the details of counter-controlled repetition –for structure header The first line

7 ForCounter.html (1 of 1)

8 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.

9 9.4 Examples Using the for Statement Summation with for Compound interest calculation with for loop –Math object Method pow Method round

10 Sum.html (1 of 1)

11 Interest.html (1 of 2)

12 9.5 switch Multiple- Selection Statement Controlling expression Case labels Default case

13 SwitchTest.html (1 of 3)

14 SwitchTest.html (2 of 3)

15 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

16 DoWhileTest.html (1 of 2)

17 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

18 BreakTest.html (1 of 2)

19 ContinueTest.html (1 of 2)

20 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

21 BreakLabelTest.html (1 of 2)

22 ContinueLabelTest.html (1 of 2)

23 Logical operators

24 9.9 Logical Operators More logical operators –Logical AND ( && ) –Logical OR ( || ) –Logical NOT ( ! )

25 9.9 Logical Operators

26

27 LogicalOperators.html (1 of 2)

28 9.9 Logical Operators

29 Conclusion to JavaScript introduction

30 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

31 When things go wrong: Debugging JavaScript Taken from http://www.ryerson.ca/JavaScript/lectures/scriptTags/debugging.htmlhttp://www.ryerson.ca/JavaScript/lectures/scriptTags/debugging.html JavaScript is case sensitive –Document.write() will not work. Neither will document.Write(). Almost every object, property, and method name like document and write() begin with a lower case letter. In cases like document.bgColor the second word in an object, property, or method name is almost always capitalized. I.e. the C in bgColor but nothing else. Use the American spelling of color –There is no colour property. Don't forget the closing bracket, and don’t forget both quotes –Make sure your pairs of (parentheses) and “double quotes” or ‘single quotes’ all match Spelling counts –Drop the e in document and watch what happens. The order you do things in matters –If you set the background colour in the head of the document the document background colour may not change. If you set it in the body it will. Watch out for quotes inside quotes –Whenever you write out an HTML tag with an attribute that requires quotes you must escape the quote inside the tag by placing a backslash just before it. –For example document.write(" A LINK ") will produce an error. –Escaping the quotes will fix this: document.write(" A LINK "). –Make sure you escape both of them.

32 8.14 Web Resources http://www.javascripter.net/faq/index.htm http://www.ecma- international.org/publications/standards/Ecma-262.htmhttp://www.ecma- international.org/publications/standards/Ecma-262.htm http://www.devguru.com/Technologies/ecmascript/quickr ef/javascript_index.htmlhttp://www.devguru.com/Technologies/ecmascript/quickr ef/javascript_index.html http://javascript.about.com/library/blscr02.htm http://www.ryerson.ca/JavaScript/lectures/ www.javascriptmall.com developer.netscape.com/tech/javascript www.mozilla.org/js/language


Download ppt "JavaScript: Control Structures September 27, 2005 Slides modified from Internet & World Wide Web: How to Program. 2004 (3rd) edition. By Deitel, Deitel,"

Similar presentations


Ads by Google