The switch/case structure lets you match a variable's or expression's value to a number of values and then execute code when a match is found. A description of the switch/case structure is shown in this article.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Win $3,500.00 worth of gas!

The switch/case Structure

The switch/case structure lets you match a variable's or expression's value to a number of values and then execute code when a match is found. A description of the switch/case structure is shown below.

switch(expression)
{
case (value):
  statements;
  break;
case (value):
  statements;
  break;
default:
  statements;
}

The variable or expression is placed in brackets after the switch statement. The body of the structure contains one or more comparison values in brackets case statements. Each case section defines a block of code to be executed if its value matches the value in the switch statement.

Note that the last line of each case branch (except the last one) is the break command. If you omit the break command, the code in every case section following the one with the matching value will be executed until a break command is found. The break command causes execution to break out of the switch/case structure.

-  The omission of the break command might be an error, but sometimes it is used as a deliberate method of program flow control.

Note the default branch at the end of the structure. Use the default section for code to execute if none of the case values are found to match.

The example below shows a switch/case structure which branches based upon matching the value of a variable placed in the switch statement.

switch(a)
{
case(1):
  alert("a = 1");
  break;
case(2):
  alert("a = 2");
  break;
case(3):
  alert("a = 3");
}

Web Design Sections

RSS Feed RSS Feed

Java Script Quick Reference
Java Script Data Types
Java Script Reserved Words
Java Script Arithmetic Operators
Comparison Operators
Java Script Arrays
Java Script Character Encoding and Decoding
The if/else Structure
The switch/case Structure
The for Loop
The while Loop
The break Statement
The continue Statement
JavaScript Math Object
Round a Number
Determine Absolute Value
Determine Minimum and Maximum
Generating Random Numbers
Java Script Trigonometric Methods
Java Script Number Object
Format a Number as Currency
Java Script Strings
Compare Two Strings
Find a Character or a Substring Within a String
Include a Quote Character in a String
Include a Backslash Character in a String
Define Lines in a String
Use Escape to Replace Dangerous Characters
Convert a Number to a String
Convert a String to a Number
The Document Object Model (DOM)
Accessing Web Page Elements
Interactively Set Webpage Colors
Get Webpage File Date and File Size
Dueling Windows
Cookie Power Made Easy

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269