Page 194 - Web Applications (803) Class 12
P. 194

Syntax:

                  switch(expression)
                  {
                    case 1:
                      // code block
                      break;
                     ---------------
                     ---------------
                    case n:
                      // code block
                      break;
                    default:
                      // code block
                  }
              Example:

                  <html>
                  <body>
                  <script>
                  var val=12;
                  switch(val)
                  {
                  case 10:document.write("Smaller Value. It's 10");
                          break;
                  case 12:document.write("Exact Value Matched. It's 12");
                          break;
                  case 14:document.write("Larger Value. It's 14");
                          break;
                  default:document.write("Invalid");
                  }
                  </script>
                  </body>
                  </html>
              Output of the preceding program is as follows:











              The break Statement

              JavaScript exits the switch block when it reaches the break keyword. This will bring the execution of the
              switch block to a halt. The last case in a switch block does not need a break statement. In any case, the block
              terminates there.





                192   Touchpad Web Applications-XII
   189   190   191   192   193   194   195   196   197   198   199