- The switch is a type of conditional statement.
- That we can used to evaluate an expression against multiple possible cases.
- The switch expression is evaluated once.
- we are using case ,break ,default keywords inside the switch.
- The value of the expression is compared with the values of each case.
- If there is a match, the associated block of code can be executed.
switch (variable) { case value: code block break; case value: code block break; -- - -- - -- - case valueN; code block; break; default: code block; }
Code:-
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>switch statement</h1>
<input type="button" onclick="chek_sw();" value="switch statement">
<script>
function chek_sw()
{
var a = 2;
switch (a)
{
case 1: //compare with a (2==1) false
alert("One");
break;
case 2: //compare with a (2==1) true
alert("Two");
break;
case 3: //compare with a (2==3) false
alert("Three");
break;
case 4: //compare with a (2==4) false
alert("Four");
break
default:
alert("Nothing was found");
break
}
}
</script>
</body>
</html>
The switch statement:- genius student
Reviewed by Network security
on
October 14, 2019
Rating:
No comments: