+ 17
If b is assigned the value of 10, and a = b++, shouldn't a = 11?
30 ответов
+ 72
As the course lesson taught when you put the ++ in front of the variable then the code adds one to the variable, then it does the assingment. So when doing a = ++b the system does: b=b+1, then does a=b. But if you have a = b++, then the system does the assignment of a=b first, and then it increments b.
You can think of it as wherever the plus signs are is the order of incrementing. If the plus signs are "before" (that is, in front of) the variable then it increments the variable "before" doing the assignment. And if the plusses are "after" (that is they follow the variable) then the system does the assingment first and increments the variable "after"wards.
+ 28
var a, b, c; // multiple variable declaration
a = 10;
b = a++; // b is still 10
a = 10;
c = ++a; // c is now 11
= a++ ASSIGNS the value FIRST and then INCREASES the value.
= ++a INCREASES the value FIRST and then ASSIGNS the value
+ 27
The value of a remains 10, and the value of b is incremented after the assignment operation.
+ 6
no because u increment it after you assigned if it was a = ++b; it will be 11
+ 3
a=++b; document.write(a);//a = 11
+ 3
^ devcoder(john sullivan) nailed the point extremely clear, probably the best answer seen yet
+ 2
b = a++ is the same as b = a; a++; because it calculates a after b is calculated. b=++a is correct
+ 2
that is how prefix and postfix increment/decrement operator works..... a=b++ (postfix) first assigns the value of b to a and then increments b.
while if you would have used. a=++b (prefix) the value of b would have been incremented first and then assigned to a....
+ 2
because it first uses the value of b then increments b,,, a=b++
but if the code is like this b++;
a=b;// a=11 in this case
+ 1
a=10
b=11
+ 1
a will be 11 only when a = ++b
+ 1
b=10 first and for suffix b is then 11, but for first value a is 10. if a =++b then a = 11
+ 1
what's the meaning of life?
+ 1
It sets value of a to b then Increases the value of b . u can use prefix like ++b
+ 1
Since b++ says its a post increment operator so firstly its value will be used and then it will get incremented from the next line
0
devcoder(john sullivan) is clear the point.
0
This confused me too but I understand now. It's prefix(before ++b) and postfix(after b++) operators. Since the operators are after the variable b, you will increment after the assignment of the variable a. In this case b will remain 10 until after it has been assigned to a, then b will increment.
0
actually ++b means first increment than assign but 'b++' means assign first than increment
0
No because what is being done is post incrementation. (b++) When the increment operator comes after the variable, basically, so what is happening in the expression is :
a = b++
1) 10 is being assigned to a
2)After a is now assigned 10, b is incremented by 1
3)b is now 11 and the expression is finished evaluating.
If it were to be :
a=++b
1) b would be incremented to 11
2) b would be assigned to a
3)a is now 11 and the expression is done evaluating.
0
b = ++a; // prefix: a = a + 1; b = a;
b = a++; // postfix: b = a; a = a + 1;