2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 18 - WMLScript: Control Structures II Outline 18.1 Introduction 18.2 Essentials of Counter-Controlled Repetition 18.3 for Repetition Structure 18.4 Examples Using the for Structure 18.5 break and continue Statements 18.6 Logical Operators 18.7 Structured Programming Summary 18.8Example: A Game of Chance
2001 Prentice Hall, Inc. All rights reserved. Outline 2 1 // Fig. 18.1: counter.wmls 2 // Counter-controlled repetition 3 4 extern function repetition() 5 { 6 var counter = 1; // initialization 7 8 while ( counter <= 6 ) { // repetition condition 9 var result = result + counter + " Times through loop\n"; counter; // increment 12 } WMLBrowser.setVar( "output", result ); 15 WMLBrowser.go( "#card2" ); 16 } counter.wmls Loop 6 times.Increment counter.
2001 Prentice Hall, Inc. All rights reserved. Outline Click OK to run script $output fig18_2.wml Display results.
2001 Prentice Hall, Inc. All rights reserved. Outline 4
2001 Prentice Hall, Inc. All rights reserved. Outline 5 1 // Fig. 18.3: counterFor.wmls 2 // Counter-controlled repetition 3 4 extern function repetition() 5 { 6 // initialization, repetition condition and incrementing 7 // are all included in the for structure header 8 for ( var counter = 1; counter <= 6; ++counter ) { 9 var result = result + counter + " Times through loop\n"; 10 } WMLBrowser.setVar( "output", result ); 13 WMLBrowser.go( "#card2" ); 14 } counterFor.wmls Loop 6 times. Increment counter.
2001 Prentice Hall, Inc. All rights reserved. Outline Click OK to run script $output fig18_4.wml Display results.
2001 Prentice Hall, Inc. All rights reserved. Outline 7
2001 Prentice Hall, Inc. All rights reserved for Repetition Structure Fig. 18.5Components of a typical for header.
2001 Prentice Hall, Inc. All rights reserved for Repetition Structure Fig. 18.6Flowcharting a typical for repetition structure.
2001 Prentice Hall, Inc. All rights reserved. Outline 10 1 // Fig. 18.7: sumInt.wmls 2 // Summation with for 3 4 extern function total() 5 { 6 var sum = 0; // running total of addition 7 8 for ( var number = 2; number <= 10; number += 2 ) { 9 sum += number; 10 } WMLBrowser.setVar( "result", sum ); 13 WMLBrowser.go( "#result" ); 14 } sumInt.wmls Loop 10 times. Increment counter by 2. Calculate total.
2001 Prentice Hall, Inc. All rights reserved. Outline Click Run to run the script The sum of the even integers from 2 to 10 is: $result fig18_8.wml Display results.
2001 Prentice Hall, Inc. All rights reserved. Outline 12
2001 Prentice Hall, Inc. All rights reserved. Outline 13 1 // Fig. 18.9: calcAmount.wmls 2 // Calculating compound interest 3 4 extern function calcAmount() 5 { 6 var principal = 1000; // initial amount of loan 7 var rate =.05; // interest rate 8 var amount; // total amount of loan 9 10 for ( var year = 1; year <= 10; ++year ) { 11 var compInt = ( principal * 12 Float.pow( rate, year ) ); amount = amount + year + " | " + 15 Float.round( compInt * 100 ) / "\n"; 16 } WMLBrowser.setVar( "result", amount ); 19 WMLBrowser.refresh(); 20 } calcAmount.wmls Calculate amount.
2001 Prentice Hall, Inc. All rights reserved. Outline Year Amount 16 $result fig18_10.wml Display results.
2001 Prentice Hall, Inc. All rights reserved. Outline 15
2001 Prentice Hall, Inc. All rights reserved. Outline 16 1 // Fig : count1.wmls 2 // Using the break statement 3 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 3 11 } 12 } var broke = "Broke out of loop at " + count; 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.
2001 Prentice Hall, Inc. All rights reserved. Outline The counter starts at $countVal 32 $brokeVal fig10_11.wml Display results.
2001 Prentice Hall, Inc. All rights reserved. Outline 18
2001 Prentice Hall, Inc. All rights reserved. Outline 19 1 // Fig : count2.wmls 2 // Using the break statement 3 4 extern function counter() 5 { 6 for ( var count = 1; count <= 3; ++count ) { 7 if ( count == "2" ){ 8 continue; // skip remaining code if count == 2 9 } var newCount = newCount + "count is: " + count + " \n"; 12 } var broke = "Used continue to skip printing 2"; 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.
2001 Prentice Hall, Inc. All rights reserved. Outline The counter starts at $countVal 32 $brokeVal fig18_14.wml Display results.
2001 Prentice Hall, Inc. All rights reserved. Outline 21
2001 Prentice Hall, Inc. All rights reserved Logical Operators
2001 Prentice Hall, Inc. All rights reserved. Outline 23 1 // Fig : operators.wmls 2 // Logical operators 3 4 extern function logicalAnd() 5 { 6 // logical AND 7 var and = "false && false: " + ( false && false ) + "\n" + 8 "false && true: " + ( false && true ) + "\n" + 9 "true && false: " + ( true && false ) + "\n" + 10 "true && true: " + ( true && true ); WMLBrowser.setVar( "operatorAnd", and ); 13 WMLBrowser.go( "#card2"); 14 } // end function logicalAnd extern function logicalOr() 17 { 18 // logical OR 19 var or = "false || false: " + ( false || false ) + "\n" + 20 "false || true: " + ( false || true ) + "\n" + 21 "true || false: " + ( true || false ) + "\n" + 22 "true || true: " + ( true || true ); WMLBrowser.setVar( "operatorOr", or ); 25 WMLBrowser.go( "#card3"); 26 } // end function logicalOR 27 operators.wmls Evaluate using logical AND.Evaluate using logical OR.
2001 Prentice Hall, Inc. All rights reserved. Outline extern function logicalNot() 29 { 30 // logical NOT 31 var not = "!false: " + ( !false ) + "\n" + 32 "!true: " + ( !true ); WMLBrowser.setVar( "operatorNot", not ); 35 WMLBrowser.go( "#card4"); 36 } // end function logicalNot operators.wmls Evaluate using logical NOT.
2001 Prentice Hall, Inc. All rights reserved. Outline Click Run to run script $operatorAnd fig18_19.wml Display results of logical AND.
2001 Prentice Hall, Inc. All rights reserved. Outline $operatorOr $operatorNot fig18_19.wml Display results of logical AND.Display results of logical NOT.
2001 Prentice Hall, Inc. All rights reserved Structured Programming Summary
2001 Prentice Hall, Inc. All rights reserved Structured Programming Summary Fig WMLScript’s single-entry/single-exit sequence, selection and repetition structures.
2001 Prentice Hall, Inc. All rights reserved Structured Programming Summary Fig The simplest flowchart.
2001 Prentice Hall, Inc. All rights reserved Structured Programming Summary Fig Repeatedly applying rule 2 of Fig to the simplest flowchart.
2001 Prentice Hall, Inc. All rights reserved Structured Programming Summary Fig Applying rule 3 of Fig to the simplest flowchart.
2001 Prentice Hall, Inc. All rights reserved Structured Programming Summary Fig Stacked, nested and overlapped building blocks. Fig An unstructured flowchart.
2001 Prentice Hall, Inc. All rights reserved. Outline 33 1 // Fig : craps.wmls 2 // Craps game 3 4 // process one roll of the dice 5 extern function play() 6 { 7 var sumOfDice = rollDice(); // sum of the dice 8 var gameStatus; // "won", "lost" or "continue" 9 var result; // holds the dealer's response // true if first roll 12 var firstRoll = WMLBrowser.getVar( "firstRoll" ); // holds sum of first roll 15 var myPoint = WMLBrowser.getVar("myPoint"); // first roll of the dice 18 if ( firstRoll == "true" ) { if ( sumOfDice == 7 || sumOfDice == 11 ) // win on first roll 21 gameStatus = "won"; 22 else if ( sumOfDice == 2 || sumOfDice == 3 || 23 sumOfDice == 12 ) // lose on first roll 24 gameStatus = "lost"; 25 else 26 gameStatus = "continue"; myPoint = sumOfDice; // remember dice sum 29 WMLBrowser.setVar( "myPoint", myPoint ) // display dice sum 30 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.
2001 Prentice Hall, Inc. All rights reserved. Outline else { if ( sumOfDice == myPoint ) // win by making point 36 gameStatus = "won"; else if ( sumOfDice == 7 ) // lose by rolling seven 39 gameStatus = "lost"; 40 else 41 gameStatus = "continue"; 42 } // sets dealer response based on game status 45 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 } // sets variables to be displayed in card2 57 WMLBrowser.setVar( "score", sumOfDice ); 58 WMLBrowser.setVar( "myPoint", myPoint ); 59 WMLBrowser.setVar( "dealer", result ); // redirects user to card2 62 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.
2001 Prentice Hall, Inc. All rights reserved. Outline // returns the sum of the roll of the dice 66 extern function rollDice() 67 { 68 var die1 = Lang.random(5) + 1; 69 var die2 = Lang.random(5) + 1; 70 var total = die1 + die2; var dieImage1 = "die" + die1 + ".wbmp"; 73 var dieImage2 = "die" + die2 + ".wbmp"; WMLBrowser.setVar( "die1", die1 ); 76 WMLBrowser.setVar( "die2", die2 ); 77 WMLBrowser.setVar( "dieOne", dieImage1 ); 78 WMLBrowser.setVar( "dieTwo", dieImage2 ); return total; 81 } craps.wmls Generate 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.
2001 Prentice Hall, Inc. All rights reserved. Outline Press roll to start the game fig18_29.wml
2001 Prentice Hall, Inc. All rights reserved. Outline Die 1: 36 Die 2: 37 The first die: $die1 38 The second die: $die2 39 The sum: $score 40 Point: $myPoint 41 $dealer fig18_29.wml Display images of dice. Display results of roll.