+ 2

How output getting for this

int a=2; int b=3; a+= ++a * b; cout <<a;

30th May 2017, 5:23 PM
Devika S Nair
Devika S Nair - avatar
8 Antworten
+ 2
12 1. ++a=3 2. ++a*b=9 3. a+=++a*b=12 C++ starts to process from immediately right side of an equivalance
30th May 2017, 5:43 PM
nzm
+ 3
Alwayzz remember that SL provides code playground You can test your programs there too😉 if u dnt knw a answr take a screenshot and test it in code playground😁✌️👍
30th May 2017, 6:59 PM
U L Knw Me soon😉😋
U L Knw Me soon😉😋 - avatar
+ 2
1. x+=x means x=x+x 2. So a+=++a*b means a=++a*b+a
30th May 2017, 7:02 PM
nzm
+ 2
int a,b; a=2; // ++a=3 value of a now used is 3 b=3; a+=++a*b; /*you can interpret it as........ x+=(something); is equivalent to x=x+(something); Same applicable here as a+= ++a * b; is equivalent to a= ++a + (++a*b); so you'll be getting a= 3+(3*3)=12*/
30th May 2017, 7:36 PM
Syed Hamza Ali
Syed Hamza Ali - avatar
+ 1
lil confused with third line.. result of ++a*b=9.. ok..but how dat 9 becomes 12 when operates with "a+=" in the 3rd line.. how dat a+= (I think itz equivalent to ++a) works here
30th May 2017, 6:57 PM
Devika S Nair
Devika S Nair - avatar
+ 1
No actually not if a variable's value incremented by preincrement operator it effects the value throughout scope hence a's initial value becomes 3 and remains so unless changed.
30th May 2017, 7:49 PM
nzm
0
Thank you!
30th May 2017, 7:22 PM
Devika S Nair
Devika S Nair - avatar
0
:)
30th May 2017, 7:31 PM
nzm