Increment and decrement operators are unary operators that add or subtract one, to or from their operand, sequentially. They are commonly implemented in imperative programming languages like java, c, c++ the increment operator increments the value of the variable by one, and similarly, the decrement operator decrements the value of the variable by one.
a=6
a++ // a becomes 7
++a // a becomes 8
a-- // a becomes 7
--a // a becomes 6
the increment operator increments the value of the variable in the expression. If the increment operator(++) is used as a prefix (before the variable), then the value of the variable is increased, and then it returns the value. If the increment operator is used as postfix(after the variable), then the original value is returned, and then the variable is increased by one.
Example in C programming:
#include <stdio.h>
int main() {
int a = 6, b = 6;
// a is displayed
// Then, a is increased to 7.
printf("%d post-increment n", a++);
// b is increased to 7
// Then, it is displayed.
printf("%d pre-incrementn", ++b);
return 0;
}
Output:
6 post-increment
7 pre-increment
Example in C++:
#include <iostream>
using namespace std;
int main() {
int a = 6, b = 6;
// a is displayed
// Then, a is increased to 7.
cout << a++ << endl;
// b is increased to 7
// Then, it is displayed.
cout << ++b << endl;
return 0;
}
Output:
6
7
Example in Java programming:
class Operator {
public static void main(String[] args) {
int a = 6, b = 6;
// a is displayed
// Then, a is increased to 7.
System.out.println(a++);
// b is increased to 6
// Then, b is displayed
System.out.println(++b);
}
}
Output:
6
7
the decrement operator decrements the value of the variable in the expression. If the decrement operator(--) is used as a prefix (before the variable), then the value of the variable is decreased, and then it returns the value. If the decrement operator is used as postfix(after the variable) then the original value is returned, and then the variable is decreased by one.
Example in C programming:
#include <stdio.h>
int main() {
int a = 6, b = 6;
// a is displayed
// Then, a is decreased to 5.
printf("%d post-decrenet n", a--);
// b is decreased to 5
// Then, it is displayed.
printf("%d pre-decrementn", --b);
return 0;
}
Output:
6 post-decrement
5 pre-decrement
Example in C++:
#include <iostream>
using namespace std;
int main() {
int a = 6, b = 6;
// a is displayed
// Then, a is decreased to 5.
cout << a-- << endl;
// b is decreased to 5
// Then, it is displayed.
cout << --b << endl;
return 0;
}
Output:
6
5
Example in Java:
class Operator {
public static void main(String[] args) {
int a = 6, b = 6;
// a is displayed
// Then, a is decreased to 5.
System.out.println(a--);
// b is decreased to 5
// Then, b is displayed
System.out.println(--b);
}
}
Output:
6
5
Increment Operator |
Decrement Operator |
The increment operator increments the variable by one. |
The decrement operator decrements the operator by one. |
Prefix increment operator indicates the variable is incremented first, and then the expression is assessed utilizing the new value of the variable. |
Prefix decrement operator indicates the variable is decremented first, and then the expression is assessed utilizing the new value of the variable. |
Postfix increment operator indicates the expression is assessed first utilizing the original value of the variable, and then the variable is increased(incremented). |
Postfix decrement operator indicates the expression is assessed first utilizing the original value of the variable, and then the variable is decreased(decremented). |
i.e pre & post operators.
First, increment the value of “a” by 1 and then evaluate the expression i.e,
b=1;
Int a,b;
A=1;
b=++a;
pre-increment i.e b=a;
o/p: a=2 b=2
First, evaluate the expression and then increment the value of “ a “ by 1
b=a++, post-increment o/p: a=2 b=1
First, decrement the value of “a” by 1 and then evaluate the expression
b=-a; pre decrement o/p : a=0 b=0.
First evaluate the expression, later decrement the value of a by 1
b=a - -, post decrement B=1, a=0
Void main()
{
Int a;
A=10;
++a; || a=a+1;
Printf(“a=%d”,a);
}
o/p: a=11
void main()
{
Int a;
A=10;
A++; || a=a+1;
Printf(“a=%d”,a);
}
o/p: a=11
Until we are not assigning the data to any other variable, there are no difference b/w pre and post operators.
Void main()
a=++a + ++a + ++a
{
=a+a+a Int a;
a=4+4+4 A=1;
all should be evaluated at a time
A=++a + ++a + ++a;
Printf("a=%d”,a);
}
o/p: 12
|
Void main()
a=++a + a++ + ++a
{
a= a+a+a Int a;
3+3+3 A=1;
9 A=++a +a ++ + ++a;
Printf(“a2%d”,a);
}
A=10;
Void main()
{
Int a;
A=1;
a+a+a A=a++ + ++a + a++;
2+2+2
a=6 Printf(“a=%d”,a);
=6 a++ =7 o/p: a=8;
and a++ - 7+1=8
}
void main() a b
{
10 11
a=a++ + ++b Int a,b;
a=a+b A=b=10;
a=10+11 A=a++ + ++b;
a=21 a++=a22 B=++a + b++;
a=23 b=11 Printf ("a=%d b=%d, a, b”);
b=++a + b++
}
23+11 B=34 b++ B=35.
Void main()
{
a=1 b=3 Int a,b;
a=a-b+a A=1; b=3;
a=1 -2+2 A=a++ - - - b+ ++a;
a=1 B=++b + - -a - a++;
b=b+ a –a 3+1-1 B=3+1=4 A
b a=3 2 b= 2 3 1 3 3 2
b=++b + - -a -a++
A=a++ - - -b+ ++a
b+a-a A=a-b+a
3+2-2 2-2+2
b=3 A=2 ++a=3
}
Void main() a b c
{
12 2 1 3 2 Int a,b,c;
a=a-b+c A=1;b=2;c=3;
2-2+2 A=++a -b - - + - -c;
a=2 B=a -- + b++ -c- -;
a=1 C=a++ - ++b + c++;
Printf(“a%d b=%d c%d”,a,b,c);
a b c 2 1 1 2 2 1 A=2 A=1 A
b c 2 1 1 2 2 1 a b c 1 2 1
b=a- - +b++ -c - - 2 3 0
a+b-c 2+1-2 C=a++
++b + c++ a – b + c 1-3+1
C=-1 C+2=-1+1 A=2, b=3, c=0
Void main()
{
Int a,b,c; A=1;
b=2;c=3;
a=++a – b- - + - - c;
b=a- - + b++ - c- -;
c=a++ - ++b + c++;
printf(“a=%d b=%d c=%d ”,a,b,c);
a b c
b=a- - + b++ - c- - 1 2 2 3 1 0
a+b-c C=a++ - ++b + c++;
2+1-2 a-b+c 1-3+1 C=-1 A=2,b=3,c=0
Void main() a b c
{
2 3 4 3 6 7 Int a,b,c;
a= a – b + c A=2 , b=4. C=6;
3 – 3 + 6 A=++a - - -b + c++;
a=6 B=a++ -b+++ - - c;
a=6 7 b=3 c=7 6
C= - -a + ++b -++c;
Printf(“a %d b=%d c=%d b=a,b,c”);
a – b + c 6 - 3 + 6
A b c 7 6 10 6 10
C = a + b – 7 6 + 11 -7
C = 9 c++=10
A=6 b=11 c=10
Void main()
{
Int a; A=10;
Printf(“%d %d %d”, ++a, a++, ++a );
}
o/p: 13 11 11
When we are working with printf() f’n always it executes towards from right to left because it works with the help of stack.
In print f’n data need to be pass towards from right to left and data need to be point towards from left to right
Void main()
{
Int a;
A=100;
Printf(“ %d %d %d ”, a++,++a,a++);
Printf(“na=%d”,++a);
}
o/p : 102 102 100 a=104
void main()
{
Int a;
A=5;
Printf(“%d %d %d”, ++a,++a,++a);
}
o/p: 8 7 6
void main()
{
Int a;
A=10;
Printf(“%d %d %d”, a++,a++,a++);
Printf(“n a=%d”,a);
o/p: 12 11 10 a=13
void main()
{
Int a;
A=15;
Printf(“%d %d %d”,++a,++a);
}
Void main()
{
Int a; A=5;
Printf(“%d %d %d”, ++a,a=10,++a);
o/p: 11 10 6
void main()
{
Int a,b;
A=1;
B=++a * ++a * ++a;
Printf(“a=%d b=%d”,a,b);
Void main()
{
Int a,b;
a=1;
b=++a*a++*++a;
printf(“a=%d b=%d”,a,b);
}
b=a*a*a
b=3*3*3
b=27 a=4
void main()
{
Int a,b;
A=1;
B=a++ - ++a *a++;
Printf(“a=%d b=%d”,a,b);
}
A=4,b=8
Void main()
{
Int a,b;
A=1;
B=a++ - a++ * a++;
Printf(“a=%d b=%d”,a,b);
}
o/p : a=4, b=1 b=a*a*a a 1*1*1 b=1 a=4
void main()
{
Int a; A=1;
Printf(“n%d”,++a * ++a * ++a);
// case1 A=1;
Printf(“n%d”,++a * a++ * ++a);
//case2 A=1;
Printf(“/n%d”,a++ * ++a * a++);
//case 3 A=1;
Printf(“n%d”,a++ * a++ * a++);
// case 4 A=1;
Printf(“n%d”, ++a * a++ * a++);
// case 5 A=1;
Printf(“n%d”,a++ * a++ * ++a);
// case 6 A=1;
Printf(“n %d”,++a*++a*a++);
// case 7
}
Case1 : 2*3*4=24
case 5 2*2*3=12
Case2: 2*2*4=16
case 6 1*2*4=8
Case 3: 1*3*3=9
case7: 2*3*3 =18
Case 4: 1*2*3=6
[Explore: Types of Pointers in C]
Program:
Void main()
{
Int a,b;
A=b=1;
Printf(“n %d %d”, ++a * ++b, a++ * b++);
A=b=2;
Printf(“n %d %d”, ++a + b++ a++ , + ++b );
Printf(“n %d %d”, - -a * ++b, ++a * - -b);
}
Void main()
{
Int a;
A=printf(“welcome”);
Printf(“n a=%d”,a);
}
o/p: welcome a=7
void main()
{
Int a;
A=printf(“%d hello %d”,1-,20);
Printf(“na=%d”,a);
}
When we are working with printf f’n it returns an integer value i.e total number of characters printed on the console.
Void main()
{
Int a;
A=printf(“ %d Hello %d”,10,200);
Printf(“n a=%d”,a);
}
o/p: 10 Hello 200
a=12(2(0)+1(5)+5(Hello)+1(5)+3(200))
void main()
{
Int a;
A=printf(“n welcome %d”);
Printf(“NarreshIT”);
Printf(“n a=%d ”,a);
}
o/p : Naresh IT welcome 9 a=w(1(n) ++ (welcome +1(s) + 1(0));
According to behavior of the printf always it executes towards from right to left thats why when we are working with nested printf towards from right side first printf always executes first
void main()
{
Int a;
a= printf(“three %d n”); printf(“two %d n”);
printf(“one %d n”); printf(“a=%d”,a);
} o/p: one two three a=7
void main()
{
int a;
a= printf(“onen”) + printf(“ two n”) + printf (“threen”); printf(“a=%d”,a);
}
o/p : one two three a=14
void main()
{
int a;
a=printf(“n one”) + printf(“n two”) * printf(“n three”);
printf(“a=%d”,a);
}
o/p: two three one
void main()
{
int a;
a=2<5 ! printf(“NIT”);
printf(“welcome”);
printf(“n a=%d”,a);
}
o/p : NIT a=3
void main()
{
int a;
a= printf(“wi”) ? printf(“BIF”) : printf(“welcome”);
printf(“n a=%d”,a);
}
o/p: Hi bye a=3;
void main()
{
int a;
a= printf(“Hi”);
printf(“BYE”);
printf(“n a=%d”,a);
}
o/p: HIBYE Hello a=5;
void main ()
{
int a,b,c;
c=scanf(“%d%d”, &a, &b);
printf(“n a= %d b=%d c=%d ”,a,b,c);
}
// i/p values are 100 200
o/p : a=100 b=200 c=2
when we are working with scanf f’n it returns an integer value i.e total no.of i/p values provided by the user.
void main()
{
int a,b,c; a=b=100;
c=scanf("%d %d %d”,a,&b);
printf(“n a=%d b=%d c=%d,a,b,c”);
}
//ip values are 500 600
o/p : a=100 b=600 c=2
void main()
{
int a,b,c;
a=10;
b=20;
c=scanf(“%d %d %d”);
printf(“n a=%d b=%d c=%d ”,a,b,c);
}
Void main ()
{
Int a,b;
A==200*200/200;
B=200/200*200;
Printf(“a=%d b=%d”,a,b);
}
B=200/200*200 1*200 B=200 200*200=40,000
Range of integer =32767 40000
-32767 -32767
7232 7233
-25536 A=-25536/200;
=-25536 *10 ^2= -12768* 10^2 =-127.68
a=-127. 2 60000
-32768 -32767
27232 27233
-5536
a=-5536/300 =-18.45=-18
Viod main()
{
Int a;
A=32767;
If (++a<32767)
//-32768<32767
Printf(“ welcome %d”,a);
Else Printf(“hello %d”,a);
}
O/p : welcome-36768
Void main()
{
Inta;
A=-32768;
If(--a>-32768)
Printf(“welcome %d”,a);
Else Printf(“Hello %d”,a);
}
O/p: Welcome +32767
void main()
{
Int a;
A=32767;
If(a++>=32767)|| 32767>32767
Printf(“welcome %d”,a);
Else Printf (“hello %d”,a);
}
O/p : Hello -32768
Void main()
{
Inta; A=-32768;
If (aà-32768)
Printf(“welcome %d:,a);
Else Printf(“Hello %d”,a);
}
O/p: Hello -32767.
Conclusion:
Increment and decrement operators are used in looping and decision making in programming like C, C++, Java, and javascript. You can increase and decrease the values of the variables by using these operators
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.