Download presentation
Presentation is loading. Please wait.
Published byRosamund Jewel Parks Modified over 9 years ago
1
2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode 17.4 Control Structures 17.5 if Selection Structure 17.6 if/else Selection Structure 17.7 while Repetition Structure 17.8 Formulating Algorithms: Case Study 1 (Counter- Controlled Repetition) 17.9 Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 2 (Sentinel-Controlled Repetition) 17.10 Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 3 (Nested Control Structures) 17.11 Assignment Operators 17.12 Increment and Decrement Operators 17.13 Note on Data Types 17.14 WMLScript Internet and World Wide Web Resources
2
2001 Prentice Hall, Inc. All rights reserved. 2 17.4 Control Structures Fig. 17.1Flowcharting WMLScript’s sequence structure.
3
2001 Prentice Hall, Inc. All rights reserved. 3 17.5 if Selection Structure Fig. 17.2Flowcharting the single-selection if structure.
4
2001 Prentice Hall, Inc. All rights reserved. 4 17.6 if/else Selection Structure Fig. 17.3Flowcharting the double-selection if/else structure. “Passed”
5
2001 Prentice Hall, Inc. All rights reserved. 5 17.7 while Repetition Structure Fig. 17.4Flowcharting the while repetition structure.
6
2001 Prentice Hall, Inc. All rights reserved. Outline 6 1 // Fig. 17.6: mean1.wmls 2 // Class average program 3 4 extern function average() 5 { 6 var total = 0; // clear total 7 var gradeCounter = 1; // prepare to loop 8 var grade; // grade input 9 var gradeValue; // number returned by parseInt method 10 11 while ( gradeCounter <= 10 ){ // loop 10 times 12 grade = Dialogs.prompt( "Enter integer grade", "" ); 13 14 gradeValue = Lang.parseInt( grade ); 15 16 total += gradeValue; 17 18 ++gradeCounter; 19 } 20 21 var average = String.format( "%3.2f", ( total / 10 ) ); 22 23 WMLBrowser.setVar( "classAve", average ); 24 WMLBrowser.go( "#classAverage" ); 25 } Mean1.wmls Loop 10 times. Prompt for user input. Convert the user input to an integer. Add the value input by the user to the total.Increment the counter. Calculate the average.
7
2001 Prentice Hall, Inc. All rights reserved. Outline 7 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Click Enter to enter the grades 19 20 21 22 23 24 The class average is: $classAve 25 26 27 Fig17_7.wml Display the class average.
8
2001 Prentice Hall, Inc. All rights reserved. Outline 8
9
2001 Prentice Hall, Inc. All rights reserved. Outline 9
10
2001 Prentice Hall, Inc. All rights reserved. Outline 10 1 // Fig. 17.9: mean2.wmls 2 // Class average program 3 4 extern function average2() 5 { 6 var total = 0; // clear total 7 var gradeCounter = 0; // prepare to loop 8 var average; 9 10 // prompt for input and read grade from user 11 var grade = Dialogs.prompt( "Enter integer grade," + 12 "(-1) to Quit", "" ); 13 14 // convert grade from a string to an integer 15 var gradeValue = Lang.parseInt( grade ); 16 17 while ( gradeValue != (-1) ) { 18 19 // add gradeValue to total 20 total = total + gradeValue; 21 22 // increment gradeCounter 23 ++gradeCounter; 24 25 // prompt for input and read grade from user 26 grade = Dialogs.prompt( "Enter integer grade, " + 27 "(-1) to Quit", "" ); 28 29 // convert grade from string to integer 30 gradeValue = Lang.parseInt( grade ); 31 } 32 Mean2.wmls Prompt for user input.Convert user input to an integer. Loop until a value of –1 is entered by user. Add grade entered to total.Prompt for user input.
11
2001 Prentice Hall, Inc. All rights reserved. Outline 11 33 // termination phase 34 if ( gradeCounter != 0 ) { 35 average = 36 String.format( "%3.2f", ( total / gradeCounter ) ); 37 } 38 else { 39 average = "No grades were entered"; 40 } 41 42 WMLBrowser.setVar( "classAve", average ); 43 WMLBrowser.go( "#classAverage" ); 44 } Mean2.wmls Calculate class average.Display an error message if no grades were entered.
12
2001 Prentice Hall, Inc. All rights reserved. Outline 12 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Click Enter to enter the grades 19 20 21 22 23 24 The class average is: $classAve 25 26 27 Fig17_10.wml Display the class average.
13
2001 Prentice Hall, Inc. All rights reserved. Outline 13
14
2001 Prentice Hall, Inc. All rights reserved. Outline 14 1 // Fig. 17.12: count.wmls 2 // Examination-results program 3 4 extern function passFail() 5 { 6 var passes = 0; // number of passes 7 var failures = 0; // number of failures 8 var student = 1; // student counter 9 10 while ( student 8 ) { 27 WMLBrowser.setVar( "tooMany", "Raise Tuition" ); 28 } 29 30 WMLBrowser.setVar( "numberPass", passes ); 31 WMLBrowser.setVar( "numberFail", failures ); 32 WMLBrowser.go( "#results" ); 33 } count.wmls Loop 10 times.Prompt for user input. Update value of passes if a 1 was entered.Update the value of failures if anything accept a 1 was entered. Update the counter.
15
2001 Prentice Hall, Inc. All rights reserved. Outline 15 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Click Run to run script 19 20 21 22 23 24 25 26 Examination results: 27 Passed: $numberPass 28 Failed: $numberFail 29 $tooMany 30 31 32 Fig17_13.wml Display the results.
16
2001 Prentice Hall, Inc. All rights reserved. Outline 16
17
2001 Prentice Hall, Inc. All rights reserved. Outline 17
18
2001 Prentice Hall, Inc. All rights reserved. 18 17.12 Increment and Decrement Operators
19
2001 Prentice Hall, Inc. All rights reserved. 19 17.12 Increment and Decrement Operators
20
2001 Prentice Hall, Inc. All rights reserved. Outline 20 1 // Fig. 17.16: incrementScript.wmls 2 // Increment example 3 4 extern function increment() 5 { 6 var variable1 = 5; // initial variable 7 var variable2 = 5; // initial variable 8 var post1, post2, post3; // results of increment 9 var pre1, pre2, pre3; // results of decrement 10 11 12 post1 = variable1; 13 post2 = variable1++; // increment 14 post3 = variable1; 15 16 pre1 = variable2; 17 pre2 = ++variable2; // decrement 18 pre3 = variable2; 19 20 WMLBrowser.setVar( "postResult", post1 ); 21 WMLBrowser.setVar( "postResult2", post2 ); 22 WMLBrowser.setVar( "postResult3", post3 ); 23 WMLBrowser.setVar( "preResult", pre1 ); 24 WMLBrowser.setVar( "preResult2", pre2 ); 25 WMLBrowser.setVar( "preResult3", pre3 ); 26 WMLBrowser.refresh(); 27 } IncrementScript.wm ls To begin, the value of variable1 is 5.To begin, the value of variable2 is 5.Postincrement variable1. Preincrement variable2.
21
2001 Prentice Hall, Inc. All rights reserved. Outline 21 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Postincrementing: 21 $postResult $postResult2 $postResult3 22 Preincrementing: 23 $preResult $preResult2 $preResult3 24 25 26 Fig17_17.wml Display the results.
22
2001 Prentice Hall, Inc. All rights reserved. Outline 22
23
2001 Prentice Hall, Inc. All rights reserved. 23 17.12 Increment and Decrement Operators
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.