switch

  • Revision slug: JavaScript/Reference/Statements/switch
  • Revision title: switch
  • Revision id: 382607
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment Hello juliandoesstuff! I think "Mangoes" and "Papayas" have same price. So, there have same process. Look at message: "Mangoes and papayas are $2.79 a pound." :-)

Revision Content

Summary

Evaluates an expression, matching the expression's value to a case label, and executes statements associated with that case.

Version Information

Statement
Implemented in: JavaScript 1.2, NES 3.0
ECMA Version: ECMA-262, Edition 3

Syntax

switch (expression) {
   case label1:
      statements1
      [break;]
   case label2:
      statements2
      [break;]
   ...
   case labelN:
      statementsN
      [break;]
   default:
      statements_def
      [break;]
}

Parameters

expression
An expression matched against each label.
labelN
Identifier used to match against expression.
statementsN
Statements that are executed if expression matches the associated label.
statements_def
Statements that are executed if expression does not match any label.

Description

If a match is found, the program executes the associated statements. If multiple cases match the provided value, the first case that matches is selected, even if the cases are not equal to each other.

The program first looks for a case clause with a label matching the value of expression and then transfers control to that clause, executing the associated statements. If no matching label is found, the program looks for the optional default clause, and if found, transfers control to that clause, executing the associated statements. If no default clause is found, the program continues execution at the statement following the end of switch. By convention, the default clause is the last clause, but it does not need to be so.

The optional break statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If break is omitted, the program continues execution at the next statement in the switch statement.

Examples

Example: Using switch

In the following example, if expr evaluates to "Bananas", the program matches the value with case "Bananas" and executes the associated statement. When break is encountered, the program breaks out of switch and executes the statement following switch. If break were omitted, the statement for case "Cherries" would also be executed.

switch (expr) {
   case "Oranges":
      document.write("Oranges are $0.59 a pound.<br>");
      break;
   case "Apples":
      document.write("Apples are $0.32 a pound.<br>");
      break;
   case "Bananas":
      document.write("Bananas are $0.48 a pound.<br>");
      break;
   case "Cherries":
      document.write("Cherries are $3.00 a pound.<br>");
      break;
   case "Mangoes":
   case "Papayas":
      document.write("Mangoes and papayas are $2.79 a pound.<br>");
      break;
   default:
      document.write("Sorry, we are out of " + expr + ".<br>");
}
document.write("Is there anything else you'd like?<br>");

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Evaluates an expression, matching the expression's value to a case label, and executes statements associated with that case.</p>
<h2 id="Version_Information">Version Information</h2>
<table class="standard-table">
  <tbody>
    <tr>
      <td class="header" colspan="2">Statement</td>
    </tr>
    <tr>
      <td>Implemented in:</td>
      <td>JavaScript 1.2, NES 3.0</td>
    </tr>
    <tr>
      <td>ECMA Version:</td>
      <td>ECMA-262, Edition 3</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
switch (<em>expression</em>) {
   case <em>label1</em>:
      <em>statements1</em>
      [break;]
   case <em>label2</em>:
      <em>statements2</em>
      [break;]
   ...
   case <em>labelN</em>:
      <em>statementsN</em>
      [break;]
   default:
      <em>statements_def</em>
      [break;]
}
</pre>

<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
  <dt><code>expression</code></dt>
  <dd>An expression matched against each label.</dd>
  <dt><code>labelN</code></dt>
  <dd>Identifier used to match against <code>expression</code>.</dd>
  <dt><code>statementsN</code></dt>
  <dd>Statements that are executed if <code>expression</code> matches the associated label.</dd>
  <dt><code>statements_def</code></dt>
  <dd>Statements that are executed if <code>expression</code> does not match any label.</dd>
</dl>


<h2 id="Description" name="Description">Description</h2>
<p>If a match is found, the program executes the associated statements. If multiple cases match the provided value, the first case that matches is selected, even if the cases are not equal to each other.</p>
<p>The program first looks for a <code>case</code> clause with a label matching the value of expression and then transfers control to that clause, executing the associated statements. If no matching label is found, the program looks for the optional <code>default</code> clause, and if found, transfers control to that clause, executing the associated statements. If no <code>default</code> clause is found, the program continues execution at the statement following the end of <code>switch</code>. By convention, the <code>default</code> clause is the last clause, but it does not need to be so.</p>
<p>The optional <code><a href="/en-US/docs/JavaScript/Reference/Statements/break" title="JavaScript/Reference/Statements/break">break</a></code> statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If <code>break</code> is omitted, the program continues execution at the next statement in the <code>switch</code> statement.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_switch" name="Example:_Using_switch">Example: Using <code>switch</code></h3>
<p>In the following example, if <code>expr</code>&nbsp;evaluates to "Bananas", the program matches the value with case "Bananas" and executes the associated statement. When <code>break</code> is encountered, the program breaks out of <code>switch</code> and executes the statement following <code>switch</code>. If <code>break</code> were omitted, the statement for case "Cherries" would also be executed.</p>
<pre class="brush: js">
switch (expr) {
   case "Oranges":
      document.write("Oranges are $0.59 a pound.&lt;br&gt;");
      break;
   case "Apples":
      document.write("Apples are $0.32 a pound.&lt;br&gt;");
      break;
   case "Bananas":
      document.write("Bananas are $0.48 a pound.&lt;br&gt;");
      break;
   case "Cherries":
      document.write("Cherries are $3.00 a pound.&lt;br&gt;");
      break;
   case "Mangoes":
   case "Papayas":
      document.write("Mangoes and papayas are $2.79 a pound.&lt;br&gt;");
      break;
   default:
      document.write("Sorry, we are out of " + expr + ".&lt;br&gt;");
}
document.write("Is there anything else you'd like?&lt;br&gt;");
</pre>

<!--languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/switch", "ja": "ja/JavaScript/Reference/Statements/switch", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/switch" } )-->
Revert to this revision