+ 12

Why when x=2 then (++x)*(++x)*(++x)=80 ?

Can anyone please explain ?

9th Aug 2018, 2:45 PM
Sumith Bakshi
19 Answers
+ 32
Sumith Bakshi , it's not recommended to use two or more than two pre increment in a single expression.... this leads to undefined behaviour of compiler.. different compiler behaves differently for this and sometimes same compiler behaves differently at different time. it's evaluated as below on sololearn compiler: first ++x makes x as 3.. second ++x makes x as 4.. now multiplication is done as x*x which becomes 16... now, last ++x makes x as 5 and result is 16*5=80.. try with x=3 and result will be 5*5*6=150 but, it is undefined behaviour and one should not use multiple preincrement in single statement..
9th Aug 2018, 3:25 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 13
Multiple postfix/prefix operators used between two sequence points would result in undefined behaviour. https://www.sololearn.com/Discuss/205618/?ref=app https://www.sololearn.com/Discuss/430244/?ref=app https://www.sololearn.com/Discuss/523078/?ref=app
10th Aug 2018, 1:23 AM
Hatsy Rei
Hatsy Rei - avatar
+ 9
x=2 (++×) =3 (++×) =4 so : (++×)*(++×) = 4*4 (++×) =5 so : (++×)*(++×)*(++×) = 4*4*5
10th Aug 2018, 11:44 AM
Luis Felipe Arroyave Valencia
Luis Felipe Arroyave Valencia - avatar
+ 8
I've made using a Java Program and the result was 60... import java.util.*; public class Main { public static void main(String[] args) { int x = 2; int y = (++x) * (++x) * (++x); System.out.println(x); System.out.println(y); } } Output: 5 60 As the analisys is made from left to right, I think the values o X were 3,4 and 5, like this: int y = 3 * (++x) * (++x); int y = 3 * 4 * (++x); int y = 3 * 4 * 5;
10th Aug 2018, 1:35 PM
Nelson Ferreira
+ 6
Hope that you don't have to maintain code written by someone who thinks he is so smart that he uses expressions like these. There was a time when the outcome of these expressions was predictable (Borland C++), but not anymore.
10th Aug 2018, 2:06 AM
lion
lion - avatar
+ 2
I tested in C language
9th Aug 2018, 3:30 PM
Sumith Bakshi
+ 2
4x*4x*4x=34
9th Aug 2018, 7:21 PM
robert rizzo
robert rizzo - avatar
+ 2
Interesting point. Never met this earlier 👍
10th Aug 2018, 10:00 PM
Steppenwolf
Steppenwolf - avatar
+ 1
like that crazy variable🤕🤕
10th Aug 2018, 12:02 PM
Munir Ahmad
Munir Ahmad - avatar
0
3*4*5=60
10th Aug 2018, 3:08 PM
Alex Enough
Alex Enough - avatar
0
Interesting to see the ++ being used more than once in a statement. Usually mathematical components would do fine in a While statement for this.
10th Aug 2018, 6:25 PM
Apple Blossom
Apple Blossom - avatar
0
kotlin fun main(args: Array<String>) { var x: Int = 10; x--; // x = x - 1; println(x); var x1: Int = 8; val y1 = ++x1; // x is incremented, assigned to y println(y1); var x2: Int = 5; val y2 = x2++; // x is assigned to y,incremented println(y2); var x3: Int = 9; x3++; // x = x + 1; println(x3); }
10th Aug 2018, 6:30 PM
Munir Ahmad
Munir Ahmad - avatar
0
3*4*5=80
11th Aug 2018, 11:38 AM
Ahmad Sultan
Ahmad Sultan - avatar
0
x=2 4*4*5=80
12th Aug 2018, 11:34 AM
Rishabh Singh
Rishabh Singh - avatar
0
operator precedence of parenthesis is higher than multiplication so x was incremented to 3 then 4 then it resolve the operation of first to operand with * so it was 16 and final step x was 5 and multiplication become 80
27th Sep 2019, 11:35 AM
Nirmal Patel
Nirmal Patel - avatar
0
3*4*5 prefix addition increment x before using it so he increment first x ... Then increment second x depends on its new valve
13th Nov 2019, 8:45 AM
Ahmad Sultan
Ahmad Sultan - avatar
0
This is a single line code, and I have not seen any documentation saying bracket operation should be executed after multiplication. Hence we can't multiply before completing instructions within brackets. If the code is split into different lines then I would agree
13th Nov 2019, 10:55 AM
Toja
Toja - avatar
- 1
This result (80) should be considered as incorrect. Brackets are executed first: x++, again x++, and again x++. If x = 2 then x++ is 3, 4, and finally 5. Now, the multiplication should be applied; 5 * 5 * 5 which is NOT 80. 25 * 5 = 125
12th Nov 2019, 3:10 AM
Toja
Toja - avatar
- 7
why: you look why: first var x=2; (++x)*(++x)*(++x)=80 (2*2)=4 (2*2)=4 4+4=8*2= 16 (2*2)=4. ☝ 16+16=32*2= 64+8+8=80; (2*2)=4. 👇 4+4=8*2= 16 this result 64+16=80 Thanks I for help you! I wait than you understand?
10th Aug 2018, 6:19 AM
Carlos Alberto Rodríguez Uribe
Carlos Alberto Rodríguez Uribe - avatar