Why does it break out of the for loop if I write a number that isn't equal to either 1, 2 or 3 (for example 4)? I understand why it breaks out of the loop when I write 1 2 3, but with other numbers? I don't get it.
1 2 3 4 5 6 7 8 9 10
int op;
int exitloop = 1;
for (int i = 0; exitloop == 1; i++) {
cout << "Something" << endl;
cin >> op;
if (op == 1, 2, 3) {
exitloop = 0;
}
}
It evaluates each thing from left to right, then returns the right-most value.
So in your case, it's evaluating op == 1, then evaluating the expression 2 (note: not op == 2), then evaluating the expression 3, and then it returns 3, which evaluates to true for an if condition (any non-zero value evaluates to bool true).
If you meant to say ||, then use || (logical OR). Each statement must also be independent.
1 2 3 4
if (op == 1 || op == 2 || op == 3)
{
exitLoop = 0;
}
Also, the naming of "exitLoop" seems to be the opposite of what its purpose is in your code.