Fall 2008Yanjun Li CSRU JavaScript: Control Statements I Internet & World Wide Web:
Fall 2008Yanjun Li CSRU 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;
Fall 2008Yanjun Li CSRU 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
Fall 2008Yanjun Li CSRU 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”
Fall 2008Yanjun Li CSRU 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! "); }
Fall 2008Yanjun Li CSRU 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”
Fall 2008Yanjun Li CSRU 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”);
Fall 2008Yanjun Li CSRU if…else Selection Statement (3) Multiple actions JavaScript Format : if if ( boolean expression ) { statementOne; statementTwo; : }else { statementThree; statementFour; : }
Fall 2008Yanjun Li CSRU while Repetition Statement (1) Repetition structure (loop) – Repeat action while some condition remains true. product <= 1000 product = 2 * product true false
Fall 2008Yanjun Li CSRU 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; }
Fall 2008Yanjun Li CSRU Counter-Controlled Repetition Counter-controlled repetition – Counter Control the number of times a set of statements executes – Definite repetition
Fall 2008Yanjun Li CSRU average.html (1 of 3)
Fall 2008Yanjun Li CSRU average.html (2 of 3)
Fall 2008Yanjun Li CSRU
Fall 2008Yanjun Li CSRU Sentinel-Controlled Repetition Indefinite repetition – Sentinel value
Fall 2008Yanjun Li CSRU average2.html (1 of 3)
Fall 2008Yanjun Li CSRU average2.html (2 of 3)
Fall 2008Yanjun Li CSRU average2.html (3 of 3)
Fall 2008Yanjun Li CSRU
Fall 2008Yanjun Li CSRU Note on Data Types Loosely typed – Automatically converts between values of different types
Fall 2008Yanjun Li CSRU Web Resources developer.netscape.com/tech/javascript
Fall 2008Yanjun Li CSRU Reference Reproduced from the PowerPoints for Internet & World Wide Web How to Program, 3e by Deitel, Deitel and Goldberg © Reproduced by permission of Pearson Education, Inc.