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 431<br />
Operator Called Sample expression Explanation<br />
++ preincrement ++a Increment a by 1, then use the new value of<br />
a in the expression in which a resides.<br />
++ postincrement a++ Use the current value of a in the expression<br />
in which a resides, then increment a by 1.<br />
-- predecrement --b Decrement b by 1, then use the new value<br />
of b in the expression in which b resides.<br />
-- postdecrement b-- Use the current value of b in the expression<br />
in which b resides, then decrement b by 1.<br />
Fig. 14.13 The increment and decrement operators.<br />
Preincrementing (predecrementing) a variable causes the variable to be incremented<br />
(decremented) by 1, then the new value of the variable is used in the expression in which it<br />
appears. Postincrementing (postdecrementing) the variable causes the current value of the<br />
variable to be used in the expression in which it appears, then the variable value is incremented<br />
(decremented) by 1.<br />
The script of Fig. 14.14 demonstrates the difference between the preincrementing version<br />
and the postincrementing version of the ++ increment operator. Postincrementing the<br />
variable c causes it to be incremented after it is used in the document.writeln method<br />
call (line 14). Preincrementing the variable c causes it to be incremented before it is used<br />
in the document.writeln method call (line 20). The program displays the value of c<br />
before and after the ++ operator is used. The decrement operator (--) works similarly.<br />
Good Programming Practice 14.10<br />
For readability, unary operators should be placed next to their operands with no intervening<br />
spaces. 14.10<br />
1 <br />
2 <br />
3 <br />
4<br />
5 <br />
6 Preincrementing and Postincrementing<br />
7<br />
8 <br />
9 var c;<br />
10<br />
11 c = 5;<br />
12 document.writeln( "Postincrementing" );<br />
13 document.writeln( c ); // print 5<br />
14 document.writeln( "" + c++ ); // print 5 then increment<br />
15 document.writeln( "" + c ); // print 6<br />
16<br />
Fig. 14.14 Differences between preincrementing and postincrementing (part 1 of 2).<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.