Increment and Decrement operator in c
Increment and decrement operators are usually used in looping statements like while loop, for loop and, do-while loop.
Increment operator: increment operator is used for incrementing the value by one.
Increment operator symbol: ++
Example:
int i=0;
i++; // increment the value by one
i++ is same as i=i+1 Or i+=1;
Before incrementation, the value of i was 0. After incrementing the value of i by 1, the value of i became 1.
There are two types of increment operator
1. Pre-increment operator
2. Post-increment operator
1. Pre-increment operator
In the pre-increment operator, the value is first incremented then it is used in the expression.
Syntax,
++variable_name;
Example:
#include<stdio.h>
int main()
{
int s=5;
while(s<=10)
{
printf("%d ",++s);
}
return 0;
}
Output:
6 7 8 9 10 11
Observe the above program,
++s is the pre-increment of the int variable s.
The condition for while-loop is,
s<=10.
In the first iteration of the loop, the value of s is compared with the condition.
The value of s is 5, which is less than or equal to 10.
The control comes inside the while loop,
In the printf statement, the value of s is first incremented by 1. The value becomes 6, and 6 is printed.
The loop continues until the condition becomes false.
When the value of s becomes 10, it is again compared with the condition. The condition becomes true as 10<=10. The control comes inside the loop, and the value gets incremented by 1 and, 11 is printed.
The new value of s is 11. Again s is compared with the condition, the condition becomes false,
As 11 is not less than equal to 10.
2. Post-increment operator:
In the post-increment operator, the value is first used in the expression, then it is incremented.
Syntax:
Variable_name++;
Example:
#include<stdio.h>
int main()
{
int s=5;
while(s<=10)
{
printf("%d ",s++);
}
return 0;
}
5 6 7 8 9 10
Observe the above program,
s++ is a post-increment operator of the int variable s.
The program is the same as the first program. The only difference is, here we are using a post-increment operator.
Initially, the value of s is 5.
In the first iteration of the loop, in the printf statement, the value of s is first printed, then it is incremented by one.
The value of s is now 6, the loop continues till the condition becomes false.
During the last iteration, when s becomes 10, the value of s is again compared with the condition, the condition becomes true and s is printed then it is incremented by 1.
Now the value of s is 11, again the value of s is compared with the condition. 11 is not less than Or equal to 10. Hence, the condition becomes false and control comes out of the loop.
2. decrement operator: Decrement operator is used to decrease the value by one.
decrement operator symbol: --
Example:
int i=1;
i--; // decrement the value by one.
i-- is same as i=i-1 Or i-=1;
the new value of i is now 0.
There are two types of decrement operator
1. Pre-decrement operator
2. Post-decrement operator
1. Pre-decrement operator
In the Pre-decrement operator, the value is first decremented then it is used in the expression.
Syntax,
--variable_name;
Example:
#include<stdio.h>
int main()
{
int s=5;
while(s>=0)
{
printf("%d ",--s);
}
return 0;
}
4 3 2 1 0 -1
Observe the above program,
--s is the per-decrement of the int variable s.
The condition for while-loop is,
s>=0.
In the first iteration of the loop, the value of s is compared with the condition.
The value of s is 5, which is greater than equal to 0.
The control comes inside the while loop,
In the printf statement, the value of s is first decremented by 1. The value becomes 4, and 4 is printed.
The loop continues until the condition becomes false.
When the value of s becomes 0, it is again compared with the condition. The condition becomes true as 0>=0. The control comes inside the loop, the value gets decremented by 1, and -1 is printed.
The new value of s is -1. Again s is compared with the condition. The condition becomes false, As -1 is not greater than equal or to 0.
2. Post-decrement operator:
In the post-decrement operator, the value is first used in the expression, then it is decremented.
Syntax:
Variable_name--;
Example:
#include<stdio.h>
int main()
{
int s=5;
while(s>=0)
{
printf("%d ",s--);
}
return 0;
}
5 4 3 2 1 0
Observe the above program,
s-- is a post-decrement operator of the int variable s.
The program is the same as the third program. The only difference is, here we are using a post-decrement operator.
Initially, the value of s is 5.
In the first iteration of the loop, in the printf statement, the value of s is first printed, then it is decremented by one.
The value of s is now 4. The loop continues till the condition becomes false.
During the last iteration, when s becomes 0, the value of s is again compared with the condition. The condition becomes true and s is printed, then it is decremented by 1.
Now the value of s is -1, again value of s is compared with the condition. -1 is not greater than Or equal to 0. Hence, the condition becomes false, and control comes out of the loop.
Post a Comment
Post a Comment