I need to know the difference between a=b++ and a=++b and what will be the value of "a" if b=10 in both cases | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

I need to know the difference between a=b++ and a=++b and what will be the value of "a" if b=10 in both cases

1st Aug 2016, 6:48 PM
kenny
11 Answers
+ 19
just to make it easier for novices. see this "a = b++" statement? now follow it from left to right 1. you see "a" first 2. then you see "=" sign, right? this means you should "assign" "a" to something to the right from "=" 3. then you see "b". yes, you see "b", not " b++". this means that "a" should be "b". " b" is 10, right? so does "a" 4. and only then you see "++" thing, which close to "b" and you increases "b" by "one" and have "11" in the end. now check "a = ++b" thingy 1 and 2 steps are same. 3. but now after "=" you see not "b", but "++". you cannot assign " a" to "++", because "++" is not a number, you should convert "++" to some kind of number first. check what you see near "++"? " b", or else "10" in our case, so you convert "++" to solid number "11" first and only then "assign" it to "a". in other word, a = b++, first goes "=", then "++" a = ++b, first goes "++", then "=" just try to read code from left to right.
2nd Aug 2016, 8:47 PM
23 43
23 43 - avatar
+ 16
to answer your question... the oder matters because if the increment sign is to the left of the variable it will increase by one first then pass the value. if the increment sign is after the variable it will pass the value first and then increase by one. for example var b = 10; var a = b++ ; // a = 10 , b = 11 var b = 10; var a = ++b; // a = 11 , b = 11 I hope this helps Kenny.
2nd Aug 2016, 8:28 AM
Katherine Adjahoe
Katherine Adjahoe - avatar
+ 3
For Example : Ex 1 : var x = 20; var y = x++; // this will not touch the value x.. but for value of y = x+1 x instead x = 20; Ex 2 : var x = 20; var y = ++x; // this will touch both the value x and y.. for value of y = x, x = 20.+ 1 ; the result would be same value x=21 and y = 21 ....
26th Sep 2016, 6:20 PM
Nabeel Nasir
Nabeel Nasir - avatar
+ 1
thank you
2nd Aug 2016, 3:18 PM
Elton Melo
Elton Melo - avatar
+ 1
thanks Katherine
2nd Aug 2016, 3:31 PM
kenny
+ 1
Инкримент увеличивает значение переменной сначало, а потом смотрит что он там увеличил, а пост инкримент смотрит что внутри переменной, а потом прибавляет
2nd Aug 2016, 5:36 PM
Ann Chebaeva
Ann Chebaeva - avatar
+ 1
var b = 10; var a = b++ ; // a = 10 , b = 11 var b = 10; var a = ++b; // a = 11 , b = 11
19th Aug 2016, 6:50 PM
Rohit Doshi
Rohit Doshi - avatar
+ 1
There are good answers here. If you or anyone reading this question is still confused or having trouble with the concept, then reading through the previous explanations above and then applying the info through practice with different values in the code playground is a good idea. This helped me understand the concept that (although simple) seems overly complex when explained in words within the lesson. Best, Chantel
21st Aug 2016, 9:33 AM
Nik Lowe
Nik Lowe - avatar
0
your welcome plz check out my little game at http://www.kadesignswow.com/rockPaperScissorGame/index.html or check out my blog at http://www.kadesignswow.com/blog you can find my source code there too
2nd Aug 2016, 3:23 PM
Katherine Adjahoe
Katherine Adjahoe - avatar
0
it's simple .... when ; a=b++ it means ... a=b and b= b++ & when a=++b it means .... first it increment in b ( e.g. b*= b++) then a=b* ... ( ***( note : we use. * this for better understand)
24th Aug 2016, 4:49 AM
vikrAm
vikrAm - avatar
0
// to know the difference between b++ and ++b; //copy past and run this C++ code you shall know the answers #include <iostream> using namespace std; int main() { cout << "Hello world!" << endl; int b=10,c,e,f=10; c=++b; cout<<"\n c=++b:c="<<c; cout<<"\n After that b="<<b; cout<<"\n\nBUT\n"; e=f++; cout<<"\n e=f++:e="<<e; cout<<"\n After that f="<<f; return 0; }
16th Feb 2020, 1:34 AM
Aginjith G J
Aginjith G J - avatar