Develop a new think

Switch

the Switch statement with case entries testing for expected values and providing processing code on a match.

Syntax Switch

switch (expression)
{
case "value1":
do this...
break

case "value2":
do this...
break

case "value3":
do this...
break

...

[
default:
do this...
break
]
}

Example

The expression tested in the switch statement evalutes to a string or numeric value. Then, a branch is made to the case entry matching the value. Each group of statements inside a case entry ends with a break statement to exit case processing. If the expression evaluates to a value that does not have an associated case, then default processing is performed.

The following example is a rewrite of the previous if...else if script using a switch statement to test the arithmetic operators and make appropriate calculations.

The following script is an example of the if...else if form of condition testing in which one of four actions—addition, subtraction, multiplication, or division—is taken on entered numbers depending on which button is clicked.

The expression tested in the switch statement evalutes to a string or numeric value. Then, a branch is made to the case entry matching the value. Each group of statements inside a case entry ends with a break statement to exit case processing. If the expression evaluates to a value that does not have an associated case, then default processing is performed.

The following example is a rewrite of the previous if...else if script using a switch statement to test the arithmetic operators and make appropriate calculations.


 
--------

<script type="text/javascript>

function Compute(Operator)
{
  var No1 = document.getElementById("Number1").value;
  var No2 = document.getElementById("Number2").value;
  var Answer = document.getElementById("Answer");

  if ( isNaN(No1) || isNaN(No2) || No1 == "" || No2 == "" ) {
    alert("Please enter numbers in the text boxes.");
  }
  else {
    switch (Operator)
    {
    case "+":
      Answer.value = parseFloat(No1) + parseFloat(No2);
      break;
    case "-":
      Answer.value = No1 - No2;
      break;
    case "*":
      Answer.value = No1 * No2;
      break;
    case "/":
      Answer.value = No1 / No2;
      break;
    }
  }
}

</script>

<table border="0" cellpadding="0" cellspacing="0">
<tr>
  <td></td>
  <td><input id="Number1" type="text"
        style="width:50px; text-align:right"/></td>
<tr>
  <td><input type="button" value="+" style="width:20px"
        onclick="Compute(this.value)"/>
      <input type="button" value="-" style="width:20px"
        onclick="Compute(this.value)"/>
      <input type="button" value="*" style="width:20px"
        onclick="Compute(this.value)"/>
      <input type="button" value="/" style="width:20px"
        onclick="Compute(this.value)"/>&nbsp;
  </td>
  <td><input id="Number2" type="text"
       style="width:50px; text-align:right"/></td>
</tr>
<tr>
  <td></td>
  <td>--------</td>
</tr>
<tr>
  <td style="text-align:right">=&nbsp;</td>
  <td><input id="Answer" type="text" style="width:50px; text-align:right"
        onfocus="this.blur()"/></td>
</tr>
</table>

The switch statement tests the Operator variable. One of four string values is detected: "+", "-", "*", or "/". The script branches to the matching case entry and performs the arithmetic, exiting the case at its break statement.

web counter