home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / oper2.cpp < prev    next >
C/C++ Source or Header  |  1993-03-18  |  751b  |  27 lines

  1. /*
  2.    C++ program illustrates the feature of the increment operator.
  3.    The ++ or -- may be included in an expression.  The value   
  4.    of the associated variable is altered after the expression  
  5.    is evaluated if the var++ (or var--) is used, or before     
  6.    when ++var (or --var) is used.                              
  7. */
  8.  
  9. #include <iostream.h>
  10.  
  11. main()
  12. {
  13.    int i, k = 5;
  14.  
  15.    // use post-incrementing
  16.    i = 10 * (k++); // k contributes 5 to the expression
  17.    cout << "i = " << i << "\n\n"; // displays 50 (= 10 * 5)
  18.  
  19.    k--; // restores the value of k to 5
  20.  
  21.    // use pre-incrementing
  22.    i = 10 * (++k); // k contributes 6 to the expression
  23.    cout << "i = " << i << "\n\n"; // displays 60 (= 10 * 6)
  24.    return 0;
  25.  
  26.