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 405<br />

prints Passed if the student’s grade is greater than or equal to 60 and prints Failed if the<br />

student’s grade is less than 60. In either case, after printing occurs, the next pseudocode<br />

statement in sequence (the next statement after the whole if/else structure) is “performed.”<br />

Note that the body of the else is also indented.<br />

Good Programming Practice 14.2<br />

Indent both body statements of an if/else structure. 14.2<br />

The indentation convention you choose should be carefully applied throughout your<br />

programs (both in pseudocode and in <strong>JavaScript</strong>). It is difficult to read programs that do not<br />

use uniform spacing conventions.<br />

The preceding pseudocode if/else structure may be written in <strong>JavaScript</strong> as<br />

if ( studentGrade >= 60 )<br />

document.writeln( "Passed" );<br />

else<br />

document.writeln( "Failed" );<br />

The flowchart of Fig. 14.4 nicely illustrates the flow of control in the if/else structure.<br />

Once again, note that the only symbols in the flowchart (besides small circles and<br />

arrows) are rectangles (for actions) and a diamond (for a decision). We continue to emphasize<br />

this action/decision model of computing. Imagine again a deep bin containing as many<br />

empty double-selection structures as might be needed to build a <strong>JavaScript</strong> algorithm. The<br />

programmer’s job is to assemble the selection structures (by stacking and nesting) with<br />

other control structures required by the algorithm and to fill in the empty rectangles and<br />

empty diamonds with actions and decisions appropriate to the algorithm being implemented.<br />

<strong>JavaScript</strong> provides an operator called the conditional operator (?:) that is closely<br />

related to the if/else structure. The operator ?: is <strong>JavaScript</strong>’s only ternary operator—<br />

it takes three operands. The operands together with the ?: form a conditional expression.<br />

The first operand is a boolean expression, the second is the value for the conditional expression<br />

if the condition evaluates to true and the third is the value for the conditional expression<br />

if the condition evaluates to false. For example, the statement<br />

print “Failed”<br />

false<br />

grade >= 60<br />

true<br />

Fig. 14.4 Flowcharting the double-selection if/else structure.<br />

print “Passed”<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!