Introduction
Overview
History
Advice
Books
Tutorials
Source Code
Top 100
Reference

Beginners
Sample Code
IDE
Syntax
Variables
Boolean Logic
Strings
Arrays
Conditional Flow

Intermediate
Date-Time
Math
Functions
Event Handlers
Error Handling

Advanced
Objects
Dynamic HTML
Cookies
Multimedia
CGI
Java Applets
Distribution

Community
Web Sites
Mailing Lists
USENET
Vendors
News

GBIC >> JavaScript >> Conditional Flow
All programming languages off multiple ways of using code to determine which code will execute in which order. JavaScript support 8 different ways to affect program flow:

if..else
The if..else statement executes one set of code if a condition is true and another set of code if the condition is false:

if (condition)
   { Block }
else
   { Block }

The if..else statement can be simplified into a simple if statement which executes a block of code if the condition is true:

if (condition)
  { Block }		//block executes if condition is true

switch
With the switch statement a JavaScript program can compare an expression with one or more other expressions. A block of code is executed when the comparison evaluates to true.

switch (expression) 
{
  case "dog" :		//note use of colon at end of line
        { Block }
        break;		//note use of semicolon at end of line
  case "cat" :
        { Block }
        break;
  default :
        { Block }
}

With the break statements, once a true comparsion result is found the program breaks out of the switch statement. Without the break statements all comparisons are made, allowing for the possibility of executing multiple blocks of code.

for..next
The for..next statements provides a way to repeat a block of code a fixed number of times, or until a condition is true. This is probably the most used flow control statement.

for (i=0; i<10; i++)	//  for (initialize;  condition;  adjust)
  { Block }		//  statements 

do..while
The do..while statement allows a block of code to execute repeatedly until a condition is true. Since the condition test is performed at the end of the loop, this approach ensures that the code is executed at least onces.

do
  { Block }
while (expression)

while
The while statement also allows a block of code to execute repeatedly until a condition is true. But since the condition test is performed at the end of the loop, the code will only be executed if the condition is true.

while (expression)
    { Block }

for..in
JavaScript provides a means of walking through a list and of performing an action on each item in the list. The for statement can cycle through all the properties in an object. It can also cycle through the elements of an array.

for (myvariable in object) //cycle through object properties
    { Block }              //cycle through elements of array

or

for (myvar in wine1)
  {record += 1}
document.write (record)

with
Writing out the complete dot notation repeatedly for properties or methods can make code visually difficult to read. The with statement provides some relief by allowing all properties/methods within its scope to leave off the repetitive portion of the properties/methods.

with (object) { Block } //properties/methods can //leave off the repetitive portion

? conditional operator
For the simple case of performing one action if a condition is true and another action if a statement is false, the '?' conditional operator is available.

result = (condition) ? (if true) : (if false) ;

Conditional Flow Options
JavaScript provides two commands which modify the way a conditional flow loop is handled. The 'break' command drops out of a loop and the 'continue' command drops out of the current iteration of a loop, as shown in the following example: