Difference between revisions of "JavaScript Flow Control and Looping"

From Techotopia
Jump to: navigation, search
(The JavaScript if Structure)
Line 21: Line 21:
 
Conditional statements control the parts of a script that executed depending the result of a particular expression (i.e whether an expression returns a boolean ''true'' or ''false'' value). The two types of conditional structures are ''if'' and ''if ... else''. In this section we will take a closer looka t both of these condtional structures.
 
Conditional statements control the parts of a script that executed depending the result of a particular expression (i.e whether an expression returns a boolean ''true'' or ''false'' value). The two types of conditional structures are ''if'' and ''if ... else''. In this section we will take a closer looka t both of these condtional structures.
  
=== The JavaScript if Structure ===
+
=== The JavaScript ''if'' Structure ===
  
 
In both cases the ''if'' statement is followed by the expression to be evaluated. In this section we will take
 
In both cases the ''if'' statement is followed by the expression to be evaluated. In this section we will take

Revision as of 18:59, 23 April 2007

One of the most powerful features of JavaScript (and every other programming or scripting language for that matter) is the ability to build intelligence and logic into your web pages. It is vital in constructing scripts to be able to have the script make decisions and repeat tasks until specified criteria are met. For example, if you are developing an e-commerce application you may want to repeatedly ask a user to enter a credit card number until a valid credit card number is entered. Alternatively, you may want your script to loop a specifc number of times through a task before moving on to the next part of the script. All of this logic and flow control is achieved using some very simple structures. These are:

Conditional Statements

  • if statements
  • if ... else ... statements

Looping Statements

  • while loops
  • do ... while loops

switch Statements

label Statements

with Statements

Conditional Statements

Conditional statements control the parts of a script that executed depending the result of a particular expression (i.e whether an expression returns a boolean true or false value). The two types of conditional structures are if and if ... else. In this section we will take a closer looka t both of these condtional structures.

The JavaScript if Structure

In both cases the if statement is followed by the expression to be evaluated. In this section we will take