Using the switch Structure : Switch « Statement « JavaScript Tutorial

JavaScript Tutorial
1. Language Basics
2. Operators
3. Statement
4. Development
5. Number Data Type
6. String
7. Function
8. Global
9. Math
10. Form
11. Array
12. Date
13. Dialogs
14. Document
15. Event
16. Location
17. Navigator
18. Screen
19. Window
20. History
21. HTML Tags
22. Style
23. DOM Node
24. Drag Drop
25. Object Oriented
26. Regular Expressions
27. XML
28. GUI Components
29. Dojo toolkit
30. jQuery
31. Animation
32. MS JScript
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
JavaScript Tutorial » Statement » Switch 
3. 5. 1. Using the switch Structure

JavaScript offers the switch statement as an alternative to using the if...else structure.

The switch statement is useful when testing all the possible results of an expression.

The format of a switch structure looks like the following:

switch (expression)
    {
      case label1:
        statement1;
        break;
      case label2:
        statement2;
        break;
      default:
        statement3;
    }

The switch statement evaluates an expression placed between parentheses.

The result is compared to labels associated with case structures that follow the switch statement.

If the result is equal to a label, the statement(s) in the corresponding case structure are executed.

A default is used at the end of a switch structure to catch results that do not match any of the case labels.

A colon always follows a label.

Curly brackets {} are used to hold all the case structures together, but they are not used within a case structure.

The keyword break is used to break out of the entire switch statement once a match is found, thus preventing the default structure from being executed accidentally.

<html>
<SCRIPT LANGUAGE='JavaScript'>
<!--
    var color = "green";
    switch (color)
    {
      case "red":
        document.write("The car is red.");
        break;
      case "blue":
        document.write("The car is blue.");
        break;
      case "green":
        document.write("The car is green.");
        break;
      default:
        document.write("The car is purple.");
    }
    //-->
</SCRIPT>
</html>
3. 5. Switch
3. 5. 1. Using the switch Structure
3. 5. 2. Use switch statement to deal with form input value
3. 5. 3. Switch statement with return value from a prompt dialog
3. 5. 4. Switch statement with default value
3. 5. 5. Switch with boolean value
3. 5. 6. Switch Statement with Integer as the control variable
3. 5. 7. Switch Statement with calculation
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.