Page 366 - Webapplication11_C11_Flipbook
P. 366
The switch Statement
Due to use of multiple if statements, the program becomes more complex and lengthier. To avoid such problem,
JavaScript provides switch statement. The switch statement executes certain statements depending on user’s choice.
The case statement is used to define different cases where each case has a unique label and a set of statements
associated with it.
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>
364 Touchpad Web Applications-XI

