+ 12
Why when x=2 then (++x)*(++x)*(++x)=80 ?
Can anyone please explain ?
19 Respostas
+ 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..
+ 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
+ 9
x=2
(++Ć) =3
(++Ć) =4
so : (++Ć)*(++Ć) = 4*4
(++Ć) =5
so : (++Ć)*(++Ć)*(++Ć) = 4*4*5
+ 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;
+ 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.
+ 2
I tested in C language
+ 2
4x*4x*4x=34
+ 2
Interesting point. Never met this earlier š
+ 1
like that crazy variableš¤š¤
0
3*4*5=60
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.
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);
}
0
3*4*5=80
0
x=2
4*4*5=80
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
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
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
- 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
- 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?