Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 18 - WMLScript: Control Structures II Outline 18.1 Introduction 18.2 Essentials of Counter-Controlled.

Similar presentations


Presentation on theme: " 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 18 - WMLScript: Control Structures II Outline 18.1 Introduction 18.2 Essentials of Counter-Controlled."— Presentation transcript:

1  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

2  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"; 10 11 ++counter; // increment 12 } 13 14 WMLBrowser.setVar( "output", result ); 15 WMLBrowser.go( "#card2" ); 16 } counter.wmls Loop 6 times.Increment counter.

3  2001 Prentice Hall, Inc. All rights reserved. Outline 3 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Click OK to run script. 18 19 20 21 22 23 24 25 26 27 28 29 $output 30 31 32 fig18_2.wml Display results.

4  2001 Prentice Hall, Inc. All rights reserved. Outline 4

5  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 } 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. Outline 6 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Click OK to run script. 18 19 20 21 22 23 24 25 26 27 28 29 $output 30 31 32 fig18_4.wml Display results.

7  2001 Prentice Hall, Inc. All rights reserved. Outline 7

8  2001 Prentice Hall, Inc. All rights reserved. 8 18.3 for Repetition Structure Fig. 18.5Components of a typical for header.

9  2001 Prentice Hall, Inc. All rights reserved. 9 18.3 for Repetition Structure Fig. 18.6Flowcharting a typical for repetition structure.

10  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 } 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. Outline 11 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Click Run to run the script 18 19 20 21 22 23 24 25 26 27 28 29 The sum of the even integers from 2 to 10 is: $result 30 31 32 fig18_8.wml Display results.

12  2001 Prentice Hall, Inc. All rights reserved. Outline 12

13  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( 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. Outline 14 1 2 4 5 6 7 8 9 10 11 12 13 14 15 Year Amount 16 $result 17 18 19 fig18_10.wml Display results.

15  2001 Prentice Hall, Inc. All rights reserved. Outline 15

16  2001 Prentice Hall, Inc. All rights reserved. Outline 16 1 // Fig. 18.11: 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 } 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. Outline 17 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 The counter starts at 1 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $countVal 32 $brokeVal 33 34 35 fig10_11.wml Display results.

18  2001 Prentice Hall, Inc. All rights reserved. Outline 18

19  2001 Prentice Hall, Inc. All rights reserved. Outline 19 1 // Fig. 18.13: 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 } 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. Outline 20 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 The counter starts at 1 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $countVal 32 $brokeVal 33 34 35 fig18_14.wml Display results.

21  2001 Prentice Hall, Inc. All rights reserved. Outline 21

22  2001 Prentice Hall, Inc. All rights reserved. 22 18.6 Logical Operators

23  2001 Prentice Hall, Inc. All rights reserved. Outline 23 1 // Fig. 18.18: 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 ); 11 12 WMLBrowser.setVar( "operatorAnd", and ); 13 WMLBrowser.go( "#card2"); 14 } // end function logicalAnd 15 16 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 ); 23 24 WMLBrowser.setVar( "operatorOr", or ); 25 WMLBrowser.go( "#card3"); 26 } // end function logicalOR 27 operators.wmls Evaluate using logical AND.Evaluate using logical OR.

24  2001 Prentice Hall, Inc. All rights reserved. Outline 24 28 extern function logicalNot() 29 { 30 // logical NOT 31 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. Outline 25 1 2 4 5 6 7 8 9 10 11 12 13 14 15 Click Run to run script. 16 17 18 19 20 21 22 23 24 25 $operatorAnd 26 27 28 29 30 31 32 33 fig18_19.wml Display results of logical AND.

26  2001 Prentice Hall, Inc. All rights reserved. Outline 26 34 35 $operatorOr 36 37 38 39 40 41 42 43 44 45 $operatorNot 46 47 48 fig18_19.wml Display results of logical AND.Display results of logical NOT.

27  2001 Prentice Hall, Inc. All rights reserved. 27 18.7 Structured Programming Summary

28  2001 Prentice Hall, Inc. All rights reserved. 28 18.7 Structured Programming Summary Fig. 18.21WMLScript’s single-entry/single-exit sequence, selection and repetition structures.

29  2001 Prentice Hall, Inc. All rights reserved. 29 18.7 Structured Programming Summary Fig. 18.23The simplest flowchart.

30  2001 Prentice Hall, Inc. All rights reserved. 30 18.7 Structured Programming Summary Fig. 18.24Repeatedly applying rule 2 of Fig. 15.21 to the simplest flowchart.

31  2001 Prentice Hall, Inc. All rights reserved. 31 18.7 Structured Programming Summary Fig. 18.25Applying rule 3 of Fig. 15.21 to the simplest flowchart.

32  2001 Prentice Hall, Inc. All rights reserved. 32 18.7 Structured Programming Summary Fig. 18.26Stacked, nested and overlapped building blocks. Fig. 18.27An unstructured flowchart.

33  2001 Prentice Hall, Inc. All rights reserved. Outline 33 1 // Fig. 18.28: 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 10 11 // true if first roll 12 var firstRoll = WMLBrowser.getVar( "firstRoll" ); 13 14 // holds sum of first roll 15 var myPoint = WMLBrowser.getVar("myPoint"); 16 17 // first roll of the dice 18 if ( firstRoll == "true" ) { 19 20 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"; 27 28 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.

34  2001 Prentice Hall, Inc. All rights reserved. Outline 34 33 else { 34 35 if ( sumOfDice == myPoint ) // win by making point 36 gameStatus = "won"; 37 38 else if ( sumOfDice == 7 ) // lose by rolling seven 39 gameStatus = "lost"; 40 else 41 gameStatus = "continue"; 42 } 43 44 // 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 } 55 56 // sets variables to be displayed in card2 57 WMLBrowser.setVar( "score", sumOfDice ); 58 WMLBrowser.setVar( "myPoint", myPoint ); 59 WMLBrowser.setVar( "dealer", result ); 60 61 // 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.

35  2001 Prentice Hall, Inc. All rights reserved. Outline 35 65 // 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; 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.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.

36  2001 Prentice Hall, Inc. All rights reserved. Outline 36 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Press roll to start the game. 22 23 24 25 26 27 28 29 30 31 32 33 fig18_29.wml

37  2001 Prentice Hall, Inc. All rights reserved. Outline 37 34 35 Die 1: 36 Die 2: 37 The first die: $die1 38 The second die: $die2 39 The sum: $score 40 Point: $myPoint 41 $dealer 42 43 44 fig18_29.wml Display images of dice. Display results of roll.


Download ppt " 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 18 - WMLScript: Control Structures II Outline 18.1 Introduction 18.2 Essentials of Counter-Controlled."

Similar presentations


Ads by Google