Professional Documents
Culture Documents
Gr.8 - Switch Statement - Notes
Gr.8 - Switch Statement - Notes
➢ If the case statement does not have a break, the code for the next
cases are also executed until a break is found. This is known as fall-
through.
➢ It is not necessary to break the last case in a switch block. The block
breaks (ends) there anyway.
Program using Numeric value
<script>
var i=2; // Assigned a value to the variable text
switch (i) // Passing the variable to switch condition
{ case 1:
document.write(“Value of i = 1");
break;
case 2:
document.write(“Value of i = 2");
break;
case 3:
document.write(“Value of i = 3");
break;
default:
document.write(“Enter numbers from1-3");
break;
} </script>
Program using String value
<script>
var text="Hello"; // Assigned a value to the variable text
switch (text) // Passing the variable to switch condition
{ case "Hello 1":
document.write("Hello 1");
break;
case "Hello":
document.write("Correct Text Hello ");
break;
default:
document.write("This is default selection");
break;
} </script>
Program using fall through
Example of different cases using the same code and default not being the last case :-