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

Slides:



Advertisements
Similar presentations
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Advertisements

1 JavaScript: Control Structures II. 2 whileCounter.html 1 2
Page 1 of 26 Javascript/Jscript Ch 7,8,9,10 Vadim Parizher Computer Science Department California State University, Northridge Spring 2003 Slides from.
A Java API Package java.security  The Java Security Package contains classes and interfaces that are required by many Java programs.  This package is.
 2002 Prentice Hall. All rights reserved Control Structures 3 control structures –Sequential structure Built into Python –Selection structure The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Chapter 3 - Structured Program Development
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
Structured programming
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Control Structures: continued.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 – Control Structures Outline 3.1 Introduction 3.2 Algorithms 3.3 Pseudocode 3.4Control Structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
1 JavaScript/Jscript 4 Functions. 2 Introduction Programs that solve real-world programs –More complex than programs from previous chapters Best way to.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 11 – JavaScript/Jscript: Functions Outline 11.1Introduction 11.2Program Modules in JavaScript.
Craps!. Example: A Game of Chance Craps simulator Rules – Roll two dice 7 or 11 on first throw, player wins 2, 3, or 12 on first throw, player loses 4,
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
1 Lecture 3 Part 1 Functions with math and randomness.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
 2003 Prentice Hall, Inc. All rights reserved.  2004 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1.
1 Lecture 4 for loops and switch statements Essentials of Counter-Controlled Repetition Counter-controlled repetition requires  Name of control.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 9 - JavaScript: Control Statements II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 23 - Wireless Internet and m- Business Outline 23.1 Introduction 23.2 M-Business 23.3 Identifying.
JavaScript: Control Structures September 27, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 16.1Introduction 16.2Program Modules in JavaScript 16.3Programmer-Defined Functions 16.4Function.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.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. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 – Methods Part I.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - JavaScript: Control Structures II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 11.1 Test-Driving the Interest Calculator Application 11.2 Essentials of Counter-Controlled Repetition.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 16 - WMLScript Functions Outline 16.1 Introduction 16.2 Program Modules in WMLScript 16.3 Programmer-Defined.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Introduction Program Components in C++ Math Library Functions Functions.
JavaScript: Control Structures I Outline 1 Introduction 2 Algorithms 3 Pseudocode 4 Control Structures 5 if Selection Structure 6 if/else Selection Structure.
Chapter 9 - JavaScript: Control Statements II
Chapter 4 – C Program Control
Chapter 10 - JavaScript: Functions
Chapter 9 - JavaScript: Control Structures II
Chapter 4 - Program Control
Chapter 3 – Control Structures
Chapter 5 – Control Structures: Part 2
Chapter 4 - Program Control
Presentation transcript:

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