JavaScript
  Home arrow JavaScript arrow Page 2 - 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  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
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 if statement


    (Page 2 of 6 )

    The if structure executes a statement based on the value produced from a boolean expression. It's written in the following way:

      if(boolean expression)
       statement;

    If the boolean expression evaluates to true, the interpreter executes the statement, then continues executing the following script statements. If the boolean expression evaluates to false, the interpreter doesn't execute the statement and continue executing the following script statements. You must delimit the boolean expression in parentheses. If you don't, the interpreter generates an error like the following one (don't put a semicolon at the end of the boolean expression, as it's not a statement, and if you do that the interpreter generates the same error because it's expecting the parentheses).

    You can execute more than one statement with the if structure (when the boolean expression evaluates to true, of course) by using the code block syntax. A code block is any number of statements surrounded by curly braces ({}). These statements are executed together or escaped together. So the if block will look like this:

    if(boolean expression)
    {
      statement 1;
      statement 2;
      statement 3;
    }

    Now if the boolean expression evaluates to true, the statements 1, 2 and 3 get executed, then the interpreter continues executing the following statements. If the boolean expression evaluates to false, however, the interpreter escapes the statement 1, 2 and 3 (I mean the if block) and continues executing the statements that follow in the script. Note that I have indented the statements using the tab key in order to make the block more readable. This is very useful, especially when we work with nested if/else statements (discussed shortly), so it's a good practice to indent your code blocks. Let's integrate the if statement into our prompt box example.

    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

    - 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
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...







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