|
JavaScript : Switch Case
Switch case statement is used when we have to execute a section of code
depending upon the satisfied condition and there are many different conditions.
The basic syntax of the switch statement is to give an expression to evaluate and
several different statements to execute based on the value of the expression. The interpreter
checks each case against the value of the expression until a match is found. If nothing matches,
a default condition will be used.
switch (expression)
{
case condition 1: statement(s)
break;
case condition 2: statement(s)
break;
...
case condition n: statement(s)
break;
default: statement(s)
}
The break statements indicate to the interpreter the end of that
particular case.
<script type="text/javascript">
<!--
var grade='A';
document.write("Switch block ");
switch (grade)
{
case 'A': document.write("Very Good <br />");
break;
case 'B': document.write("Good<br />");
break;
case 'C': document.write("Passed<br />");
break;
case 'D': document.write("Not Satisfactory<br />");
break;
case 'F': document.write("Failed<br />");
break;
default: document.write("Unknown grade<br />")
}
document.write("End Switch block");
//-->
</script>
|