27. October 2009 18:43
Another useful programming tool is the Switch Statement. The beauty of the Switch statement is that only one condition is checked. The result of that condition will lead to a portion of code to be executed. There is no need to go through a chain of “If Else” statements. So if your program will have a lot of possible choices, consider a Switch Statement.
i.e.
<script type= “text/javascript”>
var x= 3
switch(x)
{
case 1:
document.write(“One”);
break;
case 2:
document.write(“Two”);
break;
case 3:
document.write(“Three”);
break;
default:
document.write(“Not One, Two, or Three”);
}
</script>
Yes this example probably simpler than what you would use a Switch statement for, but it shows you the point. Variable ‘x’ is equal to three, so the Switch statement will jump to case 3 and print “Three” on the screen. The “break” statement is used after each case to prevent it from running through the code in the following cases.
8bc40ef3-8982-4e3c-ae55-df8f561809d6|0|.0