Syntax switch expression case n1 statements break default statements

Syntax switch expression case n1 statements break

This preview shows page 52 - 64 out of 92 pages.

Syntax: switch (expression) { case n1: statements break ; default : statements } ExampleCode:
Image of page 52
JavaScript Looping Statements JavaScript has three types of loops: for , while , and do while . The for syntax for: for (statement 1; statement 2; statement 3) { //code block to be executed } Example: for (; i < 10; ) { document.write(i); i++; } . The while loop repeats through a block of code, as long as a specified condition is true . Syntax: while (condition) { code block }
Image of page 53
JavaScript Looping Statements The Do...While Loop The for syntax : do { code block } while (condition); Syntax: while (condition) { code block }
Image of page 54
average.html (1 of 3) 1 <?xml version = "1.0" ?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "" > 4 5 <!-- Fig. 8.7: average.html --> 6 <!-- Class Average Program --> 7 8 <html xmlns = "" > 9 <head> 10 <title> Class Average Program </title> 11 12 <script type = "text/javascript" > 13 <!-- 14 var total, // sum of grades 15 gradeCounter, // number of grades entered 16 gradeValue, // grade value 17 average, // average of all grades 18 grade; // grade typed by user 19 20 // Initialization Phase 21 total = 0 ; // clear total 22 gradeCounter = 1 ; // prepare to loop 23
Image of page 55
average.html (2 of 3) 24 // Processing Phase 25 while ( gradeCounter <= 10 ) { // loop 10 times 26 27 // prompt for input and read grade from user 28 grade = window.prompt( "Enter integer grade:" , "0" ); 29 30 // convert grade from a string to an integer 31 gradeValue = parseInt( grade ); 32 33 // add gradeValue to total 34 total = total + gradeValue; 35 36 // add 1 to gradeCounter 37 gradeCounter = gradeCounter + 1 ; 38 } 39 40 // Termination Phase 41 average = total / 10 ; // calculate the average 42 43 // display average of exam grades 44 document.writeln( 45 "<h1>Class average is " + average + "</h1>" ); 46 // --> 47 </script>
Image of page 56
48 49 </head> 50 <body> 51 <p> Click Refresh (or Reload) to run the script again <p> 52 </body> 53 </html>
Image of page 57
1 <?xml version = "1.0" ?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "" > 4 5 <!-- Fig. 9.7: SwitchTest.html --> 6 <!-- Using the switch statement --> 7 8 <html xmlns = "" > 9 <head> 10 <title> Switching between XHTML List Formats </title> 11 12 <script type = "text/javascript" > 13 <!-- 14 var choice, // user’s choice 15 startTag, // starting list item tag 16 endTag, // ending list item tag 17 validInput = true , // indicates if input is valid 18 listType; // list type as a string 19 20 choice = window.prompt( "Select a list style:\n" + 21 "1 (bullet), 2 (numbered), 3 (lettered)" , "1" ); 22
Image of page 58
SwitchTest.html (2 of 3) 23 switch ( choice ) { 24 case "1" : 25 startTag = "<ul>" ; 26 endTag = "</ul>" ; 27 listType = "<h1>Bullet List</h1>" ; 28 break ; 29 case "2" : 30 startTag = "<ol>" ; 31 endTag = "</ol>" ; 32 listType = "<h1>Ordered List: Numbered</h1>" ; 33 break ; 34 case "3" : 35 startTag = "<ol type = \"A\">" ; 36 endTag = "</ol>" ; 37 listType = "<h1>Ordered List: Lettered</h1>" ; 38 break ; 39 default : 40 validInput = false ; 41 } 42 43 if ( validInput == true ) { 44 document.writeln( listType + startTag ); 45 46 for ( var i = 1 ; i <= 3 ; ++i ) 47 document.writeln( "<li>List item " + i + "</li>" );
Image of page 59
SwitchTest.html (3 of 3) 48 49 document.writeln( endTag ); 50 } 51 else 52 document.writeln( "Invalid choice: " + choice ); 53 // --> 54 </script> 55 56 </head> 57 <body> 58 <p> Click Refresh (or Reload) to run the script again </p> 59 </body> 60 </html>
Image of page 60
Image of page 61
Image of page 62
BreakTest.html (1 of 2) 1 <?xml version = "1.0" ?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "" > 4 5 <!-- Fig. 9.11: BreakTest.html
Image of page 63
Image of page 64

You've reached the end of your free preview.

Want to read all 92 pages?

  • Fall '19
  • Document Object Model

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes