0
Order of precedence
int main() int x = 5; int y = 2; int z = x - y * 2; Cout << z; Now it should be 1 but while I was solving an MCQ it gave me answer of -1. Is my mathematics that much weak?
2 Respostas
+ 4
RAJIV KUMAR KULIYA ,
let's check:-
the expression `x - y * 2` is evaluated according to the operator precedence rules.
The multiplication operator `*` has higher precedence than the subtraction operator `-`.
So, the expression is `x - (y * 2)`
`5-(2*2)`
First: `y * 2`= 2*2which gives 4.
Second: `x - (y * 2)` =5 - 4 which gives 1.
Therefore,
the answer should be 1, not -1.
Conclusion:-
Your mathematics skills are not weak!
It seems like there might be an error in the MCQ or in the way it was solved.
+ 2
No, you're mathematics aren't that weak. The MCQ is wrong. We're supposed to do the multiplication one first, then subtraction in the following code.