JavaScript/JScript: Control Structures I - Pearson Learning Solutions
JavaScript/JScript: Control Structures I - Pearson Learning Solutions
JavaScript/JScript: Control Structures I - Pearson Learning Solutions
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 403<br />
As we have shown, <strong>JavaScript</strong> has only eight control structures: sequence, three types<br />
of selection and four types of repetition. Each program is formed by combining as many of<br />
each type of control structure as is appropriate for the algorithm the program implements.<br />
As with the sequence structure of Fig. 14.1, we will see that each control structure is flowcharted<br />
with two small circle symbols, one at the entry point to the control structure and<br />
one at the exit point.<br />
Single-entry/single-exit control structures make it easy to build programs—the control<br />
structures are attached to one another by connecting the exit point of one control structure<br />
to the entry point of the next. This process is similar to the way a child stacks building<br />
blocks, so we call this control-structure stacking. We will learn that there is only one other<br />
way control structures may be connected—control-structure nesting. Thus, algorithms in<br />
<strong>JavaScript</strong> programs are constructed from only eight different types of control structures<br />
combined in only two ways.<br />
14.5 The if Selection Structure<br />
A selection structure is used to choose among alternative courses of action in a program.<br />
For example, suppose that the passing grade on an examination is 60 (out of 100). Then the<br />
pseudocode statement<br />
If student’s grade is greater than or equal to 60<br />
Print “Passed”<br />
determines if the condition “student’s grade is greater than or equal to 60” is true or false.<br />
If the condition is true, then “Passed” is printed, and the next pseudocode statement in order<br />
is “performed” (remember that pseudocode is not a real programming language). If the condition<br />
is false, the print statement is ignored, and the next pseudocode statement in order is<br />
performed. Note that the second line of this selection structure is indented. Such indentation<br />
is optional, but it is highly recommended because it emphasizes the inherent structure of<br />
structured programs. The <strong>JavaScript</strong> interpreter ignores whitespace characters: blanks, tabs<br />
and newlines used for indentation and vertical spacing. Programmers insert these<br />
whitespace characters to enhance program clarity.<br />
Good Programming Practice 14.1<br />
Consistently applying reasonable indentation conventions throughout your programs improves<br />
program readability. We suggest a fixed-size tab of about 1/4 inch or three spaces per<br />
indent. 14.1<br />
The preceding pseudocode If statement may be written in <strong>JavaScript</strong> as<br />
if ( studentGrade >= 60 )<br />
document.writeln( "Passed" );<br />
Notice that the <strong>JavaScript</strong> code corresponds closely to the pseudocode. This similarity is<br />
why pseudocode is a useful program development tool. The statement in the body of the<br />
if structure outputs the character string "Passed" in the HTML document.<br />
The flowchart of Fig. 14.3 illustrates the single-selection if structure. This flowchart<br />
contains what is perhaps the most important flowcharting symbol—the diamond symbol (or<br />
decision symbol) which indicates that a decision is to be made. The decision symbol contains<br />
an expression, such as a condition, that can be either true or false. The decision<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.