JavaScript Flow Control and Looping

Revision as of 19:36, 23 April 2007 by Neil (Talk | contribs) (JavaScript ''for'' loops)

Revision as of 19:36, 23 April 2007 by Neil (Talk | contribs) (JavaScript ''for'' loops)

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

JavaScript Conditional Statements

Conditional statements control whether a part of a script is 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 look at both of these condtional structures and provide some examples of their use.

The JavaScript if Statement

The first line of an 'if structure involves the if statement followed by the expression to be evaluated in parentheses. For example:

if (i < 2)

In the above example if i is less than 2 then the expression will be evaluated as being true, otherwise it will be returned as false

The next step in structing an if statement is to specify what action should be taken if the expression is true. This is acheived by placing the lines of script to be executed in open and closing braces after the if statement:

if (i < 2)
{
document.writeln ("The value of i is less than 2");
i++;
}

Note that if there is only one line of script to be performed if the if expression is true then the braces are optional:

if (i < 2)
document.writeln ("The value of i is less than 2");

Whilst this is perfectly valid JavaScript it is recommended, for the purposes of consistent scripting style, that the braces be used even for a single line of script after the 'if' statement.


The JavaScript if ... else Statements

The if statement above allows you to specify what should happen is a particular expression evaluates to true. It does not, however, provide the option to specify something else that should happen in the event that the expression evaluates to be false. This is where the if ... else construct comes into play.

The syntax for if ... else is the same as for the if statement with the exception that the esle statement can be used to specify alternate action:

if (customerName = "Fred")
{
document.write ("Hello Fred!");
} 
else 
{
document.write ("You're not Fred! Where's Fred?");
}

As shown in the above example the script following the if statement is executed when the expression is evaluated to be true (i.e the customerName variable contains the string "Fred") and the script after the else statement is executed when the customerName does nmot match the string "Fred".

JavaScript Looping Statements

It is generally common knowledge that computers are great at performing repetitive tasks an infinite number of times, and doing so very quickly. It is also common knowlede that computers really don't do anything unless someone programs them to tell them what to do. Loop statements are the primary mechanism for telling a computer to perform the same task over and over again until aset of crieria are met. This is where for, while and do ... while loops are of use.

JavaScript for loops

Suppose you wanted to add a number to itself ten times. One way to do this might be to write the following JavaScript:


var j = 10;

j += j;
j += j;
j += j;
j += j;
j += j;
j += j;
j += j;
j += j;
j += j;
j += j;

Whilst this is somewhat cumbersome it does work. What would happen, however, if you needed to perform this task 100 times or 10,000 times. Writing a script to perform this as above would be prohibitive. This is exactly what the for loop is intended to handle.

The syntax of a for loop is as follows:

for ( initializer; conditional expression; loop expression ) { statements to be executed }

The initializer typically initializes a counter variable. Traditionally the variable i is used for this purpose. For example:

i = 0

which sets the counter to be the value i and sets it to zero.

The conditional expression specifies the test to perform to verify whether the loop has been performed the required number of times. For example, if we want to loop 100 times:

i < 10

Finally the loop expression specifies the action to perform on the counter variable. For example to increment by 1:

i++

Bringing this all together we can create a for loop to perform the task outlined in the earlier example:


var j = 10;

for (i=0; i<10; i++)
{
j += j;
}

As with the for loop the enclosing braces are optional if a single line of script is to be executed, but their use is recommeded regardless.

JavaScript while loop