Switch Statement
Switch Statement
The switch statement is used to perform different action based on different conditions
switch (condition)
{
case condition 1: code to be executed if condition is true
break
case condition 2: code to be executed if condition is true
break
…
…
…
case condition n: code to be executed if condition is true
break
default: code to be executed if condition is true
Example: 1
Write any javascript application for switch statement
<html><head><title></title>
<script language="javascript">
var a=2
switch(a)
{
case 1:
document.write("One")
break
case 2:
document.write("Two")
break
case 3:
document.write("Three")
break
default:
document.write("Not One Two Three")
}
</script>
</head>
<body>
</body>
</html>
Output:
Two
Example: 2
<html><head><title></title>
<script language="javascript">
var a=9
switch(a)
{
case 1:
document.write("January")
break
case 2:
document.write("February")
break
case 3:
document.write("March")
break
case 4:
document.write("April")
break
case 5:
document.write("May")
break
case 6:
document.write("June")
break
case 7:
document.write("July")
break
case 8:
document.write("August")
break
case 9:
document.write("September")
break
case 10:
document.write("Octomber")
break
case 11:
document.write("November")
break
case 12:
document.write("December")
break
default:
document.write("Please enter proper number between 1 to 12")
}
</script>
</head>
<body>
</body>
</html>
Output:
September