JavaScript
  Home arrow JavaScript arrow Page 5 - The Power of Javascript: Controlling the E...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

The Power of Javascript: Controlling the Execution of the Script
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 8
    2005-08-09

    Table of Contents:
  • The Power of Javascript: Controlling the Execution of the Script
  • The if statement
  • The if statement example
  • Advanced if statements (else if and else)
  • The switch statement
  • The switch example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    The Power of Javascript: Controlling the Execution of the Script - The switch statement


    (Page 5 of 6 )

    In the last example we have used more than one else if statement inside the if/else structure to test the variable someText with more than one value, then if a match is found the expression evaluates to true, and this else if statement executes its block. This is fine, but Javascript supports the more suitable statement for this situation, the switch statement. The switch statement is useful when we want to check a given variable against multiple values and execute a different block of code depending on the value of the variable. The syntax of the switch statement is complex because it's divided into case sections of the possible values for the variable in question, so let's take a look.

    switch(variable)
    {
      case value1:
        statement 1;
        statement 2;
        statement 3;
        break;

      case value2:
        statement 1;
        statement 2;
        statement 3;
        break;

      case value3:
        statement 1;
        statement 2;
        statement 3;
        break;

      default:
        statement 1;
        statement 2;
        statement 3;
        break;
    }

    As you can see, the switch block is made up of sections, and each of them begins with the keyword case (except for the last section, which we will talk about shortly) followed by a string or numerical value, followed by a colon, then the group of statements, then the keyword break and a semicolon. The interpreter produces the value of the variable between the switch's parentheses, then goes to the first case section and evaluates this section's value. If it's the same as the switch variable's value, it executes this section's statements until it encounters the keyword break, which terminates the execution of the switch statement and executes the first statement in the script following it.

    As you can see, the case value: and the break; is much more like a delimiter for each section, because all of the sections exist in one big code block. We are going to use the keyword break in the next article, in which you will really grasp its use, but for now just think of it as a way to exit the switch statement. The last section is labeled default because it's the default section that will execute if no match is found. Normally the interpreter goes to the next case section until a match is found or until the default section. This means that the default section is similar to the else block in the if/else structure. Let's modify our example to use switch instead of multiple else if statements.

    More JavaScript Articles
    More By Michael Youssef


       · I'm very novice in JavaScript, but wouldn't it be possible to create a large web...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT