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) Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 3 (Nested Control Structures) Assignment Operators Increment and Decrement Operators Note on Data Types WMLScript Internet and World Wide Web Resources
2001 Prentice Hall, Inc. All rights reserved Control Structures Fig. 17.1Flowcharting WMLScript’s sequence structure.
2001 Prentice Hall, Inc. All rights reserved if Selection Structure Fig. 17.2Flowcharting the single-selection if structure.
2001 Prentice Hall, Inc. All rights reserved if/else Selection Structure Fig. 17.3Flowcharting the double-selection if/else structure. “Passed”
2001 Prentice Hall, Inc. All rights reserved while Repetition Structure Fig. 17.4Flowcharting the while repetition structure.
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 while ( gradeCounter <= 10 ){ // loop 10 times 12 grade = Dialogs.prompt( "Enter integer grade", "" ); gradeValue = Lang.parseInt( grade ); total += gradeValue; gradeCounter; 19 } var average = String.format( "%3.2f", ( total / 10 ) ); 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.
2001 Prentice Hall, Inc. All rights reserved. Outline Click Enter to enter the grades The class average is: $classAve Fig17_7.wml Display the class average.
2001 Prentice Hall, Inc. All rights reserved. Outline 8
2001 Prentice Hall, Inc. All rights reserved. Outline 9
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", "" ); // convert grade from a string to an integer 15 var gradeValue = Lang.parseInt( grade ); while ( gradeValue != (-1) ) { // add gradeValue to total 20 total = total + gradeValue; // increment gradeCounter 23 ++gradeCounter; // prompt for input and read grade from user 26 grade = Dialogs.prompt( "Enter integer grade, " + 27 "(-1) to Quit", "" ); // 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.
2001 Prentice Hall, Inc. All rights reserved. Outline // 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 } WMLBrowser.setVar( "classAve", average ); 43 WMLBrowser.go( "#classAverage" ); 44 } Mean2.wmls Calculate class average.Display an error message if no grades were entered.
2001 Prentice Hall, Inc. All rights reserved. Outline Click Enter to enter the grades The class average is: $classAve Fig17_10.wml Display the class average.
2001 Prentice Hall, Inc. All rights reserved. Outline 13
2001 Prentice Hall, Inc. All rights reserved. Outline 14 1 // Fig : 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 } 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.
2001 Prentice Hall, Inc. All rights reserved. Outline Click Run to run script Examination results: 27 Passed: $numberPass 28 Failed: $numberFail 29 $tooMany Fig17_13.wml Display the results.
2001 Prentice Hall, Inc. All rights reserved. Outline 16
2001 Prentice Hall, Inc. All rights reserved. Outline 17
2001 Prentice Hall, Inc. All rights reserved Increment and Decrement Operators
2001 Prentice Hall, Inc. All rights reserved Increment and Decrement Operators
2001 Prentice Hall, Inc. All rights reserved. Outline 20 1 // Fig : 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 post1 = variable1; 13 post2 = variable1++; // increment 14 post3 = variable1; pre1 = variable2; 17 pre2 = ++variable2; // decrement 18 pre3 = variable2; 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.
2001 Prentice Hall, Inc. All rights reserved. Outline Postincrementing: 21 $postResult $postResult2 $postResult3 22 Preincrementing: 23 $preResult $preResult2 $preResult Fig17_17.wml Display the results.
2001 Prentice Hall, Inc. All rights reserved. Outline 22
2001 Prentice Hall, Inc. All rights reserved Increment and Decrement Operators