Javascript: Operators - switch

Author: Ronald S. Holland

@

Total Application Works

ronholland@sumtotalz.com





To contact us



[
HOME] [Consulting] [Design] [Maintenance] [Project] [Testing] [Training] [Turnkey] [Java] [SQL] [HTML] [JavaScript]

© 2002 - 2014 All Rights Reserved Total Application Works


  • Tell a friend about this site (copy and paste the following HTML page into a file (e.g., using Notepad).)
  • <html>
    <head>
    <title>Example of a link </title>
    </head>
    <body>
    <a href="http://sumtotalz.com/TotalAppsWorks/Javascript/JS_Switch.html">
    JavaScript Operators </a>
    </table>
    </body>
    </html>

JavaScript Syntax

Introduction to JavaScript switch constructs

The purpose of this tutorial is to introduce you to javaScript switch construct concepts. The switch statement/construct allows the user to select one of many blocks of code to be executed. In the next section, we will briefly describe JavaScript.

What is JavaScript?

JavaScript is a prototype-based scripting language that is interpretive/dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles. Next, we will briefly describe the JavaScript switch construct.

switch - multiple selection structure This structure allows selection among multiple sections of code, depending on the value of an integral expression. It operates much like a nested if construct. For example, a nested if construct might look like the following:

 
if ( condition 1 ) {
   do something 1;
}
else if ( condition 2 ) {
   do something 2;
}
else if ( condition 3 ) {
   do something 3;
}
...
else if ( condition n ) {
   do something n;
}


 
Figure 1: if - nested if construct


Below is a example of a JavaScript switch construct along with a brief description.



     
    
    switch( expression ) {
       case 1;
          do something 1;
       case 2;
          do something 2;
       case 3;
          do something 3;
       ...
    
       default;
          do something n;
    }
    
     
    Figure 2: switch - multiple selection structure


Switch construct code

  1. Switch 2
  2. Switch 3

Definitions

Dynamic programming language

    Dynamic programming language is a term used broadly in computer science to describe a class of high-level programming languages that execute at runtime many common behaviors that other languages might perform during compilation.

Interpreted language

    this type of programming language is not compiled and then executed. It is a programming language where programs are "interpreted" by an interpreter program at runtime, on the host CPU. An interpreter may be a program that either
    1. executes the source code directly
    2. translates source code into some efficient intermediate representation (code) and immediately executes this
      • Sun Microsystem's Java bytecode is the intermediate language used by all compilers targeting the Java Virtual Machine.
    3. explicitly executes stored precompiled code[1] made by a compiler which is part of the interpreter system Some examples of Interpreted language are:
      • Ant
      • BASIC (some dialects)
      • Pascal (early implementations)
      • Perl
      • PostScript
      • Python
      • REXX
      • VBScript

Prototype-based programming

    is a style of object-oriented programming where the process of cloning of existing objects is used instead of inheritance. This method is similar to the prototyping of classes in object-oriented languages for the purposes of reuse (known as inheritance in class-based languages.










HOME] [Consulting] [Design] [Maintenance] [Project] [Testing] [Training] [Turnkey] [Java] [SQL] [HTML] [JavaScript]



To contact us

© 2002 - 2014 All Rights Reserved Total Application Works