1 / 123
What is the difference between ++var and var++?
The ++ operator is called the increment operator. When the operator is placed before the variable (++var), the variable is incremented by 1 before it is used in the expression. When the operator is placed after the variable (var++), the expression is evaluated, and then the variable is incremented by 1.
The same holds true for the decrement operator (--). When the operator is placed before the variable, you are said to have a prefix operation. When the operator is placed after the variable, you are said to have a postfix operation.
For instance, consider the following example of postfix incrementation:
int x, y;
x = 1;
y = (x++ * 5);
In this example, postfix incrementation is used, and x is not incremented until after the evaluation of the expression is done. Therefore, y evaluates to 1 times 5, or 5. After the evaluation, x is incremented to 2.
Now look at an example using prefix incrementation:
int x, y;
x = 1;
y = (++x * 5);
This example is the same as the first one, except that this example uses prefix incrementation rather than postfix. Therefore, x is incremented before the expression is evaluated, making it 2. Hence, y evaluates to 2 times 5, or 10.
Asked In ::
Sajal Das
13 Jan, 2020 6:04 PM
The same holds true for the decrement operator (--). When the operator is placed before the variable, you are said to have a prefix operation. When the operator is placed after the variable, you are said to have a postfix operation.
For instance, consider the following example of postfix incrementation:
int x, y;
x = 1;
y = (x++ * 5);
In this example, postfix incrementation is used, and x is not incremented until after the evaluation of the expression is done. Therefore, y evaluates to 1 times 5, or 5. After the evaluation, x is incremented to 2.
Now look at an example using prefix incrementation:
int x, y;
x = 1;
y = (++x * 5);
This example is the same as the first one, except that this example uses prefix incrementation rather than postfix. Therefore, x is incremented before the expression is evaluated, making it 2. Hence, y evaluates to 2 times 5, or 10.
Subir Ghosh
11 Jan, 2020 3:32 PM
Arnab Mitra
23 Feb, 2019 4:46 AM
++var returns its value after incrementing.
Binoy Sarkar
23 Feb, 2019 4:46 AM
Chanchal Basak
23 Feb, 2019 4:46 AM
Debashish Karmakar
23 Feb, 2019 4:46 AM
x = ++var
means:
PHP Code:
var = var + 1
x = var
postincrement
x = var++
means:
PHP Code:
x = var
var = var + 1
Dipankar Saha
23 Feb, 2019 4:46 AM
var++: It will first use variable and then increment it.
Raghavan
23 Feb, 2019 4:46 AM
var++ returns its value before incrementing.
++var returns its value after incrementing.
Ravi
23 Feb, 2019 4:46 AM
in var increment of the variable occured first and then initialization
happen and in var initialization of the variable ocurred first.
int x, y;
x = 1;
y = ( x * 5);
This example is the same as the first one, except that this example uses prefix incrementation rather than postfix. Therefore, x is incremented before the expression is evaluated, making it 2. Hence, y evaluates to 2 times 5, or 10.
Sumath
23 Feb, 2019 4:46 AM
testing