What are the purpose of pre/post Increment and decrements operators ? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 8

What are the purpose of pre/post Increment and decrements operators ?

how they works ? where can we to use pre and where post ?

23rd Oct 2016, 9:44 AM
Suryaprakash Tiwari
Suryaprakash Tiwari - avatar
6 Réponses
+ 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.
5th May 2017, 9:41 PM
Daniele Gaito
Daniele Gaito - avatar
+ 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;
23rd Oct 2016, 9:49 AM
Suryaprakash Tiwari
Suryaprakash Tiwari - avatar
+ 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
25th Nov 2016, 9:34 PM
Salvatore Correnti
Salvatore Correnti - avatar
+ 1
why we increment the value of a? we have also increase the value of a by factor 1 itself
24th Oct 2016, 1:59 PM
Hassam Saeed
Hassam Saeed - avatar
- 2
in pre case
24th Oct 2016, 1:59 PM
Hassam Saeed
Hassam Saeed - avatar
- 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.
23rd Oct 2016, 10:26 AM
Jishnu
Jishnu - avatar