+ 1
what's the output of this? c*(b*(++a)--)
would be pleased if explain it step by step
20 odpowiedzi
+ 9
athena
++/-- are unary operators means those can be applied to single variable ++a or a--
But not applied to (b*(++a), this is compound statement, not a single variable.
So there in (c*(b*(++a)--))
First in ++a is evaluated so a=a+1 so next a-- but this is post increment so value of a is used first in expression then after only a is decremented so value used in expression is a+1 but after expression a value is a = (a+1) - 1, (a+1) is result of ++a.
So expression is evaluated as
(c*(b*(++a)--))
=>(c*b*(a+1)--)
=>(c*b*(a+1)), a =( a+1)--
=>c*b*(a+1) will give you result, and a = a+1 - 1= a;
D_Stark
Yes. (++a) -- => (a+1)-- => a+1-1=>a
Preincrement value used in expression, post increment applied on pre incremented value because of braces..
It's invalid statement in java. And I think work only in c++. It's a unary operator works on single variable not on compound statement. Thank you..
+ 5
athena I posted an example code above step by step
+ 3
without checking I think it works like so...
"a" is incemented by 1 value is used
"b" is multiplied by "a" current value then "a" is decremented by 1 afterwards
"c" is then multiplied by the the value of that was left from "b" and "a" including the decrement of "a"
https://code.sololearn.com/c9WiZY0rGpUl/?ref=app
+ 2
Jayakrishna🇮🇳 hey, is the decrement taken from the variable "a" value or the value after the valuation im just curious as I cant use the decrement operator in this way in java as it looks for a variable but cant find one as it's been used. Thanks
+ 2
athena what I know is when the variables have been evaluated inside the parenthesis it becomes just a holding place for a number ie the result so..
a = 9
b = 5
c = 2
(a+(b+c)-1); the next step would look like so
(a+7-1) and next step
(15)
+ 2
Continuing.. athena
In your code :
int a=1;
int b=3;
int c=6;
=>(c*(b*(a+1)))
=>(6*(3*(1+1)))
=>(6*(3*(2)))
=>6*3*2
=>36
And
c=6,b=3, a = (a+1)-1=a=1 again..
@athena hope it clears..
You're welcome...
+ 1
athena i just taken a sample values to show you how the expressions result..
There b, c values will not change but value of a change first by (++a) by one and after using the value a-- happens so a value comes to same of previous value..
You can take any values for a, b, c.
++/-- will applied to single operator. It is *unary operator. So affect only single operator, not applied to compound statements.. So b won't change..
Edit :
that statement is valid in c++. But invalid in other languages mostly I think.....
+ 1
athena you'll have to show me your code
+ 1
》++a assigns the value of as a+1 and then proceeds. Therefore a becomes a+1 .
》(a+1)-- will reduce the value of a+1 after assihnment. So the value does not change
the result will be c*b*a +c*b
+ 1
athena have a look at lvalue and rvalue this should sort your understanding why this statment executes as ++a is a lvalue so the refrenece to this object is still accessible.
+ 1
athena
Have a look at this once for post/pre incrementation evaluation if you need to know.. Hope it helps..
https://www.sololearn.com/Discuss/1690694/?ref=app
0
#include <iostream>
using namespace std;
int main() {
int a = 2,b = 3,c= 4;
cout<<(c*(b*(++a)--))<<endl;
//this result to as :
cout <<(4*3*3); //first it use ++a, then done a-- so (++a)-- => a=2, output=36
}
0
D_Stark so after the increment which happens to a
then we will have b=6 by multiplying
then what will happen to the a?
why the decrement will only effects on a? and not the b=6?
0
Jayakrishna🇮🇳 why c is equal to 4?
0
Jayakrishna🇮🇳 yeah I'm ok with that but i cant get how will -- affect on "a" and not "b*(++a)"
and after multiplying b and a what will be the number that is gonna be multiplied by c
0
D_Stark but i think the answer is 36
I am not sure at all
0
Jayakrishna🇮🇳 thank you very much
0
Jayakrishna🇮🇳 thank you very much ❤️
0
You're welcome athena