Chapter 8 Java Script Control Structures I Outline


















- Slides: 18

Chapter 8 - Java. Script: Control Structures I Outline 8. 1 8. 2 8. 3 8. 4 8. 5 8. 6 8. 7 8. 8 8. 9 8. 10 8. 11 8. 12 8. 13 8. 14 Introduction Algorithms Pseudocode Control Structures if Selection Structure if/else Selection Structure while Repetition Structure Formulating Algorithms: Case Study 1 (Counter-Controlled Repetition) Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 2 (Sentinel-Controlled Repetition) Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 3 (Nested Control Structures) Assignment Operators Increment and Decrement Operators Note on Data Types Java. Script Internet and World Wide Web Resources 2001 Prentice Hall, Inc. All rights reserved. 1

2 8. 4 Control Structures Fig. 8. 1 Flowcharting Java. Script’s sequence structure. 2001 Prentice Hall, Inc. All rights reserved.

3 8. 4 Control Structures 2001 Prentice Hall, Inc. All rights reserved.

4 8. 6 if/else Selection Structure Fig. 8. 3 Flowcharting the single-selection if structure. 2001 Prentice Hall, Inc. All rights reserved.

5 8. 6 if/else Selection Structure false print “Failed” Fig. 8. 4 grade >= 60 true print “Passed” Flowcharting the double-selection if/else structure. 2001 Prentice Hall, Inc. All rights reserved.

8. 8 Formulating Algorithms: Case Study 1 (Counter-Controlled Repetition) product <= 1000 true false Fig. 8. 5 2001 Prentice Hall, Inc. All rights reserved. Flowcharting the while repetition structure. 6

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 Outline <? xml version = "1. 0"? > <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Strict//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -strict. dtd"> <!-- Fig. 8. 7: average. html --> <!-- Class Average Program --> <html xmlns = "http: //www. w 3. org/1999/ xhtml"> <head> <title>Class Average Program</title> The while loop will execute the statements in "text/javascript"> the body of the loop until the value of grade. Counter equals 10. // sum of grades <script type = <!-var total, grade. Counter , grade. Value, average, grade; // // number of grades entered grade value average of all grades grade typed by user // Initialization Phase Prompt for the total = 0; // clear total grade. Counter = 1; // prepare to loop // Processing Phase while ( grade. Counter <= 10 ) { Average. html user input a grade. // loop 10 times Convert an integer. // prompt for input and read grade input from to user grade = window. prompt( "Enter integer grade: ", "0" ); // convert grade from a string to an integer grade. Value = parse. Int( grade ); Add new grade to total. // add grade. Value to total = total + grade. Value; 2001 Prentice Hall, Inc. All rights reserved. 7

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 Outline // add 1 to grade. Counter = grade. Counter + 1; } // Termination Phase average = total / 10; Increment the counter. // calculate the average // display average of exam. Calculate grades the average of the document. writeln( input by the user. "<h 1>Class average is " + average + "</h 1>" ); // --> </script> grades Average. html Write the result to the XHTML document. </head> <body> <p>Click Refresh (or Reload) to run the script again <p> </body> </html> Program Output 2001 Prentice Hall, Inc. All rights reserved. 8

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 Outline <? xml version = "1. 0"? > <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Strict//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -strict. dtd"> <!-- Fig. 8. 9: Average 2. html --> <!-- Sentinel-controlled Repetition --> <html xmlns = "http: //www. w 3. org/1999/ xhtml"> <head> <title>Class Average Program: Sentinel-controlled Repetition </title> <script type = "text/javascript"> <!-var grade. Counter, // number of grades entered grade. Value, // grade value total, // sum of grades average, // average of all grades grade; // grade typed by user Average 2. html Prompt for the user to enter a grade, -1 to end. // Initialization phase total = 0; // clear total grade. Counter = 0; // prepare to loop // Processing phase. The while loop will continue until // prompt for inputequals and read – 1. grade from user grade = window. prompt( "Enter Integer Grade, -1 to Quit: " , "0" ); the user input // convert grade from a string to an integer grade. Value = parse. Int( grade ); while ( grade. Value != -1 ) { // add grade. Value to total = total + grade. Value; 2001 Prentice Hall, Inc. All rights reserved. 9

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 // add 1 to grade. Counter = grade. Counter + 1; Outline // prompt for input and read grade from user grade = window. prompt( "Enter Integer Grade, -1 to Quit: " , "0" ); // convert grade from. Each a string to of anthe integer iteration loop will grade. Value = parse. Int( grade ); } open a prompt dialog allowing the user to input another grade. // Termination phase if ( grade. Counter != 0 ) { average = total / grade. Counter; // display average of exam grades document. writeln( "<h 1>Class average is " + average + "</h 1>" ); Average 2. html } else document. writeln( "<p>No grades were entered</p>" ); // --> </script> </head> <body> <p>Click Refresh (or Reload) to run the script again </p> </body> </html> 2001 Prentice Hall, Inc. All rights reserved. 10

Outline Program Output 2001 Prentice Hall, Inc. All rights reserved. 11

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <? xml version = "1. 0"? > <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Strict//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -strict. dtd"> Outline <!-- Fig. 8. 11: analysis. html --> <!-- Analyzing Exam Results --> The while loop will continue until the value of "http: //www. w 3. org/1999/ xhtml"> student is 10 meaning 10 results were entered. <html xmlns = <head> <title>Analysis of Examination Results </title> Entering a 1 into the prompt dialog means the <script type = "text/javascript"> <!-student passed the exam. A value of 2 means the // initializing variables in declarations student failed. var passes = 0, // number of passes failures = 0, // number of failures student = 1, // student counter result; // one exam result Analysis. html // process 10 students; counter-controlled loop while ( student <= 10 ) { result = window. prompt( If a value of 1 was "Enter result (1=pass, 2=fail)", "0" ); if ( result == "1" ) passes = passes + 1; else failures = failures + 1; entered, the value of passes is incremented by one, otherwise, failures is incremented. student = student + 1; } 2001 Prentice Hall, Inc. All rights reserved. 12

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 // termination phase document. writeln( "<h 1>Examination Results</h 1>" ); document. writeln( "Passed: " + passes + " Failed: " + failures ); Outline if ( passes > 8 ) document. writeln( " Raise Tuition" ); // --> </script> If more than 8 students passed the </head> <body> to “Raise Tuition”. <p>Click Refresh (or Reload) to run the script again </p> </body> </html> exam, the program says Analysis. html 2001 Prentice Hall, Inc. All rights reserved. 13

Outline Program Output 2001 Prentice Hall, Inc. All rights reserved. 14

15 8. 12 Increment and Decrement Operators 2001 Prentice Hall, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <? xml version = "1. 0"? > <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Strict//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -strict. dtd"> Outline <!-- Fig. 8. 14: increment. html --> <!-- Preincrementing and Postincrementing --> <html xmlns = "http: //www. w 3. org/1999/ xhtml"> <head> <title>Preincrementing and Postincrementing</title> <script type = "text/javascript"> <!-var c; Postincrementing the variable will print the variable and then increment the value by one. c = 5; document. writeln( "<h 3>Postincrementing</h 3>" ); document. writeln( c ); // print 5 Increment. html // print 5 then increment document. writeln( " " + c++ ); Preincrementing the variable will increment the document. writeln( " " + c ); value // print by one 6 and then print the value. c = 5; document. writeln( // increment then document. writeln( // --> </script> "<h 3>Preincrementing</h 3>" ); c ); // print 5 print 6 " " + ++c ); " " + c ); // print 6 </head><body></body> </html> 2001 Prentice Hall, Inc. All rights reserved. 16

Outline Program Output 2001 Prentice Hall, Inc. All rights reserved. 17

18 8. 13 Note on Data Types 2001 Prentice Hall, Inc. All rights reserved.