">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 JavaScript: Control Structures II. 2 whileCounter.html 1 2

Similar presentations


Presentation on theme: "1 JavaScript: Control Structures II. 2 whileCounter.html 1 2 "— Presentation transcript:

1 1 JavaScript: Control Structures II

2 2 whileCounter.html 1 2 Counter-Controlled Repetition The while loop will continue until the value of counter is greater than 7. Increment the counter.

3 3 Program Output

4 4 ForCounter.html 1 2 Counter-Controlled Repetition InitializationRepetition conditionIncrementing

5 5 Program Output

6 6 For Repetition Structure for (var counter = 1 ; counter <= 7 ; ++counter ) Initial value of control variableIncrement of control variable Control variable nameFinal value of control variable for which the condition is true for keyword Loop-continuation condition Components of a typical for structure header.

7 7 Examples Using the for Structure counter <= 7 document.writeln( "

XHTML font size "+ counter + "ex " ); true false var counter = 1 ++counter Establish initial value of control variable. Determine if final value of control variable has been reached. Body of loop (this may be many statements) Increment the control variable. Flowcharting a typical for repetition structure.

XHTML font size "+ counter + "ex " ); tru">

8 8 Sum.html Sum.html Program Output 1 2 Sum the Even Integers from 2 to The for loop will continue until the value of number is greater than 100. Initialization.Repetition condition.Incrementing.

9 9 Interest.html 1 2 Calculating Compound Interest 35 Opening table element.Each iteration of the for loop creates a table row listing the year of the loan and the amount.

10 10 Interest.html Interest.html Program Output

11 11 SwitchTest.html 1 2 Switching between XHTML List Formats Click Refresh (or Reload) to run the script again If none of the case s match, variable validInput is set to false. If the user input a valid value, the list is created. Otherwise, the message “Invalid choice” is displayed in the browser.

13 13 Program Output

14 14 Program Output

15 15 switch Multiple-Selection Structure switch multiple-selection structure.

16 16 DoWhileTest.html 1 2 Using the do/while Repetition Structure Each iteration of the do / while loop writes a line of text with a header element to the XHTML document. The loop stops when the value of counter is greater than 6.

17 17 Program Output

18 18 do/while Repetition Structure condition true action(s) false Flowcharting the do/while repetition structure.

19 19 BreakTest.html 1 2 Using the break Statement in a for Structure When the value of variable count equals 5, the break statement causes program control to proceed to the first line outside the for loop.

20 20 Program Output

21 21 ContinueTest.html 1 2 Using the continue Statement in a for Structure When the value of variable count equals 5, the continue statement causes program control to proceed to the next iteration of the for loop.

22 22 Program Output

23 23 BreakLabelTest.htm 1 2 Using the break Statement with a Label

25 25 ContinueLabelTest.htm 1 2 Using the continue Statement with a Label nextRow is the label for the continue statement.If the value of variable column is greater than the value of variable row, the continue statement causes the next interation of the loop. If the continue statement is performed, method write does not print the string “* “ in the XHTML document.

26 26 Program Output

27 27 LogicalOperators.html 1 2 Demonstrating the Logical Operators These expressions demonstrate the use of logical NOT.

29 29 Summary of Structured Programming JavaScript’s single-entry/single-exit sequence, selection and repetition structures.

30 30 Summary of Structured Programming Unstructured flowchart.

31 31 JavaScript: Functions

32 32 Program Modules in JavaScript main worker1worker2worker3 worker4worker5 Hierarchical boss-function/worker-function relationship.

33 33 SquareInt.html 1 2 A Programmer-Defined square Function Calling function square and passing it the value of x.Variable y gets the value of variable x. The return statement passes the value of y * y back to the calling function.

34 34 Program Output

35 35 Maximum.html 1 2 Finding the Maximum of Three Values Click Refresh (or Reload) to run the script again 43 44

37 37 Program Output

38 38 RandomInt.html 1 2 Shifted and Scaled Random Integers The for loop creates 4 rows with 5 cells of a table.Each cell is populated with a random number generated by method random. Method floor rounds the number generated by method random down.

39 39 RandomInt.html RandomInt.html Program Output Click Refresh (or Reload) to run the script again 38 39

40 40 RollD.html 1 2 Roll a Six-Sided Die 6000 Times Click Refresh (or Reload) to run the script again The results of the dice being rolled 600 times are displayed in a table.

43 43 Craps.html 1 2 Program that Simulates the Game of Craps Craps 94 Die Die Sum Point Function rollDice is called to simulate the rolling of two dice on the craps table. Methods random and floor are used to generate the values for the two dice. Referencing the names of form elements in the XHTML document, the vlaues of the dice are placed in their respective form fields.

46 46 Craps.html Craps.html Program Output 106

47 47 Program Output

48 48 Program Output

49 49 Scoping.html 1 2 A Scoping Example The value of x is incremented. Function functionB multiplies the value of x by 10.

51 51 Program Output

52 52 JavaScript Global Functions

53 53 JavaScript Global Functions

54 54 Recursion 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 (a) Procession of recursive calls.(b) Values returned from each recursive call. 5! = 5 * 24 = 120 is returned 4! = 4 * 6 = 24 is returned 2! = 2 * 1 = 2 is returned 3! = 3 * 2 = 6 is returned 1 returned Recursive evaluation of 5!. Final value = 120

55 55 FactorialTest.html 1 2 Recursive Factorial Function document.writeln( " Factorials of 1 to 10 " ); 13 document.writeln( 14 " " ); for ( var i = 0; i <= 10; i++ ) 17 document.writeln( " " + i + "! " + 18 factorial( i ) + " " ); document.writeln( " " ); // Recursive definition of function factorial 23 function factorial( number ) 24 { 25 if ( number <= 1 ) // base case 26 return 1; 27 else 28 return number * factorial( number - 1 ); 29 } Calling function factorial and passing it the value of i. Variable number gets the value of variable i.Call to function factorial and passing it 1 less than the current value of number.

56 56 Program Output

57 57 FibonacciTest.html 1 2 Recursive Fibonacci Function // Event handler for button XHTML component in myForm 14 function getFibonacciValue() 15 { 16 var value = parseInt( 17 document.myForm.number.value ); 18 window.status = 19 "Calculating Fibonacci number for " + value; 20 document.myForm.result.value = fibonacci( value ); 21 window.status = "Done calculating Fibonacci number"; 22 } // Recursive definition of function fibonacci 25 function fibonacci( n ) 26 { 27 if ( n == 0 || n == 1 ) // base case 28 return n; 29 else 30 return fibonacci( n - 1 ) + fibonacci( n - 2 ); 31 } Convert from a string to an integer the value the user typed into the number text field. Display the number the user entered in the status bar. The status bar displays a message that the call to function fibonacci is complete. Test for base case ( n equal to 1 or 0 ). Two recursive calls are made if n is greater than 1.

58 58 FibonacciTest.html FibonacciTest.html Program Output Enter an integer

59 59 Program Output

60 60 Example Using Recursion: Fibonacci Series Set of recursive calls to function fibonacci.

61 61 JavaScript: Arrays

62 62 Arrays c[ 6 ] Name of array (Note that all elements of this array have the same name, c ) c[ 0 ] c[ 1 ] c[ 2 ] c[ 3 ] c[ 11 ] c[ 10 ] c[ 9 ] c[ 8 ] c[ 7 ] c[ 5 ] c[ 4 ] Position number (index or subscript) of the element within array c A 12-element array.

63 63 InitArray.html 1 2 Initializing an Array The first time function ouputArray is called, variable header gets the value of “Array n1 contains ” and variable theArray gets the value of n1. The second time function ouputArray is called, variable header gets the value of “Array n2 contains ” and variable theArray gets the value of n2.

65 65 Program Output

66 66 InitArray2.html 1 2 Initializing an Array with a Declaration

68 68 Program Output

69 69 SumArray.html 1 2 Sum the Elements of an Array The for loop sums the values contained in the 10- element integer array called theArray. Variable element is assigned a subscript in the range of 0 up to, but not including, theArray.length.

70 70 Program Output

71 71 RollDie.html 1 2 Roll a Six-Sided Die 6000 Times Referencing Array frequency replaces the switch statement used in Chapter 10’s example.

72 72 RollDie.html RollDie.html Program Output Click Refresh (or Reload) to run the script again 39 40

73 73 PassArray.html 1 2 Passing Arrays and Individual Array 11 Elements to Functions Method join takes as its argument a string containing a separator that should be used to separate the elements of the array in the string that is returned. Multiply each element in theArray by 2.

75 75 Program Output

76 76 Sort.html 1 2 Sorting an Array with Array Method sort

78 78 LinearSearch.htm 1 2 Linear Search of an Array Enter integer search key Result Function linearSearch compares each each element with a search key. Variable theArray gets the value of Array a and variable key gets the value of variable searchKey.

80 80 Program Output

81 81 BinarySearch.htm 1 2 Binary Search Enter integer search key 96 Result Function buildOutput creates the markup that displays the results of the search.

84 84 Program Output

85 85 Multiple-Subscripted Arrays Double-subscripted array with three rows and four columns.

86 86 InitArray3.html 1 2 Initializing Multidimensional Arrays Referencing the multidimensional array theArray.

88 88 End of Lecture


Download ppt "1 JavaScript: Control Structures II. 2 whileCounter.html 1 2 "

Similar presentations


Ads by Google