I have this loop statement, which I'll express using C-like syntax (C, C++, Java, JavaScript, PHP, etc. all use similar syntax):
while (c != 'o' || c != 'x') {
c = getANewValue();
}
I want it to run until I get a 'o'
or 'x'
, but it never exits, even when c
is 'o'
or 'x'
. Why not?
I've also tried using if
:
if (c != 'o' || c != 'x') {
// Show an error saying it must be either 'o' or 'x'
}
but that also always shows the error message, even when c
is 'o'
or 'x'
. Why?
It must be if(c!='o' && c!='x')
instead of if(c!='o' || c!='x')
. If you use the or
operator the boolean expression will be always true.