Home » Online Computer Education » Learn Java Script, The switch Statement, Lesson No. 20

Learn Java Script, The switch Statement, Lesson No. 20

 
The switch statement evaluates a series of conditional tests or cases and executes additional statements based on whether the case proves true. The syntax of the switch statement is shown here:

switch (expression) {
case label:
statements;
break;
.
.
.
case label:
statements;
break;
default:
statements;
}
The switch statement compares the result of the expression against the label for each case. The statements for the first case that proves true are executed. If no case proves true, the statements associated with the default statement are executed if the optional default statement was included.

The break statement at the end of each case is optional. Its purpose is to tell the script to exit the switch statement as soon as the statements in the first matching case are executed and to proceed with the next script statement following the switch statement. If you remove the optional break statements, the script will continue to examine each case and execute the statements of any case that matches the expression.

In the following example, the user is prompted to type the name of a business account. Starting with the first case statement, the script compares the value assigned to the accountName variable to the label assigned to the first case statement. If the condition proves true (that is, if there is a match), the statements for that case statement are executed, and the break at the end of the case statement tells the script to jump to the first statement after the switch statement, which in this example happens to be an if statement. If none of the case statements proves true, the statement associated with the default statement executes, stating that the account name the user typed is not registered. Figure 2.8 shows what this example looks like when it’s executed.

The script flags Paterson as an invalid account name.

7

<HTML>
<HEAD>
<TITLE>Script 2.10 – Demonstration of the switch
statement</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE=”JavaScript” TYPE=”Text/JavaScript”>
<!– Start hiding JavaScript statements
var accountStatus = “None”;
var accountName = window.prompt(“Enter your account
name:”);
switch (accountName) {
case “Morganstern”:
document.write(“The account number for this
account is 12321.”);
accountStatus = “approved”;
break;
case “Davidson”:
document.write(“The account number for this
account is 88844.”);
accountStatus = “warning”;
break;
default:
document.write(“The account is not registered in
this system.”);
accountStatus = “error”;
}
if (accountStatus == “warning”) {
document.write(accountStatus);
window.alert(“Contact the on-duty supervisor”);
}
// End hiding JavaScript statements –>
</SCRIPT>
</BODY>
</HTML>

About

The main objective of this website is to provide quality study material to all students (from 1st to 12th class of any board) irrespective of their background as our motto is “Education for Everyone”. It is also a very good platform for teachers who want to share their valuable knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *