2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline...

23
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

Transcript of 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline...

Page 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.

2001 Prentice Hall, Inc. All rights reserved.

1

Chapter 17 - WMLScript: Control Structures I

Outline 17.1 Introduction17.2 Algorithms17.3 Pseudocode17.4 Control Structures17.5 if Selection Structure17.6 if/else Selection Structure17.7 while Repetition Structure17.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 Operators17.12 Increment and Decrement Operators17.13 Note on Data Types17.14 WMLScript Internet and World Wide Web Resources

Page 2: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

2

17.4 Control Structures

add grade to to tal tota l = tota l + grade;

add 1 to c ounter counter = c ounter + 1;

Fig. 17.1 Flowcharting WMLScript’s sequence structure.

Page 3: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

3

17.5 if Selection Structure

grade >= 60 true

false

print “Passed”

Fig. 17.2 Flowcharting the single-selection if structure.

Page 4: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

4

17.6 if/else Selection Structure

grade >= 60

print “Failed” print

Fig. 17.3 Flowcharting the double-selection if/else structure.

“Passed”

Page 5: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

5

17.7 while Repetition Structure

product <= 1000 produc t = 2 * produc ttrue

fa lse

Fig. 17.4 Flowcharting the while repetition structure.

Page 6: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

Outline61 // Fig. 17.6: mean1.wmls

2 // Class average program3 4 extern function average()5 {6 var total = 0; // clear total7 var gradeCounter = 1; // prepare to loop8 var grade; // grade input9 var gradeValue; // number returned by parseInt method10 11 while ( gradeCounter <= 10 ){ // loop 10 times12 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.wmlsLoop 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.

Page 7: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

Outline7

1 <?xml version = "1.0"?>2 <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" 3 "http://www.wapforum.org/DTD/wml12.dtd"> 4 5 <!-- Fig. 17.7: fig17_7.wml -->6 <!-- Class average program -->7 8 <wml>9 10 <card id = "index" title = "Class Average Program">11 12 <!-- spft key that calls function average -->13 <do type = "accept" label = "Enter">14 <go href = "mean1.wmls#average()" />15 </do>16 17 <p>18 Click Enter to enter the grades19 </p>20 </card>21 22 <card id = "classAverage" title = "Class Average">23 <p>24 The class average is: $classAve <!-- display result -->25 </p>26 </card>27 </wml>

Fig17_7.wml

Display the class average.

Page 8: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

Outline8

Page 9: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

Outline9

Page 10: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

Outline101 // Fig. 17.9: mean2.wmls

2 // Class average program3 4 extern function average2()5 {6 var total = 0; // clear total7 var gradeCounter = 0; // prepare to loop8 var average;9 10 // prompt for input and read grade from user11 var grade = Dialogs.prompt( "Enter integer grade," + 12 "(-1) to Quit", "" );13 14 // convert grade from a string to an integer15 var gradeValue = Lang.parseInt( grade );16 17 while ( gradeValue != (-1) ) {18 19 // add gradeValue to total20 total = total + gradeValue;21 22 // increment gradeCounter23 ++gradeCounter;24 25 // prompt for input and read grade from user26 grade = Dialogs.prompt( "Enter integer grade, " + 27 "(-1) to Quit", "" );28 29 // convert grade from string to integer30 gradeValue = Lang.parseInt( grade );31 }32

Mean2.wmlsPrompt 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.

Page 11: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

Outline11

33 // termination phase34 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.

Page 12: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

Outline12

1 <?xml version = "1.0"?>2 <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" 3 "http://www.wapforum.org/DTD/wml12.dtd"> 4 5 <!-- Fig. 17.10 : fig17_10.wml -->6 <!-- Class average program -->7 8 <wml>9 10 <card id = "index" title = "Class Average">11 12 <!-- soft key that calls function average2 -->13 <do type = "accept" label = "Enter">14 <go href = "mean2.wmls#average2()"/>15 </do>16 17 <p>18 Click Enter to enter the grades19 </p>20 </card>21 22 <card id = "classAverage" title = "Class Average">23 <p>24 The class average is: $classAve <!-- display result -->25 </p>26 </card>27 </wml>

Fig17_10.wml

Display the class average.

Page 13: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

Outline13

Page 14: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

Outline14

1 // Fig. 17.12: count.wmls2 // Examination-results program3 4 extern function passFail()5 {6 var passes = 0; // number of passes7 var failures = 0; // number of failures8 var student = 1; // student counter9 10 while ( student <= 10 ) { 11 12 // prompt for user input13 var result = 14 Dialogs.prompt( "Enter result (1 = pass, 2 = fail)", "");15 16 if ( result == "1" ) {17 passes = passes + 1;18 }19 else {20 failures = failures + 1;21 }22 23 student = student + 1;24 }25 26 if ( passes > 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.wmlsLoop 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.

Page 15: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

Outline15

1 <?xml version = "1.0"?>2 <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" 3 "http://www.wapforum.org/DTD/wml12.dtd">4 5 <!-- Fig. 17.13: fig17_13.wml -->6 <!-- Examination results program -->7 8 <wml>9 10 <card id = "index" title = "Exam Results Program">11 12 <!-- soft key that calls function passFail -->13 <do type = "accept" label = "Run">14 <go href = "count.wmls#passFail()" />15 </do>16 17 <p>18 Click Run to run script19 </p>20 </card>21 22 <card id = "results" title = "Results">23 <p>24 25 <!-- display results -->26 Examination results:<br />27 Passed: $numberPass<br />28 Failed: $numberFail<br />29 $tooMany30 </p>31 </card>32 </wml>

Fig17_13.wml

Display the results.

Page 16: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

Outline16

Page 17: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

Outline17

Page 18: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

18

17.12 Increment and Decrement Operators

Assignment operator

Initia l variable value Sample expression

Explanation Assigns

+= c = 3 c += 7 c = c + 7 10 to c

-= d = 5 d -= 4 d = d - 4 1 to d

*= e = 4 e *= 5 e = e * 5 20 to e

/= f = 6 f /= 3 f = f / 3 2 to f

%= g = 12 g %= 9 g = g % 9 3 to g

Fig. 17.14 Arithmetic assignment operators.

Page 19: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

19

17.12 Increment and Decrement Operators

Operator C alled Sample expression Explanation

++ preincrement ++a 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. 17.15 The increment and decrement operators.

Page 20: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

Outline20

1 // Fig. 17.16: incrementScript.wmls2 // Increment example3 4 extern function increment() 5 {6 var variable1 = 5; // initial variable7 var variable2 = 5; // initial variable8 var post1, post2, post3; // results of increment9 var pre1, pre2, pre3; // results of decrement10 11 12 post1 = variable1;13 post2 = variable1++; // increment14 post3 = variable1;15 16 pre1 = variable2;17 pre2 = ++variable2; // decrement18 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.wmls

To begin, the value of variable1 is 5.To begin, the value of variable2 is 5.

Postincrement variable1.

Preincrement variable2.

Page 21: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

Outline21

1 <?xml version = "1.0"?>2 <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" 3 "http://www.wapforum.org/DTD/wml12.dtd"> 4 5 <!-- Fig. 17.17: fig17_17.wml -->6 <!-- Increment example -->7 8 <wml>9 10 <card id = "index" title = "Incrementing">11 12 <!-- soft key that calls function increment -->13 <do type = "accept" label = "Run">14 <go href = "incrementScript.wmls#increment()" />15 </do>16 17 <p>18 19 <!-- display results -->20 Postincrementing:<br />21 $postResult $postResult2 $postResult3 <br />22 Preincrementing:<br />23 $preResult $preResult2 $preResult3 <br />24 </p>25 </card> 26 </wml>

Fig17_17.wml

Display the results.

Page 22: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

Outline22

Page 23: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - WMLScript: Control Structures I Outline 17.1 Introduction 17.2 Algorithms 17.3 Pseudocode.

2001 Prentice Hall, Inc. All rights reserved.

23

17.12 Increment and Decrement Operators

Operators Assoc iativity Type

() left to right parentheses

++ -- right to left unary

* / % left to right multiplicative

+ - left to right additive

< <= > >= left to right relational

== != left to right equality

?: right to left conditional

= += -= *= /= %= right to left assignment

Fig. 17.18 Precedenc e and assoc ia tivity of the operators d iscussed so far.