Page 323 - Web Applications (803) Class 11
P. 323

The syntax of the switch statement is given the following:
                   switch(choice)
                   {
                   case 1:
                   Statements of block 1;
                   break;
                   case 2:
                   Statements of block 2;
                   break;
                   .
                   .
                   .
                   default:
                   Statements of block last;
                   }
                 Example:

                   <HTML>
                   <BODY>
                   <SCRIPT>
                   var grade = prompt("Enter Grade");
                   document.write("Grade Entered:"+grade+"<BR> <BR>");
                   switch (grade) {
                       case 'A': document.write("You get a bonus of 5000 rupees<br>");
                                  break;
                       case 'B': document.write("You get a bonus of 2500 rupees<br>");
                                  break;
                       case 'C': document.write("You get a bonus of 1000 rupees<br>");
                                  break;
                       case 'D': document.write("You get a bonus of 500 rupees<br>");
                                  break;
                       default:  document.write("Invalid grade<br>")
                               }
                   document.write("Switch block ends.......");
                   </SCRIPT>
                   </BODY>
                   </HTML>
                 Output:























                                                                      Introduction to Dynamic Websites Using JavaScript  321
   318   319   320   321   322   323   324   325   326   327   328