Thursday, 6 December 2012

Using Increment and Decrement Operators

              The increment operator (++) provides a shortcut to increase the value of a variable by one.The decrement operator (--) simply subtracts a value of one from a variable.The increment and decrement operators are helpful when used with loop counter variables.

  1. <html>
  2. <head>
  3. <title>Increment and Decrement Operator</title>
  4. </head>
  5. <body>
  6. <h3>Using Increment Operator</h3>
  7. <%
  8. int i=4;
  9. %>
  10. <p>
  11. The value of <b>i</b> is <%=i%>
  12. <br>
  13. The value of <b>++i</b> is <%=++i %>
  14. <p>
  15. <h3>Using Decrement Operator</h3>
  16. <p>
  17. The value of <b>i</b> is <%=i %>
  18. <br>
  19. The value of <b>--i</b> is <%=--i%>
  20. </body>
  21. </html>

Description:

  • Assign an initial integer value to a variable.This variable will be used to illustrate the increment and decrement operators.
  • Type the increment operator (++) before the variable name and use the Expression tag to print the result to the Web page.The variable will be incremented by one before it is displayed.
  • Type the decrement operator (--) before the variable name and use the Expression tag to print the result to the Web page.The variable will be decreased by one before it is displayed.

*******************************OUTPUT****************************************

Using Increment Operator

The value of i is 4
The value of ++i is 5

Using Decrement Operator

The value of i is 5
The value of --i is 4


******************************************************************************

















No comments:

Post a Comment