23.07.2013 Views

JavaScript/JScript: Control Structures I - Pearson Learning Solutions

JavaScript/JScript: Control Structures I - Pearson Learning Solutions

JavaScript/JScript: Control Structures I - Pearson Learning Solutions

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2008934301<br />

Chapter 14 <strong>JavaScript</strong>/<strong>JScript</strong>: <strong>Control</strong> <strong>Structures</strong> I 401<br />

blame was pointed at the goto statement, which allows the programmer to specify a<br />

transfer of control to one of a very wide range of possible destinations in a program. The<br />

notion of so-called structured programming became almost synonymous with “ goto<br />

elimination.” <strong>JavaScript</strong> does not have a goto statement.<br />

The research of Bohm and Jacopini1<br />

had demonstrated that programs could be written<br />

without any goto statements. The challenge of the era for programmers was to shift their<br />

styles to “ goto-less<br />

programming.” It was not until the 1970s that programmers started<br />

taking structured programming seriously. The results have been impressive, as software<br />

development groups have reported reduced development times, more frequent on-time<br />

delivery of systems and more frequent within-budget completion of software projects. The<br />

key to these successes is that structured programs are clearer, easier to debug and modify,<br />

and more likely to be bug-free in the first place.<br />

Bohm and Jacopini’s work demonstrated that all programs could be written in terms of<br />

only three control structures,<br />

namely the sequence structure,<br />

the selection structure and the<br />

repetition structure.<br />

The sequence structure is built into <strong>JavaScript</strong>. Unless directed otherwise,<br />

the computer executes <strong>JavaScript</strong> statements one after the other in the order in which<br />

they are written. The flowchart segment of Fig. 14.1 illustrates a typical sequence structure<br />

in which two calculations are performed in order.<br />

A flowchart is a graphical representation of an algorithm or of a portion of an algorithm.<br />

Flowcharts are drawn using certain special-purpose symbols such as rectangles, diamonds,<br />

ovals and small circles; these symbols are connected by arrows called flowlines,<br />

which indicate the order in which the actions of the algorithm execute.<br />

Like pseudocode, flowcharts are often useful for developing and representing algorithms,<br />

although pseudocode is strongly preferred by many programmers. Flowcharts show clearly<br />

how control structures operate; that is all we use them for in this text. The reader should carefully<br />

compare the pseudocode and flowchart representations of each control structure.<br />

Consider the flowchart segment for the sequence structure on the left side of Fig. 14.1.<br />

We use the rectangle symbol (or action symbol)<br />

to indicate any type of action, including a calculation<br />

or an input/output operation. The flowlines in the figure indicate the order in which<br />

the actions are performed—first, grade is added to total,<br />

then 1 is added to counter.<br />

<strong>JavaScript</strong> allows us to have as many actions as we want in a sequence structure. As we will<br />

soon see, anywhere a single action may be placed, we may place several actions in sequence.<br />

Fig. 14.1<br />

add grade to total total = total + grade;<br />

add 1 to counter counter = counter + 1;<br />

Flowcharting <strong>JavaScript</strong>’s sequence structure.<br />

1. Bohm, C., and G. Jacopini, “Flow Diagrams, Turing Machines, and Languages with Only Two<br />

Formation Rules,” Communications of the ACM, Vol. 9, No. 5, May 1966, pp. 336–371.<br />

e-Business and e-Commerce: How to Program, by Harvey M. Deitel, Paul J. Deitel, and Tem R. Nieto. Published by Prentice Hall.<br />

Copyright © 2001 by <strong>Pearson</strong> Education, Inc.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!