JavaScript/Control Structures

From Wikibooks, open books for an open world
Jump to: navigation, search
Previous: Operators Index Next: Functions and Objects

The control structures within Javascript allow the program flow to change within a unit of code or function. These statements can determine whether or not given statements are executed, as well as repeated execution of a block of code.

With the exception of the switch control structure, all control structures can operate either on a statement or a block of code enclosed with braces ({ and }). The same structures utilize booleans to determine whether or not a block gets executed, where any defined variable that is neither zero or an empty string is treated as true.

if[edit]

The if statement is straight forward - if the given expression is true, the statement or statements will be executed. Otherwise, they are skipped.

if (a === b) {
  document.body.innerHTML += "a equals b";
}

The if statement may also consist of multiple parts, incorporating else and else if sections. These keywords are part of the if statement, and identify the code blocks that are executed if the preceding condition is false.

if (a === b) {
  document.body.innerHTML += "a equals b";
} else if (a === c) {
  document.body.innerHTML += "a equals c";
} else {
  document.body.innerHTML += "a does not equal either b or c";
}

while[edit]

The while statement executes a given statement as long as a given expression is true. For example, the code block below will increase the variable c to 10:

while (c < 10) {
  c += 1;
  // ...
}

This control loop also recognizes the break and continue keywords. The break keyword causes the immediate termination of the loop, allowing for the loop to terminate from anywhere within the block.

The continue keyword finishes the current iteration of the while block or statement, and checks the condition to see if it is true. If it is true, the loop commences again.

do... while[edit]

The do ... while statement executes a given statement as long as a given expression is true - however, unlike the while statement, this control structure will always execute the statement or block at least once. For example, the code block below will increase the variable c to 10:

do {
  c += 1;
} while (c < 10);

As with while, break and continue are both recognized and operate in the same manner. break exits the loop, and continue checks the condition before attempting to restart the loop.

for[edit]

The for statement allows greater control over the condition of iteration. While it has a conditional statement, it also allows a pre-loop statement, and post-loop increment without affecting the condition. The initial expression is executed once, and the conditional is always checked at the beginning of each loop. At the end of the loop, the increment statement executes before the condition is checked once again. The syntax is:

for (<initial expression>;<condition>;<final expression>)

The for statement is usually used for integer counters:

var c;
for (c = 0; c < 10; c += 1) {
  // ...
}

While the increment statement is normally used to increase a variable by one per loop iteration, it can contain any statement, such as one that decreases the counter.

Break and continue are both recognized. The continue statement will still execute the increment statement before the condition is checked.

switch[edit]

The switch statement evaluates an expression, and determines flow control based on the result of the expression:

switch(i) {
case 1:
  // ...
  break;
case 2:
  // ...
  break;
default:
  // ...
  break;
}

When i gets evaluated, it's value is checked against each of the case labels. These case labels appear in the switch statement and, if the value for the case matches i, continues the execution at that point. If none of the case labels match, execution continues at the default label (or skips the switch statement entirely if none is present.)

Case labels may only have constants as part of their condition.

The break keyword exits the switch statement, and appears at the end of each case in order to prevent undesired code from executing. While the break keyword may be omitted (for example, you want a block of code executed for multiple cases), it may be considered bad practice doing so.

The continue keyword does not apply to switch statements.


Previous: Operators Index Next: Functions and Objects