Thursday, 16 October 2014

Switch Case in JavaScript

<script type="text/javascript">
<!--
var result='A';
document.write("I am entering in switch block<br />");
switch (grade)
{
  case 'A': document.write("Good<br />");
            break;
  case 'B': document.write("Pretty<br />");
            break;
  case 'C': document.write("Passed<br />");
            break;
  case 'D': document.write("Not so<br />");
            break;
  case 'F': document.write("Failed<br />");
            break;
  default:  document.write("NA<br />")
}
document.write("I am Exiting from switch block");
//-->
</script>
 
This will produce the following result:
 
I am entering in switch block
Good
I am Exiting from switch block
 
if you do not use Break 

<script type="text/javascript">
<!--
var result='A';
document.write("I am entering in switch block<br />");
switch (grade)
{
  case 'A': document.write("Good<br />");            
  case 'B': document.write("Pretty<br />");            
  case 'C': document.write("Passed<br />");            
  case 'D': document.write("Not so<br />");            
  case 'F': document.write("Failed<br />");            
  default:  document.write("NA<br />")
}
document.write("I am Exiting from switch block");
//-->
</script>
 
This will produce the following Result:
 
I am entering in switch block
Good 
Pretty
Passed 
Not so
Failed 
NA
I am Exiting from switch block 
 

No comments:

Post a Comment