Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | Increment and Decrement Operators - JavaScript(JS)

Chapter: Internet & World Wide Web HOW TO PROGRAM - The Ajax Client - JavaScript: Control Statements I

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Increment and Decrement Operators - JavaScript(JS)

JavaScript provides the unary increment operator (++) and decrement operator (--) (sum-marized in Fig. 7.13).

Increment and Decrement Operators

 

JavaScript provides the unary increment operator (++) and decrement operator (--) (sum-marized in Fig. 7.13). If a variable c is incremented by 1, the increment operator, ++, can be used rather than the expression c = c + 1 or c += 1. If an increment or decrement oper-ator is placed before a variable, it is referred to as the preincrement or predecrement op-erator, respectively. If an increment or decrement operator is placed after a variable, it is referred to as the postincrement or postdecrement operator, respectively.


Preincrementing (or predecrementing) a variable causes the program to increment (decrement) the variable by 1, then use the new value of the variable in the expression in which it appears. Postincrementing (postdecrementing) the variable causes the program to use the current value of the variable in the expression in which it appears, then increment (decrement) the variable by 1.

 

The script in Fig. 7.14 demonstrates the difference between the preincrementing ver-sion and the postincrementing version of the ++ increment operator. Postincrementing the variable c causes it to be incremented after it is used in the document.writeln method call (line 18). Preincrementing the variable c causes it to be incremented before it is used in the document.writeln method call (line 25). The program displays the value of c before and after the ++ operator is used. The decrement operator (--) works similarly.

 

      <?xml version = "1.0" encoding = "utf-8"?>

 

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

 

      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

 

      <!-- Fig. 7.14: increment.html -->

 

      <!-- Preincrementing and Postincrementing. -->

 

      <html xmlns = "http://www.w3.org/1999/xhtml">

 

      <head>

 

      <title>Preincrementing and Postincrementing</title>

 

        <script type = "text/javascript">

        <!—

        var c;

      c = 5;

 

      document.writeln( "<h3>Postincrementing</h3>" );

 

      document.writeln( c ); // prints 5

 

      // prints 5 then increments

 

      document.writeln( "<br />" + c++ );

 

      document.writeln( "<br />" + c ); // prints 6

      c = 5;

 

      document.writeln( "<h3>Preincrementing</h3>" );

 

      document.writeln( c ); // prints 5

 

      // increments then prints 6

 

      document.writeln( "<br />" + ++c );

 

      document.writeln( "<br />" + c ); // prints 6

      // -->

 

      </script>

 

      </head><body></body>

 

        </html>


The three assignment statements in Fig. 7.11 (lines 24, 26 and 28, respectively),

 

passes = passes + 1;

failures = failures + 1;

student = student + 1;

 

can be written more concisely with assignment operators as

passes += 1;

failures += 1;

student += 1;

with preincrement operators as

 

++passes;

 

++failures;

 

++student;

 

or with postincrement operators as

 

passes++;

 

failures++;

 

student++;

 

It is important to note here that when incrementing or decrementing a variable in a statement by itself, the preincrement and postincrement forms have the same effect, and the predecrement and postdecrement forms have the same effect. It is only when a variable appears in the context of a larger expression that preincrementing the variable and post-incrementing the variable have different effects. Predecrementing and postdecrementing behave similarly.

Figure 7.15 lists the precedence and associativity of the operators introduced to this point. The operators are shown top to bottom in decreasing order of precedence. The second column describes the associativity of the operators at each level of precedence. Notice that the conditional operator (?:), the unary operators increment (++) and decre-ment (--) and the assignment operators =, +=, -=, *=, /= and %= associate from right to left. All other operators in the operator precedence table (Fig. 7.15) associate from left to right. The third column names the groups of operators.




Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.