2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 18 - WMLScript: Control Structures II...
-
Upload
crystal-martin -
Category
Documents
-
view
222 -
download
4
Transcript of 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 18 - WMLScript: Control Structures II...
2001 Prentice Hall, Inc. All rights reserved.
1
Chapter 18 - WMLScript: Control Structures II
Outline18.1 Introduction18.2 Essentials of Counter-Controlled Repetition18.3 for Repetition Structure18.4 Examples Using the for Structure18.5 break and continue Statements18.6 Logical Operators18.7 Structured Programming Summary18.8 Example: A Game of Chance
2
2001 Prentice Hall, Inc. All rights reserved.
Outline1 // Fig. 18.1: counter.wmls2 // Counter-controlled repetition3 4 extern function repetition()5 {6 var counter = 1; // initialization7 8 while ( counter <= 6 ) { // repetition condition9 var result = result + counter + " Times through loop\n";10 11 ++counter; // increment12 }13 14 WMLBrowser.setVar( "output", result );15 WMLBrowser.go( "#card2" );16 }
counter.wmlsLoop 6 times.
Increment counter.
3
2001 Prentice Hall, Inc. All rights reserved.
Outline1 <?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. 18.2 : fig18_2.wml -->6 <!-- Counter-controlled repetition -->7 8 <wml>9 <card id = "index" title = "Repetition">10 11 <!-- soft key that calls function repetition -->12 <do type = "accept" label = "OK">13 <go href = "counter.wmls#repetition()"/>14 </do>15 16 <p>17 Click OK to run script.18 </p>19 </card>20 21 <card id = "card2" title = "Repetition">22 23 <!-- soft key that links back to previous card -->24 <do type = "accept" label = "Back">25 <prev />26 </do>27 28 <p>29 $output <!-- display results -->30 </p>31 </card> 32 </wml>
fig18_2.wml
Display results.
4
2001 Prentice Hall, Inc. All rights reserved.
Outline
5
2001 Prentice Hall, Inc. All rights reserved.
Outline1 // Fig. 18.3: counterFor.wmls2 // Counter-controlled repetition3 4 extern function repetition()5 {6 // initialization, repetition condition and incrementing 7 // are all included in the for structure header8 for ( var counter = 1; counter <= 6; ++counter ) {9 var result = result + counter + " Times through loop\n";10 }11 12 WMLBrowser.setVar( "output", result );13 WMLBrowser.go( "#card2" );14 }
counterFor.wmls
Loop 6 times.
Increment counter.
6
2001 Prentice Hall, Inc. All rights reserved.
Outline1 <?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. 18.4 : fig18_4.wml -->6 <!-- Counter-controlled repetition -->7 8 <wml>9 <card id = "index" title = "Repetition">10 11 <!-- soft key that calls function repetition -->12 <do type = "accept" label = "OK">13 <go href = "counterFor.wmls#repetition()"/>14 </do>15 16 <p>17 Click OK to run script.18 </p>19 </card>20 21 <card id = "card2" title = "Repetition">22 23 <!-- soft key that links back to previous card -->24 <do type = "accept" label = "Back">25 <prev />26 </do>27 28 <p>29 $output <!-- display result -->30 </p>31 </card> 32 </wml>
fig18_4.wml
Display results.
7
2001 Prentice Hall, Inc. All rights reserved.
Outline
2001 Prentice Hall, Inc. All rights reserved.
8
18.3 for Repetition Structure
for ( var counter = 1; counter <= 6; ++counter )
Initial value of control variable Increment of c ontrol variable
C ontro l variable name Final value o f control variable fo r whic h the c ondition is true
for keyword
Loop-continuation condition
Fig. 18.5 Components of a typical for header.
2001 Prentice Hall, Inc. All rights reserved.
9
18.3 for Repetition Structure
counter <= 6true
false
++c ounter
Establish initial value of control variable
Determine if final value of contro l variable has been reac hed
Body o f loop (this may be many statements)
Increment the c ontrol variable
Body
var counter = 1
Fig. 18.6 Flowcharting a typical for repetition structure.
10
2001 Prentice Hall, Inc. All rights reserved.
Outline1 // Fig. 18.7: sumInt.wmls2 // Summation with for3 4 extern function total() 5 {6 var sum = 0; // running total of addition7 8 for ( var number = 2; number <= 10; number += 2 ) {9 sum += number;10 }11 12 WMLBrowser.setVar( "result", sum );13 WMLBrowser.go( "#result" );14 }
sumInt.wmls
Loop 10 times.
Increment counter by 2.
Calculate total.
11
2001 Prentice Hall, Inc. All rights reserved.
Outline1 <?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. 18.8 : fig18_8.wml -->6 <!-- Summation with for -->7 8 <wml>9 <card id = "index" title = "Sum of Integers">10 11 <!-- soft key that calls function total -->12 <do type = "accept" label = "Run">13 <go href = "sumInt.wmls#total()" />14 </do>15 16 <p>17 Click Run to run the script18 </p>19 </card> 20 21 <card id = "result" title = "Sum of Integers">22 23 <!-- soft key that links back to previous card -->24 <do type = "accept" label = "Home">25 <prev />26 </do>27 28 <p>29 The sum of the even integers from 2 to 10 is: $result30 </p>31 </card> 32 </wml>
fig18_8.wml
Display results.
12
2001 Prentice Hall, Inc. All rights reserved.
Outline
13
2001 Prentice Hall, Inc. All rights reserved.
Outline1 // Fig. 18.9: calcAmount.wmls2 // Calculating compound interest3 4 extern function calcAmount()5 {6 var principal = 1000; // initial amount of loan7 var rate = .05; // interest rate8 var amount; // total amount of loan9 10 for ( var year = 1; year <= 10; ++year ) {11 var compInt = ( principal * 12 Float.pow( 1.0 + rate, year ) );13 14 amount = amount + year + " | " + 15 Float.round( compInt * 100 ) / 100 + "\n";16 }17 18 WMLBrowser.setVar( "result", amount );19 WMLBrowser.refresh();20 }
calcAmount.wmls
Calculate amount.
14
2001 Prentice Hall, Inc. All rights reserved.
Outline1 <?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. 18.10 : fig18_10.wml -->6 <!-- Calculating compound interest -->7 8 <wml>9 <card id = "index" title = "Compound Interest">10 <do type = "accept" label = "Calc">11 <go href = "calcAmount.wmls#calcAmount()"/>12 </do>13 14 <p>15 Year Amount<br />16 $result <!-- display result -->17 </p>18 </card> 19 </wml>
fig18_10.wml
Display results.
15
2001 Prentice Hall, Inc. All rights reserved.
Outline
16
2001 Prentice Hall, Inc. All rights reserved.
Outline1 // Fig. 18.11: count1.wmls2 // Using the break statement3 4 extern function counter()5 {6 for ( var count = 1; count <= 10; ++count ) {7 var newCount = newCount + "count is: " + count + " \n";8 9 if ( count == "3" ){10 break; // break out of loop if count equals 311 }12 }13 14 var broke = "Broke out of loop at " + count;15 16 17 WMLBrowser.setVar( "brokeVal", broke );18 WMLBrowser.setVar( "countVal", newCount );19 WMLBrowser.go( "#card2" );20 }
count.wmls
Loop 10 times.
Break out of loop if the value of count is 3.
17
2001 Prentice Hall, Inc. All rights reserved.
Outline1 <?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. 18.12 : fig18_12.wml -->6 <!-- Using the break statement -->7 8 <wml>9 <card id = "index" title = "Break Statement">10 11 <!-- soft key that calls function counter -->12 <do type = "accept" label = "Run">13 <go href = "count1.wmls#counter()" />14 </do>15 16 <p>17 The counter starts at 118 </p>19 </card> 20 21 <card id = "card2" title = "Break Statement">22 23 <!-- soft key that links back to previous card -->24 <do type = "accept" label = "Home">25 <prev />26 </do>27 28 <p>29 30 <!-- display results -->31 $countVal <br />32 $brokeVal33 </p>34 </card> 35 </wml>
fig10_11.wml
Display results.
18
2001 Prentice Hall, Inc. All rights reserved.
Outline
19
2001 Prentice Hall, Inc. All rights reserved.
Outline1 // Fig. 18.13: count2.wmls2 // Using the break statement3 4 extern function counter()5 {6 for ( var count = 1; count <= 3; ++count ) {7 if ( count == "2" ){8 continue; // skip remaining code if count == 29 }10 11 var newCount = newCount + "count is: " + count + " \n";12 }13 14 var broke = "Used continue to skip printing 2";15 16 17 WMLBrowser.setVar( "brokeVal", broke );18 WMLBrowser.setVar( "countVal", newCount );19 WMLBrowser.go( "#card2" );20 }
count2.wmls
Skip remaining code and continue looping if the value of count is 2.
20
2001 Prentice Hall, Inc. All rights reserved.
Outline1 <?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. 18.14: fig18_14.wml -->6 <!-- Using the continue statement -->7 8 <wml>9 <card id = "index" title = "Cont Statement">10 11 <!-- soft key that calls function counter -->12 <do type = "accept" label = "Run">13 <go href = "count2.wmls#counter()" />14 </do>15 16 <p>17 The counter starts at 118 </p>19 </card>20 21 <card id = "card2" title = "Cont Statement">22 23 <!-- soft key that links back to previous card -->24 <do type = "accept" label = "Home">25 <prev />26 </do>27 28 <p>29 30 <!-- display results -->31 $countVal <br />32 $brokeVal33 </p>34 </card> 35 </wml>
fig18_14.wml
Display results.
21
2001 Prentice Hall, Inc. All rights reserved.
Outline
2001 Prentice Hall, Inc. All rights reserved.
22
18.6 Logical Operators
expression1 expression2 expression1 && expression2
false false false false true false true false false true true true Fig. 18.15 Truth table for the && (logical AND) operator.
expression1 expression2 expression1 || expression2
false false false false true true true false true true true true Fig. 18.16 Truth table for the || (logical OR) operator.
expression !expression
false true true false Fig. 18.17 Truth table for operator ! (logical negation).
23
2001 Prentice Hall, Inc. All rights reserved.
Outline1 // Fig. 18.18: operators.wmls 2 // Logical operators3 4 extern function logicalAnd()5 {6 // logical AND7 var and = "false && false: " + ( false && false ) + "\n" +8 "false && true: " + ( false && true ) + "\n" +9 "true && false: " + ( true && false ) + "\n" +10 "true && true: " + ( true && true );11 12 WMLBrowser.setVar( "operatorAnd", and );13 WMLBrowser.go( "#card2");14 } // end function logicalAnd15 16 extern function logicalOr()17 {18 // logical OR19 var or = "false || false: " + ( false || false ) + "\n" +20 "false || true: " + ( false || true ) + "\n" +21 "true || false: " + ( true || false ) + "\n" +22 "true || true: " + ( true || true );23 24 WMLBrowser.setVar( "operatorOr", or );25 WMLBrowser.go( "#card3");26 } // end function logicalOR27
operators.wmls
Evaluate using logical AND.
Evaluate using logical OR.
24
2001 Prentice Hall, Inc. All rights reserved.
Outline28 extern function logicalNot()29 {30 // logical NOT31 var not = "!false: " + ( !false ) + "\n" +32 "!true: " + ( !true );33 34 WMLBrowser.setVar( "operatorNot", not );35 WMLBrowser.go( "#card4");36 } // end function logicalNot
operators.wmls
Evaluate using logical NOT.
25
2001 Prentice Hall, Inc. All rights reserved.
Outline1 <?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. 18.19: fig18_19.wml -->6 <!-- Using logical operators -->7 8 <wml>9 <card id = "index" title = "Logical">10 <do type = "accept" label = "Run">11 <go href = "operators.wmls#logicalAnd()" />12 </do>13 14 <p>15 Click Run to run script.16 </p>17 </card> 18 19 <card id = "card2" title = "And">20 <do type = "accept" label = "Next">21 <go href = "operators.wmls#logicalOr()" />22 </do>23 24 <p>25 $operatorAnd26 </p>27 </card> 28 29 <card id = "card3" title = "Or">30 <do type = "accept" label = "Next">31 <go href = "operators.wmls#logicalNot()" />32 </do>33
fig18_19.wml
Display results of logical AND.
26
2001 Prentice Hall, Inc. All rights reserved.
Outline34 <p>35 $operatorOr36 </p>37 </card> 38 39 <card id = "card4" title = "Not">40 <do type = "accept" label = "Home">41 <go href = "#index" />42 </do>43 44 <p>45 $operatorNot46 </p>47 </card> 48 </wml>
fig18_19.wml
Display results of logical AND.
Display results of logical NOT.
2001 Prentice Hall, Inc. All rights reserved.
27
18.7 Structured Programming Summary
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
&& left to right logical AND
|| left to right logical OR
?: right to left conditional
= += -= *= /= %= right to left assignment
Fig. 18.20 Precedenc e and assoc ia tivity of the operators d iscussed so far.
2001 Prentice Hall, Inc. All rights reserved.
28
18.7 Structured Programming Summary
T
F
if struc ture(single selec tion)
TF
if/else struc ture(double se lection)
Sequence Selection
Repetition
T
F
while struc ture
T
F
for struc ture
.
.
Fig. 18.21 WMLScript’s single-entry/single-exit sequence, selection and repetition structures.
2001 Prentice Hall, Inc. All rights reserved.
29
18.7 Structured Programming Summary
Rules for Forming Structured Programs 1) Begin with the “simplest flowchart” (Fig. 15.22).
2) Any rectangle (action) can be replaced by two rectangles (actions) in sequence.
3) Any rectangle (action) can be replaced by any control structure (sequence, if, if/else, while, or for).
4) Rules 2 and 3 may be applied as often as you like and in any order.
Fig. 18.22 Rules for forming structured programs.
Fig. 18.23 The simplest flowchart.
2001 Prentice Hall, Inc. All rights reserved.
30
18.7 Structured Programming Summary
.
.
Rule 2 Rule 2 Rule 2
Fig. 18.24 Repeatedly applying rule 2 of Fig. 15.21 to the simplest flowchart.
2001 Prentice Hall, Inc. All rights reserved.
31
18.7 Structured Programming Summary
Rule 3
Rule 3Rule 3
Fig. 18.25 Applying rule 3 of Fig. 15.21 to the simplest flowchart.
2001 Prentice Hall, Inc. All rights reserved.
32
18.7 Structured Programming Summary
Stacked building blocks Nested building blocks
Overlapping build ing blocks(Illega l in struc tured programs)
Fig. 18.26 Stacked, nested and overlapped building blocks.
Fig. 18.27 An unstructured flowchart.
33
2001 Prentice Hall, Inc. All rights reserved.
Outline1 // Fig. 18.28: craps.wmls2 // Craps game3 4 // process one roll of the dice5 extern function play()6 {7 var sumOfDice = rollDice(); // sum of the dice8 var gameStatus; // "won", "lost" or "continue"9 var result; // holds the dealer's response10 11 // true if first roll12 var firstRoll = WMLBrowser.getVar( "firstRoll" ); 13 14 // holds sum of first roll15 var myPoint = WMLBrowser.getVar("myPoint"); 16 17 // first roll of the dice18 if ( firstRoll == "true" ) {19 20 if ( sumOfDice == 7 || sumOfDice == 11 ) // win on first roll21 gameStatus = "won";22 else if ( sumOfDice == 2 || sumOfDice == 3 ||23 sumOfDice == 12 ) // lose on first roll24 gameStatus = "lost";25 else26 gameStatus = "continue";27 28 myPoint = sumOfDice; // remember dice sum29 WMLBrowser.setVar( "myPoint", myPoint ) // display dice sum30 WMLBrowser.setVar( "firstRoll", "false" );31 } 32
craps.wmls
Retrieve the value of browser variable firstRoll.
Retrieve the value of browser variable myPoint.
Check to see if first roll of the game.Player wins on 7 or 11 on first roll.
Player loses on 2, 3 or 12 on first roll.
34
2001 Prentice Hall, Inc. All rights reserved.
Outline33 else {34 35 if ( sumOfDice == myPoint ) // win by making point36 gameStatus = "won";37 38 else if ( sumOfDice == 7 ) // lose by rolling seven39 gameStatus = "lost";40 else 41 gameStatus = "continue";42 } 43 44 // sets dealer response based on game status45 if ( gameStatus == "continue" ) 46 result = "Dealer says: Roll Again.";47 else if ( gameStatus == "won" ) {48 result = "Dealer says: You win!";49 WMLBrowser.setVar( "firstRoll", "true" );50 } 51 else if ( gameStatus == "lost" ) {52 result = "Dealer says: You lose!";53 WMLBrowser.setVar( "firstRoll", "true" );54 }55 56 // sets variables to be displayed in card257 WMLBrowser.setVar( "score", sumOfDice ); 58 WMLBrowser.setVar( "myPoint", myPoint );59 WMLBrowser.setVar( "dealer", result );60 61 // redirects user to card262 WMLBrowser.go( "#card2" );63 }64
craps.wmls
If the roll of the dice equals the value of the point, player wins.
If the roll of the dice equals 7, the player loses.
If the roll of the dice does not equal the point or 7, the game continues.
Display the result of current roll.
35
2001 Prentice Hall, Inc. All rights reserved.
Outline65 // returns the sum of the roll of the dice66 extern function rollDice()67 {68 var die1 = Lang.random(5) + 1;69 var die2 = Lang.random(5) + 1;70 var total = die1 + die2;71 72 var dieImage1 = "die" + die1 + ".wbmp";73 var dieImage2 = "die" + die2 + ".wbmp";74 75 WMLBrowser.setVar( "die1", die1 );76 WMLBrowser.setVar( "die2", die2 );77 WMLBrowser.setVar( "dieOne", dieImage1 );78 WMLBrowser.setVar( "dieTwo", dieImage2 );79 80 return total; 81 }
craps.wmlsGenerate two radnom numbers between 1 and to represent each die.
Generate the names of the two images to be displayed.
Calculate the total of the two die.
36
2001 Prentice Hall, Inc. All rights reserved.
Outline1 <?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. 18.29 : fig18_29.wml -->6 <!-- A craps game -->7 8 <wml>9 <card id = "index" title = "Craps Game" newcontext = "true">10 <onevent type = "onenterforward">11 <refresh>12 <setvar name = "firstRoll" value = "true" />13 </refresh>14 </onevent>15 16 <do type = "accept" label = "Roll">17 <go href = "theHouse.wmls#play()" />18 </do>19 20 <p>21 Press roll to start the game.22 </p>23 </card>24 25 <card id = "card2" title = "Results">26 <do type = "accept" label = "Roll Again">27 <go href = "theHouse.wmls#play()"/>28 </do>29 30 <do type = "options" label = "New Game">31 <go href = "#index" />32 </do>33
fig18_29.wml
37
2001 Prentice Hall, Inc. All rights reserved.
Outline34 <p>35 Die 1: <img src = "$dieOne" alt = "Die image" /><br /> 36 Die 2: <img src = "$dieTwo" alt = "Die image" /><br />37 The first die: $die1 <br />38 The second die: $die2 <br />39 The sum: $score <br />40 Point: $myPoint <br />41 $dealer 42 </p>43 </card>44 </wml>
fig18_29.wmlDisplay images of dice.
Display results of roll.