+ 8
What are the purpose of pre/post Increment and decrements operators ?
how they works ? where can we to use pre and where post ?
6 Antworten
+ 10
To understand Pre-increment and Post-increment note that:
$b=$a++; // post-increment
is equivalent to:
$b=$a;
$a++; //$a=$a+1;
while
$b=++$a; // pre-increment
is equivalent to:
$a++; //$a=$a+1;
$b=$a;
In both cases $a is increment of 1 (respect the original value), while $b is incremented only in pre-increment case.
Pre-increment and Post-increment are both equivalent to the execution of 2 statements ($b=$a; $a++;), what change is the order in which these statements are executed.
I hope this can help.
+ 6
As the words itself suggest us :
pre means before, we have to use the operator first then variable name like (++a)
In this case first the value of a will increase by one then the new value of a will used.
post means after , we have to use operator after variable name like (a++)
In this case current value of will be in use and value will be increase by one in later steps (in the memory it will increase by one)
suppose,
a=5;
b=++a;
in this case , now the values in memories are a=6,b=6
but if we use
b=a++;
then the values in memories are a=6; but b=5;
+ 3
They are shorthands for x+=1 and x-=1.
Indeed, they are more useful than the normal ways (+=1 and -=1) because in machine language the increment operation is faster than the add operation
+ 1
why we increment the value of a?
we have also increase the value of a by factor 1 itself
- 2
in pre case
- 4
Self answering???????????????????????????????????? I will explain better
Let a=1
Cout<<++a;
Cout<<a;
The output will be 2. 2.
Now
Cout<<a++;
Cout<<a;
The output is. 1. 2.