Professional Documents
Culture Documents
Outline
8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 Introduction Algorithms Pseudocode Control Structures if Selection Statement ifelse Selection Statement while Repetition Statement Formulating Algorithms: Case Study 1 (Counter-Controlled Repetition) 8.9 Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 2 (Sentinel-Controlled Repetition) 8.10 Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 3 (Nested Control Structures) 8.11 Assignment Operators 8.12 Increment and Decrement Operators 8.13 Note on Data Types 8.14 Web Resources
2004 2003 Prentice Hall, Inc. All rights reserved.
8.2 Algorithms Actions to be executed Order in which the actions are to be executed
Transfer of control
Next statement to execute may not be the next one in sequence
Repetition structure
while dowhile for forin
Rectangle symbol
Indicate any type of action
Oval symbol
A complete algorithm
Diamond symbol
Indicates a decision is to be made
2004 2003 Prentice Hall, Inc. All rights reserved.
add 1 to counter
counter = counter + 1;
Fig. 8.1
continue finally instanceof throw while default for new try with
8.5 if Selection Statement Single-entry/single-exit structure Indicate action only when the condition evaluates to true
grade >= 60
true
print Passed
false
Fig. 8.3
8.6 ifelse Selection Statement Indicate different actions to be perform when condition is true or false Conditional operator (?:)
JavaScripts only ternary operator
Three operands Forms a conditional expression
Dangling-else problem
grade >= 60
Fig. 8.4
true
product =
2 * product
Fig. 8.5
Definite repetition
1 2 3 4 5 6 7 8 9 10
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!-- Fig. 8.7: average.html --> <!-- Class Average Program <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Class Average Program</title> <script type = "text/javascript"> <!-var total, gradeCounter, gradeValue, average, grade; // sum of grades // number of grades entered // grade value // average of all grades // grade typed by user -->
Outline
average.html (1 of 3)
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
Outline
average.html (2 of 3)
// prompt for input and read grade from user grade = window.prompt( "Enter integer grade:", "0" ); // convert grade from a string to an integer gradeValue = parseInt( grade ); // add gradeValue to total total = total + gradeValue; // add 1 to gradeCounter gradeCounter = gradeCounter + 1; } // Termination Phase average = total / 10; // calculate the average
// display average of exam grades document.writeln( "<h1>Class average is " + average + "</h1>" ); // --> </script>
48 49 50 51 52 </head> <body> <p>Click Refresh (or Reload) to run the script again<p> </body>
Outline
average.html (3 of 3)
53 </html>
8.9 Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 2 (Sentinel-Controlled Repetition) Indefinite repetition
Sentinel value
1 2 3 4 5 6 7 8 9 10 11
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!-- Fig. 8.9: average2.html <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Class Average Program: Sentinel-controlled Repetition</title> <script type = "text/javascript"> <!-var gradeCounter, gradeValue, total, average, grade; // number of grades entered // grade value // sum of grades // average of all grades // grade typed by user --> <!-- Sentinel-controlled Repetition -->
Outline
average2.html (1 of 3)
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
// Processing phase // prompt for input and read grade from user grade = window.prompt( "Enter Integer Grade, -1 to Quit:", "0" ); // convert grade from a string to an integer gradeValue = parseInt( grade ); while ( gradeValue != -1 ) { // add gradeValue to total total = total + gradeValue; // add 1 to gradeCounter gradeCounter = gradeCounter + 1; // prompt for input and read grade from user grade = window.prompt( "Enter Integer Grade, -1 to Quit:", "0" ); // convert grade from a string to an integer gradeValue = parseInt( grade ); }
Outline
average2.html (2 of 3)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
// Termination phase if ( gradeCounter != 0 ) { average = total / gradeCounter; // display average of exam grades document.writeln( "<h1>Class average is " + average + "</h1>" ); } else document.writeln( "<p>No grades were entered</p>" ); // --> </script> </head> <body> <p>Click Refresh (or Reload) to run the script again</p> </body>
Outline
average2.html (3 of 3)
65 </html>
8.10 Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 3 (Nested Control Structures) Consider problem Make observations Top-down, stepwise refinement
1 2 3 4 5 6 7 8 9 10
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!-- Fig. 8.11: analysis.html --> <!-- Analyzing Exam Results <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Analysis of Examination Results</title> <script type = "text/javascript"> <!-// initializing variables in declarations var passes = 0, failures = 0, student = 1, result; // number of passes // number of failures // student counter // one exam result -->
Outline
analysis.html (1 of 2)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 // process 10 students; counter-controlled loop while ( student <= 10 ) { result = window.prompt( "Enter result (1=pass,2=fail)", "0" );
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 </head> <body> }
Outline
analysis.html (2 of 2)
// termination phase document.writeln( "<h1>Examination Results</h1>" ); document.writeln( "Passed: " + passes + "<br />Failed: " + failures ); if ( passes > 8 ) document.writeln( "<br />Raise Tuition" ); // --> </script>
47 </html>
Increment a by 1, then use the new value of a in the expression in which a resides. postincrement a++ Use the current value of a in ++ the expression in which a resides, then increment a by 1. predecrement --b Decrement b by 1, then use the -new value of b in the expression in which b resides. postdecrement b-Use the current value of b in -the expression in which b resides, then decrement b by 1. Fig. 8.13 increment and decrement operators.
1 2 3 4 5 6 7 8 9 10
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!-- Fig. 8.14: increment.html <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Preincrementing and Postincrementing</title> <script type = "text/javascript"> <!-var c; c = 5; document.writeln( "<h3>Postincrementing</h3>" ); document.writeln( c ); // print 5 then increment document.writeln( "<br />" + c++ ); document.writeln( "<br />" + c ); c = 5; document.writeln( "<h3>Preincrementing</h3>" ); document.writeln( c ); // print 5 // print 6 // print 5 --> <!-- Preincrementing and Postincrementing -->
Outline
increment.html (1 of 2)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
26 27 28 29 30 31 32
// increment then print 6 document.writeln( "<br />" + ++c ); document.writeln( "<br />" + c ); // --> </script> </head><body></body> // print 6
Outline
increment.html (2 of 2)
33 </html>