Increment and decrement operator
Increment and decrement operator
C# provides two operators that can be used to increase or decrease a variable's value by one. These are the increment operator (++) and decrement operator (--). These operators are known as unary operators because they are applied to a single value. The operators can be placed before the variable to be adjusted (prefix) or after it (postfix).
Example:-
C# provides two operators that can be used to increase or decrease a variable's value by one. These are the increment operator (++) and decrement operator (--). These operators are known as unary operators because they are applied to a single value. The operators can be placed before the variable to be adjusted (prefix) or after it (postfix).
postfix=i++;( In post i=i+i)
prefix=++i; (In pre i=i+i)
Increment operator
int
a;
int
b;
// Prefix. a is incremented before its value is assigned to b
a = 10;
b = ++a;
//a = 11, b = 11;
// Postfix. a is incremented after its value is assigned to b
a = 10;
b = a++;
//a = 11, b = 10;
One more example to explain you better
Decrement operator
Decrement operator is same as Increment operator .
Pre = --i
Post = i--
Example:-
These increment & decrement are use in loops.
Example(using both Increment and decrement operator ):-
int i=5,j=9,a,b; a=i++; // output 5
b=++j; // output 10
Decrement operator
Decrement operator is same as Increment operator .
Pre = --i
Post = i--
Example:-
int i=10, a=15,z;
z=(a++)*(++i)/i;
// using their constants
z= 15*11/11;
These increment & decrement are use in loops.
Example(using both Increment and decrement operator ):-
#include<conio.h>#include<stdio.h>
void main()
{ int i=10,a=20,b;
b=(i++)+(--a);
printf("a:=%d\n",a);
printf("b:=%d\n",b);
printf("i:=%d\n",c);
getch();/* this function is scan input .I used it to stop program from closing because when program done its all process it autometically get close*/
}
Comments
Post a Comment