//operator precedence exercise (use the debugger)
int main() {
float x = 3;
float a = x*x / 4 + 1;
float b = (x*x) / 4 + 1; //these three are all the same
float c = ((x*x) / 4) + 1; //but I think this is clearer
a = 2.0f;
b = 1.0f;
c = 3.0f;
float result1 = a / b * c; //ambiguous (in mathematics)
float result2 = (a / b)*c; //this is how C++ evaluates it
float result3 = a / (b*c); //we can change the order using parenthesis
return 0;
}
Comments
0 B
|0 👍
/0 👎
0 B
|👍
/👎