IntroductionBefore writing a script to solve a problem, it is essential to have a thorough understanding of the problem and a carefully planned approach to solving the problem. When writing a script,it is equally essential to understand the types of building blocks that are available and to employ proven program-construction principles.

AlgorithmsAny computing problem can be solved by executing a series of actions in a specific order. A procedure for solving a problem in terms of 1) the actions to be executed and 2) the order in which the actions are to be executed is called an algorithm.

Consider the "rise-and-shine algorithm" followed by one junior executive for getting out of bed and going to work: 1) get out of bed, 2) take off pajamas, 3) take a shower, 4) get dressed, 5) eat breakfast, 6) carpool to work. This routine gets the exec to work well prepared to make critical decisions.

Suppose, however, that the same steps are performed in a slightly different order: 1) get out of bed, 2) take off pajamas, 3) get dressed, 4) take a shower, 5) eat breakfast, 6) carpool to work. In this case, our junior exec shows up for work soaking wet. Specifying the order in which statements are to be executed is a computer program is called program control.

Pseudocode

Control StructuresNormally, statements in a program execute one after the other in the order in which they are written. This process is called sequential execution. Various JavaScript statements enable the programmer to specify that the next statement to execute may not be the next one in sequence. This is known as transfer of control.

In 1966 Carrado Bohm and Guiseppi Jacopini, two Italian mathematicians, proved mathematically that it is possible to express any algorithm in terms of only three constructs, along with an arbitrary number of boolean flags [3]. The three constructs are:

1. sequences of statements

2. selection (e.g., if-else, switch)

3. repetition, or iteration (e.g., while, for, do)

FlowchartsA flowchart is a graphical representation of an algorithm or a portion of one. Flowcharts are drawn using certain special-purpose symbols, such as rectangles, diamonds, ovals and circles. These symbols are connected by arrows called flowlines, which indicate the order in which the actions of the algorithm execute.

Like pseudocode, flowcharts often are useful for developing and representing algorithms. They show clearly how control structures operate.

Selection structuresJavaScript provides three types of selections structures.

  1. The if selection statement performs (or selects) an action if a condition is true or skips the action if the condition is false.
  2. The if...else selection statement performs an action if a condition is true and performs a different action if the condition is false.
  3. The switch selection statement performs one of many different actions, depending on the value of an expression.

The if statement is called a single-selection structure because it selects or ignores a single action or group of actions. The if..else statement is a double-selection structure because it selects between two different actions or groups of actions. The switch statement is a multiple-selection structure because it selects among many different actions or groups of actions.

Repetion structuresJavscript provdes four repetition structure types:

  1. while
  2. do..while
  3. for
  4. for...in

Which brings up the topic of reserved words.



If...

Table of Contents