+ 17

If b is assigned the value of 10, and a = b++, shouldn't a = 11?

15th Dec 2015, 1:11 AM
Xavier Duncan
30 Antworten
+ 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.
20th Jun 2016, 2:29 AM
DevCoder (John Sullivan)
DevCoder (John Sullivan) - avatar
+ 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
8th Sep 2016, 7:49 AM
Sebastian Norr
Sebastian Norr - avatar
+ 27
The value of a remains 10, and the value of b is incremented after the assignment operation.
28th Apr 2016, 11:15 AM
James Flanders
+ 6
no because u increment it after you assigned if it was a = ++b; it will be 11
25th Dec 2016, 1:45 PM
Deniz Bulmuş
Deniz Bulmuş - avatar
+ 3
a=++b; document.write(a);//a = 11
13th Jun 2016, 9:55 PM
xgqfrms
xgqfrms - avatar
+ 3
^ devcoder(john sullivan) nailed the point extremely clear, probably the best answer seen yet
6th Jul 2016, 5:02 PM
flare WK
flare WK - avatar
+ 2
b = a++ is the same as b = a; a++; because it calculates a after b is calculated. b=++a is correct
25th Oct 2016, 10:15 AM
Trey
+ 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....
24th Dec 2016, 5:27 AM
Aruj Sharma
Aruj Sharma - avatar
+ 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
25th Dec 2016, 7:48 AM
Shubham Mittal
Shubham Mittal - avatar
+ 1
a=10 b=11
19th Nov 2016, 6:06 AM
satya kumari
satya kumari - avatar
+ 1
a will be 11 only when a = ++b
20th Nov 2016, 2:48 PM
MD. Tofael Ahmed
MD. Tofael Ahmed - avatar
+ 1
b=10 first and for suffix b is then 11, but for first value a is 10. if a =++b then a = 11
11th Dec 2016, 5:10 PM
MD. Tofael Ahmed
MD. Tofael Ahmed - avatar
+ 1
what's the meaning of life?
24th Dec 2016, 4:11 AM
Aaron Marcel
Aaron Marcel - avatar
+ 1
It sets value of a to b then Increases the value of b . u can use prefix like ++b
25th Dec 2016, 6:18 AM
Saksham
Saksham - avatar
+ 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
25th Dec 2016, 1:24 PM
JAY PRAKASH
0
devcoder(john sullivan) is clear the point.
12th Aug 2016, 8:12 PM
Tanjed Hasan (Riyad)
Tanjed Hasan (Riyad) - avatar
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.
4th Nov 2016, 5:55 AM
Jose Ramirez
Jose Ramirez - avatar
0
actually ++b means first increment than assign but 'b++' means assign first than increment
10th Dec 2016, 8:39 PM
Pankaj Gautam
Pankaj Gautam - avatar
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.
24th Dec 2016, 9:47 AM
Blood
Blood - avatar
0
b = ++a; // prefix: a = a + 1; b = a; b = a++; // postfix: b = a; a = a + 1;
24th Dec 2016, 12:47 PM
Boris Batinkov
Boris Batinkov - avatar