JavaScript Decision Structures

Published: November 9, 2011

The concept of structured programming introduced the philosophy that good programming requires the use of structures to control the flow of the program. This philosophy led to a departure from using a statement that unconditionally jumped to other areas of the program. Uncontrolled flow was accomplished using the jump and goto statements.

Structures all but eliminated the use of the jump and goto statements and the main structure responsible for this orderly flow control is the decision structure, also known as conditional branching. The two most common decision structures in JavaScript are the if...then...else structure and the case structure.

The basic flow of the if-then structure simply uses the word if to ask a question. The if conditional is followed by a parameter condition that may be tested as a true or false value. The following snippet presents a simplified example:

if (some condition) {
  some instruction code;
  some other instruction code;
}

Notice that the condition is enclosed in a set of parentheses "()", this format is a syntactical necessity and delineates the condition from the rest of the statement. The instructions enclosed in the squiggly-brackets "{}" tell the interpreter what instructions to perform if the condition is true. If the condition is false then the interpreter skips the block of code and continues to the instruction following the if statement.

The squiggly brackets are only necessary if more than one statement will execute following a successful test of the condition. If only one statement is to be executed then omit the brackets and place the instruction directly following the if statement.

The following code snippet replaces the generic code above with an actual condition and instruction block.

var first=1;
var second=2;
if (second>first){
  second=second-first;
  first=second;
}

The else Structure

An alternative to the condition in an if statement may be included by adding an else imperative to the basic if statement, when the clause in the if statement fails the conditional test, control then passes to the else clause, as demonstrated in the following code snippet.

var first=1;
var second=2;
if (second>first)
  second=second-first;
else 
  first=first-second;

Notice that the above code does not include squiggly-brackets after either the if or else clauses. This is because each condition may lead to the execution of only one statement.

The if-then-else structure may be used in many programming languages besides JavaScript so if you learn how to use the structure in JavaScript programs you will be a little ahead of the game when learning other languages.

Was this tutorial useful?

The author appreciates all comments.

More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working