JavaScript Switch Statement
Javascript switch statements are a form of control statement similar to if-else statements. They determine whether an expression evaluates to one of many possible values, then execute instructions based on that value.
The switch statement uses reserved words to identify each of its parts:
switch — the switch statement
case — identifies a valid value for the expression the statement evaluates
default — special case used when an expression evaluates to an invalid value
break — terminates execution of instructions within a case
- The switch statement evaluates an expression and executes the corresponding body.
- The syntax of the switch statement is:
- If the result of the expression is equal to value1, its body is executed.
- If the result of the expression is equal to value2, its body is executed.
- This process goes on. If there is no matching case, the default body executes.
Syntax Example
<body>
<button onclick="fun()">click me</button>
<script type="text/javascript">
function fun()
{
var w =prompt("enter your number")
var day;
switch(w)
{
case'1':
day="Sunday";
break;
case'2':
day="Monday";
break;
case'3':
day="Tuesday";
break;
case'4':
day="Wednesday";
break;
case'5':
day="Thursday";
break;
case'6':
day="Friday";
break;
case'7':
day="Saturday";
break;
default:
day="enter valid value";
}
document.write(day);
}
</script>
</body>
SWITCH EXAMPLE(WITH BREAK)
Unlike an if else statement, a switch statement does not stop evaluating its values once it finds a match for the expression. Unless you force execution to stop with the reserved word “break,” a switch statement will continue evaluating cases after the matched case.
In the code below, if the expression evaluated to 1, the instructions under case 1, case 2, and the default case would all be executed.
<body>
<button onclick="fun()">click me </button>
<script type="text/javascript">
function fun()
{
var w =prompt("enter your number");
var txt;
switch(w)
{
case'1':
case'2':
case'3':
case'4':
case'5':
case'6':
txt ="ok happy";
break;
case'101':
case'102':
case'103':
case'104':
case'105':
txt ="ok done";
break;
default:
txt ="not good";
}
document.write(txt);
}
</script>
</body>
HELP OF SWITCH MAKE A MINI CALCULATOR
Switch statements are always evaluated from top to bottom. The order of cases does not matter as long as you always have the reserved word “break” at the end of all cases that shouldn’t have fall-through.
The result of the expression in the switch statement must have the same type and value as a case in order to execute that case’s instructions. This is called strict comparison.
In the following example, the expression would match the default case, since the string “1” is not the same type and value as the number 1 required to match a case. The case requires a number, not a string, so the comparison for the case fails.
Make a mini calculator using this code
<body>
<button onclick="fun()">click me</button>
<script type="text/javascript">
function fun()
{
var j =prompt("enter your first number");
var k =prompt("enter your second number");
var ch =prompt("enter your choice 1)add 2)sub 3)mul 4)div");
var txt
var a = parseInt(j)
var b = parseInt(k)
switch(ch)
{
case'1':
txt=a+b;
break;
case'2':
txt=a-b;
break;
case'3':
txt=a*b;
break;
case'4':
txt=a/b;
break;
default:
txt="enter valid value"
}
document.write(txt);
}
</script>
</body>
1 Comments
It's helpful
ReplyDelete