Dec 16, 2011

Assignment & Increment/Decrement Operators

Assignment & Increment/Decrement Operators


This operators are used to shorten the code. Assignment Operators are used to assign value with arithmetic operators & Increment or Decrement used to increase by 1 or decrease by 1 using ++ or -- sign 



you can learn more check out following program:-


#include<iostream.h>
#include<conio.h>
int main()
{
int a = 10, b = 20; // declare and assign value to variable


a += b; // this means a = a + b
cout << a<<endl;  // print 30
a -= b; // this means  a = a - b
cout << a<<endl; // print 10


a *= b; // this means  a = a * b

cout << a<<endl; // print 200


a /= b; // this means  a = a / b

cout << a <<endl; // print 10

//using Increment and Decrement Operators



a++;
cout << a <<endl;   // print 11
cout << a++ <<endl;   // print 11
cout << a <<endl;   //print 12
cout << ++a <<endl;   // print 13

/* AS you see last four line as i cout a it shows 11 when i use a++ then also its shows 11 this is because when we use a++ its value remain same at that part or that time of declaration. This is known as post-increment. It Increase Value by 1 later of declaration. Like here after cout a++ on next line i cout << a; its shows value of a with 1 increment. & after that i use ++a this is pre-increment and this increase value by 1 at the same time it show 13*/


a--; // now a = 13
cout << a <<endl;   // print 12
cout << a-- <<endl;   // print 12
cout << a <<endl;   //print 11
cout << --a <<endl;   // print 10
 /* Here also concept is same of increment but difference is that there is increase by 1 but here decrease by 1 with same concept
*/


getch();
return 0;
}




OUTPUT




0 comments:

Post a Comment