Increment operator (++)
Many time in our program we need to increase the value of variable one by one for any particular limit. Specially, when we use the loop statement in our program such as for or while loop (we learn detail later about for or while loop ).
* for loop : for(var = 0; var<=100; var++) { statement ……………; } * while loop : var = 0; while ( var <= 100) { var ++ ; } Here increasing value one by one of the variable var from 0 to 100 using the increment operator. |
There are no space between the two +.
Example :
#include<stdio.h> void main() { int i; for( i = 0 ; i<=3 ; i++) { printf(“Shojib Org\n”); } } …………………………………………………………. Output : Shojib Org Shojib Org Shojib Org |
We can use this operator in before variable and after the variable. Before using called prefix (++i) and use after called postfix (i++).
Both are doing the same thing but compiler take it some different. If we use postfix (i++) then firstly the compiler take the initial value of variable and increase one by one to add the initial value. But in prefix, first compile add one with the initial value and execute the program.
Let look at the example :
#include<stdio.h> void main() { int var; var = 1; printf(“Using postfix (var++) var = %d\n ”,var++); printf(“Value after increment is : %d\n ”, var); var = 1; printf(“Usint prefix (++var) var = %d\n”,++var); printf(“Value after increment is : %d\n”,var); } ………………………………………………………… Output : Using postfix (var++) var = 1 Value after increment is : 2 Usint prefix (++var) var = 2 Value after increment is : 2 |
I hope you are understand that operation. Thank you.
Written by “Shojib”
No comments:
Post a Comment