Javascript - Switch Case

switch (expression) {
   case condition 1: statement(s)
   break;
 
   case condition 2: statement(s)
   break;
   ...
 
   case condition n: statement(s)
   break;
 
   default: statement(s)
}

<html>
   <body> 
      <script type = "text/javascript">
         <!--
            var grade = 'A';
            document.write("Entering switch block<br />");
            switch (grade) {
               case 'A': document.write("Good job<br />");
               break;
           
               case 'B': document.write("Pretty good<br />");
               break;
           
               case 'C': document.write("Passed<br />");
               break;
           
               case 'D': document.write("Not so good<br />");
               break;
           
               case 'F': document.write("Failed<br />");
               break;
           
               default:  document.write("Unknown grade<br />")
            }
            document.write("Exiting switch block");
         //-->
      </script>     
      <p>Set the variable to different value and then try...</p>
   </body>
</html>

Entering switch block
Good job
Exiting switch block
Set the variable to different value and then try...


<html>
   <body>     
      <script type = "text/javascript">
         <!--
            var grade = 'A';
            document.write("Entering switch block<br />");
            switch (grade) {
               case 'A': document.write("Good job<br />");
               case 'B': document.write("Pretty good<br />");
               case 'C': document.write("Passed<br />");
               case 'D': document.write("Not so good<br />");
               case 'F': document.write("Failed<br />");
               default: document.write("Unknown grade<br />")
            }
            document.write("Exiting switch block");
         //-->
      </script>     
      <p>Set the variable to different value and then try...</p>
   </body>
</html>

Entering switch block
Good job
Pretty good
Passed
Not so good
Failed
Unknown grade
Exiting switch block
Set the variable to different value and then try...

No comments:

Post a Comment

Welcome

Lets learn java script