C++ Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
C++ Tutorial
C++ Language Tutorial - Object Oriented Programing - OOPs History of C++
C++ Language Tutorial - Object Oriented Programing - OOPs Structure of C++
C++ Language Tutorial - Object Oriented Programing - OOPs C++ Datatypes
C++ Language Tutorial - Object Oriented Programing - OOPs Variables
C++ Language Tutorial - Object Oriented Programing - OOPs Constants
C++ Language Tutorial - Object Oriented Programing - OOPs Operators
C++ Language Tutorial - Object Oriented Programing - OOPs Control Structures
C++ Language Tutorial - Object Oriented Programing - OOPs Arrays
C++ Language Tutorial - Object Oriented Programing - OOPs Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Class
C++ Language Tutorial - Object Oriented Programing - OOPs Predefined Functions
C++ Language Tutorial - Object Oriented Programing - OOPs I/O Functions
C++ Language Tutorial - Object Oriented Programing - OOPs String, Character Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Mathematical Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Time Date Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Dynamic Allocation
C++ Language Tutorial - Object Oriented Programing - OOPs Utility Functions
C++ Language Tutorial - Object Oriented Programing - OOPs OOP's Concept
C++ Language Tutorial - Object Oriented Programing - OOPs Special Topics
C++ Language Tutorial - Object Oriented Programing - OOPs Type casting
C++ Language Tutorial - Object Oriented Programing - OOPs Feedback
C++ Language Tutorial - Object Oriented Programing - OOPs Ask Your Doubts
 





Increment / Decrement Operators in C++


Tutorials Cpp

Topic

What are Increment / Decrement Operators in C++?
How to add or subtract an integer variable in C++?



Explanation

The increment and decrement operator are used to increase or decrease the value of an operand by "1" or simply, to add or subtract integer variable. These operators can be used add or subtract an operand before the operation or after the operation, which is known as post increment or pre increment of integer variable and vice versa.

Example:

  #include <iostream.h>
  using namespace std;
  void main( )
   {
      int i = 3, k,l, n=6,m;
      //Example for post and pre increment
      k = i++;
      l = ++i;
      cout << "Post Increment value for 3 is::" <<  k  << endl;
      cout << "Pre Increment value for 4 is::"  <<  l  << endl;
      //Example for post and pre decrement
      m=--n;
      cout << "Pre Decrement value of 6 is::" << m << endl;
      for( int x = 1; x > 0;x-- )
       {
         cout << "Value of x before Post Decrement::" << x << "\n";
         x--;
         cout << "Value of x after Post Decrement::" << x << "\n";  
       }
   } 

Result:
   Post Increment value for 3 is::4 
   Pre Increment value of 4 is::5
   Pre Decrement value of 6 is::5   
   Value of x before Post Decrement::1
   Value of x after Post Decrement::0

In the above example the difference is that k will be assigned 3 first then its post-incremented to become "4" . In the next scenario "k" already has the pre-incremented value of i that is "5".The pre-decrement operator is used to decrement the value of n even before assignment.Using the post-decrement operator the value of "x" is decremented after the assignment using a loop.

Example:

  #include <iostream.h>
  using namespace std;
  void main( )
   {
     for( int x = 1; x > 0;x-- )
      {
        cout << "Post decrement value of  x is::" << x << "\n";  
      }
   } 

Result:
   Post Decrement value of x is::5 
   Post Decrement value of x is::4
   Post Decrement value of x is::3   
   Post Decrement value of x is::2
   Post Decrement value of x is::1

In the the above example the given value is post-decremented after the assigned value of "5".








Note
C++ is one of the most used programming languages in the world. Also known as "C with Classes". Hope you enjoy this tutorial. Do send your feedback or suggestions on this C++ tutorial. This is a copyright content.

Other Links

web hosting