Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9: Control Statements II CIS 275—Web Application Development for Business I.

Similar presentations


Presentation on theme: "Chapter 9: Control Statements II CIS 275—Web Application Development for Business I."— Presentation transcript:

1 Chapter 9: Control Statements II CIS 275—Web Application Development for Business I

2 2 LoopingLooping I ______ loop for (i = 0; i <= 5; i++) { document.write("The number is " + i + “ ”); } ________ loop i = 0 while (i <= 5) { document.write("The number is " + i + “ ”); i++; }

3 3 Looping II __________ loop do { document.write("The number is " + i); document.write(" "); i++; } while (i <= 5)

4 4 Math Object Rounding a number Rounding Math.round(7.25) returns _____ Generate a random number between 0 and 1random number Math.random() Generate an integer between 0 and 9 randomlyinteger Math.floor(Math.random()*10) Find the minimum of a series of numbersminimum Math.min(2, 4, 6, 7, 1) returns _____ Find the square root of a number Math.sqrt(49) returns ____ Raise a number to a power Math.pow(2, 8) returns _____

5 5 ConditionalConditional II _________ statement var d = new Date(); var theDay=d.getDay(); switch (theDay) { case 5: document.write("Finally Friday"); break; case 6: document.write("Super Saturday"); break; case 0: document.write("Sleepy Sunday"); break; default: document.write("I'm really looking forward to this+ weekend!"); }

6 6 break Statement break is used to exit early from the immediately enclosing structure. for(var count = 1; count <= 10; ++count){ if(count == 5) break; document.write(“Count is: ” + count + “ ”); } Labeled break causes exit from the labeled block stop: { // imagine a series of nested control structures if(i == 5) break stop; // end of nested control structures } // end of labeled block

7 7 continue Statement continue skips to the next iteration of the immediately enclosing structure. for(var count = 1; count <= 10; ++count){ if(count == 5) continue; document.write(“Count is: ” + count + “ ”); } Labeled continue skips to the next iteration of the labeled structure. nextRow: // imagine several nested control structures continue nextRow; // end of nested control structures }

8 8 Logical Operators and Expressions Logical (or Boolean) operators in JavaScript ! expression ! means ______ true becomes false, false becomes true expression1 && expression2 && means ______ both expressions must be true for the whole to be true expression1 || expression2 || means ______ both expressions must be false for the whole to be false Examples (5 ‘B’) is _______ !(5 ‘B’) is _______

9 9 Structured Programming Rules Only single-entry/single-exit control structures are used. Control structure _________: The exit point of one control structure is connected to the entry point of the next control structure. To create a structured program 1. Start with the “simplest flowchart” 2. Any rectangle can be replaced by two rectangles in sequence 3. Any rectangle can be replaced by any control structure 4. Rules 2 and 3 may be applied as often as desired and in any order


Download ppt "Chapter 9: Control Statements II CIS 275—Web Application Development for Business I."

Similar presentations


Ads by Google