Java Script Control Structures 1 Control Structures Flowcharting

  • Slides: 19
Download presentation
Java. Script: Control Structures 1

Java. Script: Control Structures 1

Control Structures Flowcharting Java. Script’s sequence structure. 2

Control Structures Flowcharting Java. Script’s sequence structure. 2

if/else Selection Structure Flowcharting the single-selection if structure. 3

if/else Selection Structure Flowcharting the single-selection if structure. 3

if/else Selection Structure false print “Failed” grade >= 60 true print “Passed” Flowcharting the

if/else Selection Structure false print “Failed” grade >= 60 true print “Passed” Flowcharting the double-selection if/else structure. 4

Counter-Controlled Repetition product <= 1000 true false Flowcharting the while repetition structure. 5

Counter-Controlled Repetition product <= 1000 true false Flowcharting the while repetition structure. 5

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <? xml version = "1. 0"? > <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Strict//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -strict. dtd" > <!-- average. html <!-- Class Average Program --> <html xmlns = "http: //www. w 3. org/1999/xhtml" > <head> <title>Class Average Program</title> The while loop will execute the statements in "text/javascript"> the body of the loop until the value of grade. Counter equals 10. // sum of grades <script type = <!-var total, grade. Counter, grade. Value, average, grade; // // number of grades entered grade value average of all grades grade typed by user // Initialization Phase Prompt for the total = 0; // clear total grade. Counter = 1; // prepare to loop // Processing Phase while ( grade. Counter <= 10 ) { Average. html user input a grade. // loop 10 times Convert an integer. // prompt for input and read grade input from to user grade = window. prompt( "Enter integer grade: ", "0" ); // convert grade from a string to an integer grade. Value = parse. Int( grade ); Add new grade to total. // add grade. Value to total = total + grade. Value; 6

36 37 38 39 40 41 42 43 44 45 46 47 48 49

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 // add 1 to grade. Counter = grade. Counter + 1; } // Termination Phase average = total / 10; Increment the counter. // calculate the average // display average of exam. Calculate grades the average of the document. writeln( input by the user. "<h 1>Class average is " + average + "</h 1>" ); // --> </script> grades Average. html Write the result to the XHTML document. </head> <body> <p>Click Refresh (or Reload) to run the script again <p> </body> </html> Program Output 7

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <? xml version = "1. 0"? > <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Strict//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -strict. dtd" > <!-- Average 2. html --> <!-- Sentinel-controlled Repetition --> <html xmlns = "http: //www. w 3. org/1999/xhtml" > <head> <title>Class Average Program: Sentinel-controlled Repetition </title> <script type = "text/javascript"> <!-var grade. Counter, // number of grades entered grade. Value, // grade value total, // sum of grades average, // average of all grades grade; // grade typed by user Average 2. html Prompt for the user to enter a grade, -1 to end. // Initialization phase total = 0; // clear total grade. Counter = 0; // prepare to loop // Processing phase. The while loop will continue until // prompt for inputequals and read – 1. grade from user grade = window. prompt( "Enter Integer Grade, -1 to Quit: " , "0" ); the user input // convert grade from a string to an integer grade. Value = parse. Int( grade ); while ( grade. Value != -1 ) { // add grade. Value to total = total + grade. Value; 8

36 37 38 39 40 41 42 43 44 45 46 47 48 49

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 // add 1 to grade. Counter = grade. Counter + 1; // prompt for input and read grade from user grade = window. prompt( "Enter Integer Grade, -1 to Quit: " , "0" ); // convert grade from. Each a string to of anthe integer iteration loop will grade. Value = parse. Int( grade ); } open a prompt dialog allowing the user to input another grade. // Termination phase if ( grade. Counter != 0 ) { average = total / grade. Counter; // display average of exam grades document. writeln( "<h 1>Class average is " + average + "</h 1>" ); Average 2. html } else document. writeln( "<p>No grades were entered</p>" ); // --> </script> </head> <body> <p>Click Refresh (or Reload) to run the script again </p> </body> </html> 9

Program Output 10

Program Output 10

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <? xml version = "1. 0"? > <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Strict//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -strict. dtd" > <!-- analysis. html <!-- Analyzing Exam Results --> The while loop will continue until the value of "http: //www. w 3. org/1999/xhtml" > student is 10 meaning 10 results were entered. <html xmlns = <head> <title>Analysis of Examination Results </title> Entering a 1 into the prompt dialog means the <script type = "text/javascript"> <!-student passed the exam. A value of 2 means the // initializing variables in declarations student failed. var passes = 0, // number of passes failures = 0, // number of failures student = 1, // student counter result; // one exam result Analysis. html // process 10 students; counter-controlled loop while ( student <= 10 ) { result = window. prompt( If a value of 1 was "Enter result (1=pass, 2=fail)", "0" ); if ( result == "1" ) passes = passes + 1; else failures = failures + 1; entered, the value of passes is incremented by one, otherwise, failures is incremented. student = student + 1; } 11

33 34 35 36 37 38 39 40 41 42 43 44 45 46

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 // termination phase document. writeln( "<h 1>Examination Results</h 1>" ); document. writeln( "Passed: " + passes + " Failed: " + failures ); if ( passes > 8 ) document. writeln( " Raise Tuition" ); // --> </script> If more than 8 students passed the </head> <body> to “Raise Tuition”. <p>Click Refresh (or Reload) to run the script again </p> </body> </html> exam, the program says Analysis. html 12

Program Output 13

Program Output 13

Increment and Decrement Operators 14

Increment and Decrement Operators 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <? xml version = "1. 0"? > <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Strict//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -strict. dtd" > <!-- increment. html --> <!-- Preincrementing and Postincrementing --> <html xmlns = "http: //www. w 3. org/1999/xhtml" > <head> <title>Preincrementing and Postincrementing</title> <script type = "text/javascript"> <!-var c; Postincrementing the variable will print the variable and then increment the value by one. c = 5; document. writeln( "<h 3>Postincrementing</h 3>" ); document. writeln( c ); // print 5 then increment document. writeln( " " + c++ ); document. writeln( " " + c ); // print 6 c = 5; document. writeln( // increment then document. writeln( // --> </script> Increment. html Preincrementing the variable will increment the value by); one and then print the value. "<h 3>Preincrementing</h 3>" c ); // print 5 print 6 " " + ++c ); " " + c ); // print 6 </head><body></body> </html> 15

Program Output 16

Program Output 16

Split() The split method is a handy way to "split" a string into two

Split() The split method is a handy way to "split" a string into two or more parts based on a character that divides the parts. The character that divides the parts could be many things -- a comma, a slash, a symbol ( | ), or another of your choice. 17

Split() example <SCRIPT language=“Java. Script”> function divide_string() { var where_is_mytool="home/mytool. cgi"; var mytool_array=where_is_mytool. split(“/”);

Split() example <SCRIPT language=“Java. Script”> function divide_string() { var where_is_mytool="home/mytool. cgi"; var mytool_array=where_is_mytool. split(“/”); alert(mytool_array[0]+” “+mytool_array[1]+” “+mytool_array[2]); } </SCRIPT> <FORM> <INPUT TYPE=“button” on. Click=“divide_string()” value=“Go!”> </FORM> Split. html 18

End of Lecture • Next time, more Java. Script! 19

End of Lecture • Next time, more Java. Script! 19