As your programming skills grow, you move from small projects to bigger projects. As your projects get bigger, so does the amount of code they use. Even if the project does require a good amount of code, you will begin to look for shortcuts or faster ways to achieve a goal in the program. One such shortcut is the “Switch” statement. Using this statement in your program will essentially remove the need for a mess of “If…Else” statements. The ideal place for a “Switch” statement is in a program that offers the user options of what to do.
for example:
<script type= “text/javascript”>
var x= 2
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>
The example may be too simple, but it effectively shows the point. The value of variable ‘x’ is 2, so when the “Switch” statement is reached, it will jump to “case 2” and execute the code. The break statements after each case ensure that the code does not keep running through to the next cases.
1d42caef-592b-462f-9878-3870eb96b870|0|.0