Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2008Yanjun Li CSRU2350 1 JavaScript: Control Statements I Internet & World Wide Web:

Similar presentations


Presentation on theme: "Fall 2008Yanjun Li CSRU2350 1 JavaScript: Control Statements I Internet & World Wide Web:"— Presentation transcript:

1 Fall 2008Yanjun Li CSRU2350 1 JavaScript: Control Statements I Internet & World Wide Web:

2 Fall 2008Yanjun Li CSRU2350 2 Control Structures Sequential execution – Statements execute in the order they are written Transfer of control – Next statement to execute may not be the next one in sequence add grade to total total = total + grade; add 1 to counter counter = counter + 1;

3 Fall 2008Yanjun Li CSRU2350 3 Boolean Expression Using Equality Operators – x = = y – x != y Using Relational Operators – x > y – x < y – x >= y – x <= y The value of the expression is either true or false

4 Fall 2008Yanjun Li CSRU2350 4 if Selection Statement (1) Indicate action only when the condition evaluates to true JavaScript Format: if if ( boolean expression ) statement; Example: if if (grade>= 60) document.writeln(“Passed”); grade >= 60 true false print “Passed”

5 Fall 2008Yanjun Li CSRU2350 5 if Selection Statement (2) Multiple actions are performed when the condition is true JavaScript Format: if if ( boolean expression ) { statementOne; statementTwo; : } Example: if if (grade>= 60) { document.writeln(" " + "Congratulations! "); document.writeln(" You Passed! "); }

6 Fall 2008Yanjun Li CSRU2350 6 if…else Selection Statement (1) Indicate different actions to be perform when condition is true or false grade >= 60 true print “Failed” false print “Passed”

7 Fall 2008Yanjun Li CSRU2350 7 if…else Selection Statement (2) JavaScript Format: if if ( boolean expression ) statement;else JavaScript Example : if ( grade >= 60 ) document.writeln(“Passed”); else document.writeln(“Failed”);

8 Fall 2008Yanjun Li CSRU2350 8 if…else Selection Statement (3) Multiple actions JavaScript Format : if if ( boolean expression ) { statementOne; statementTwo; : }else { statementThree; statementFour; : }

9 Fall 2008Yanjun Li CSRU2350 9 while Repetition Statement (1) Repetition structure (loop) – Repeat action while some condition remains true. product <= 1000 product = 2 * product true false

10 Fall 2008Yanjun Li CSRU2350 10 while Repetition Statement (2) JavaScript Format : initialization; while while ( boolean expression ) { statement; update; } JavaScript Example : var product=2; while ( product <= 1000 ) { document.writeln(product); product = 2 * product; }

11 Fall 2008Yanjun Li CSRU2350 11 Counter-Controlled Repetition Counter-controlled repetition – Counter Control the number of times a set of statements executes – Definite repetition

12 Fall 2008Yanjun Li CSRU2350 12 average.html (1 of 3)

13 Fall 2008Yanjun Li CSRU2350 13 average.html (2 of 3)

14 Fall 2008Yanjun Li CSRU2350 14

15 Fall 2008Yanjun Li CSRU2350 15 Sentinel-Controlled Repetition Indefinite repetition – Sentinel value

16 Fall 2008Yanjun Li CSRU2350 16 average2.html (1 of 3)

17 Fall 2008Yanjun Li CSRU2350 17 average2.html (2 of 3)

18 Fall 2008Yanjun Li CSRU2350 18 average2.html (3 of 3)

19 Fall 2008Yanjun Li CSRU2350 19

20 Fall 2008Yanjun Li CSRU2350 20 Note on Data Types Loosely typed – Automatically converts between values of different types

21 Fall 2008Yanjun Li CSRU2350 21 8.14 Web Resources www.javascriptmall.com developer.netscape.com/tech/javascript www.mozilla.org/js/language

22 Fall 2008Yanjun Li CSRU2350 22 Reference Reproduced from the PowerPoints for Internet & World Wide Web How to Program, 3e by Deitel, Deitel and Goldberg © 2004. Reproduced by permission of Pearson Education, Inc.


Download ppt "Fall 2008Yanjun Li CSRU2350 1 JavaScript: Control Statements I Internet & World Wide Web:"

Similar presentations


Ads by Google