Need help understanding.

This seems like a really simple question, but it bothers me when I don't understand why something does something, so... I need help with this.

void calculation(int a, int b) {
if (opChoice == 1) {
answer = a + b;
}
else if (opChoice == 2) {
answer = a - b;
}
else if (opChoice == 3) {
answer = a/b;
}
else if (opChoice == 4) {
answer = a*b;
}
else if (opChoice == 5) {
answer= pow(a,b);
}
}

Basically, when I have "if (opChoice == 1)" and I input '4' in the command prompt, it does multiplication- which is what it's supposed to do. But when I take out one "=" (so it would look like this >if (opChoice = 1)<), it makes it so if I enter a '4' or any other acceptable options (1-5), it ends up adding instead of Multiplying or the other operations.
Last edited on
opChoice == 1 compares for equality. It evaluates to true if the operands are equal; it does not modify opChoice.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators


opChoice = 1 performs an assignment. It assigns the value 1 to opChoice.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators



Topic archived. No new replies allowed.