1.11 Control Structures

Algorithms require two important control structures: iteration and selection. Both of these are supported by JavaScript in various forms. The programmer can choose the statement that is most useful for the given circumstance.

For iteration, JavaScript provides a standard while statement and a very powerful for statement.

1.11.1 Iteration

while loop

The while statement repeats a body of code as long as the condition is true.

var counter = 0;
while (counter < 3) {
  console.log('Counter at', counter);
  counter = counter + 1;
}
// Counter at 0
// Counter at 1
// Counter at 2

The condition on the while statement is evaluated at the start of each repetition. If the condition is True, the body of the statement will execute.

do...while

In the previous example the statement block was executed only if the condition was true. The do...while loop does the opposite

var counter = 0;
do {
  console.log('Counter at', counter);
  counter = counter + 1;
} while (counter < 3);
// Counter at 0
// Counter at 1
// Counter at 2

The block following do is executed first and then the condition is evaluated. If the while condition is true, the block is executed again and repeats until the condition becomes false. This is known as a post-test loop as the condition is evaluated after the block has executed.

for loop

The most frequently used loop in JavaScript is the for-loop. It consists of three parts, separated by semicolons. The first is the initializer (var i = 1) which initializes the loop and is executed only once at the start. The second is a test condition (i <= 50). When a conditional expression evaluates to true, the body of the loop is executed. When false, the loop terminates. The third part is an updater (i++) which is invoked after each iteration. The updater typically increments or decrements the loop counter.

for (var i = 0; i < 3; i++) {
   console.log(i);
}
// Counter at 0
// Counter at 1
// Counter at 2

for-in loop

A for-in loop iterates through the properties of an object and executes the loop's body once for each enumerable property of the object.

var fruit = { name: 'Apple', kingdom: 'Plantae', family: '    Rosaceae' };
for (var item in fruit) {
   console.log(fruit[item]);
}
// Apple
// Plantae
// Rosoceae

break

When JavaScript encounters a break statement in a loop it immediately exits the loop without executing any other statements in the loop. Control is immediately transferred to the statement following the loop body.

for (var i = 0; i < 3; i++) {
  if (i === 1) {
    break;
  }
  console.log(i);
}
// 0

continue

When JavaScript encounters a continue statement in a loop it stops the execution of the current iteration and goes back to the beginning of the loop to begin the next iteration. The example below displays only even numbers:

for (var i = 1; i <= 10; i++) {
   if ((i % 2) != 0) {
      continue;
   }
   console.log(i);
}
// 2
// 4
// 6
// 8
// 10

1.11.2 Selection

Selection statements allow programmers to ask questions and then, based on the result, perform different actions. Most programming languages provide two versions of this useful construct: the ifelse and the if. A simple example of a binary selection uses the ifelse statement.

if

if (i < 0){
  console.log('Sorry, value is negative');
} else {
  console.log( Math.sqrt(i) );
}

results matching ""

    No results matching ""