+ 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
11 ответов
+ 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.
+ 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.
+ 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 ....
+ 1
thank you
+ 1
thanks Katherine
+ 1
Инкримент увеличивает значение переменной сначало, а потом смотрит что он там увеличил, а пост инкримент смотрит что внутри переменной, а потом прибавляет
+ 1
var b = 10;
var a = b++ ; // a = 10 , b = 11
var b = 10;
var a = ++b; // a = 11 , b = 11
+ 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
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
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)
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;
}