JAVA SCRIPT DAY 6 ( Switch Case)

DAY 6

JavaScript - Switch Case

Ø    Switch statement is used to select one of many blocks of code to be executed.
Ø    The objective of a switch statement is to give an expression to evaluate and several different statements to execute based on the value of the expression. The interpreter checks each case against the value of the expression until a match is found. If nothing matches, a default condition will be used.
Ø    The break statements breaks out of the switch block. It will stop the execution of code and  / or case testing inside the block. If they were omitted, the interpreter would continue executing each statement in each of the following cases.
Ø    The defaultspecifies the code to run if there is no case match.

Flow Chart

Ø    Syntax

switch(expression)
{
case condition 1: statement(s)
break;

case condition 2: statement(s)
break;
...

case condition n: statement(s)
break;

default: statement(s)
}

Example

<html>
<body>

<scripttype="text/javascript">
<!--
var grade='A';
document.write("Entering switch block<br />");
switch(grade)
{
case'A':document.write("Good job<br />");
break;

case'B':document.write("Pretty good<br />");
break;

case'C':document.write("Passed<br />");
break;

case'D':document.write("Not so good<br />");
break;

case'F':document.write("Failed<br />");
break;

default:document.write("Unknown grade<br />")
}
document.write("Exiting switch block");
//-->
</script>

<p>Set the variable to different value and then try...</p>
</body>
</html>

Output

Entering switch block
Good job
Exiting switch block
Switch-case statement without any break statement.
<html>
<body>

<scripttype="text/javascript">
<!--
var grade='A';
document.write("Entering switch block<br />");
switch(grade)
{
case'A':document.write("Good job<br />");
case'B':document.write("Pretty good<br />");
case'C':document.write("Passed<br />");
case'D':document.write("Not so good<br />");
case'F':document.write("Failed<br />");
default:document.write("Unknown grade<br />")
}
document.write("Exiting switch block");
//-->
</script>

<p>Set the variable to different value and then try...</p>
</body>
</html>

Output

Entering switch block
Good job
Pretty good
Passed
Not so good
Failed
Unknown grade
Exiting switch block

No comments:

Post a Comment

Give your valuable feedback

Topic :Software & Types, Subject: Computer Fundamental Notes for CSJM University Kanpur(for different courses like BBA, BCA, etc..)

Software Software refers to the programs, data, and instructions that enable a computer or other digital device to perform specific tasks or...