tags:

views:

143

answers:

5

Can somebody please explain why an expression (I study C) like

while(a!=1 || b!=1 || c!=1)

causes problems.

In particular I have this specific code:

while (ch != '\n' || ch != '\t' || ch != ' ') { ... } 
+4  A: 

That's perfectly correct. But it depends on what you want, perhaps you mean && instead of ||

Computer Guru
I have expression like this while(ch!='\n' || ch!='\t' || ch!=' ') ...I need stop reading chars if I meet tab or white space or enter
lego69
but I can't exit from the loop, so what can be the problem?
lego69
sepp2k
I think you need `!(ch=='\n' || ch=='\t' || ch==' ')`
Martin Smith
lego69: exactly what I mean.You need to be NOT a \n AND not a \t AND not a <space>
Computer Guru
+3  A: 

You should be careful when using not in boolean expressions that you don't get the AND and OR mixed up. Read about De Morgan's Laws. Often it is easier to read with only one negative. Applying De Morgan's Law to your expression gives this:

while (!(ch=='\r' && ch=='\n' && ch==' '))

If you had written it in this form you will hopefully immediate notice that (ch == '\r' && ch == '\n') can never be true.

The solution is to change this:

while (ch != '\n' || ch != '\t' || ch != ' ')

into this:

while (!(ch == '\n' || ch == '\t' || ch == ' '))

You can read it as "While we don't have \n or \t or space, do this...". Note that "while not" is similar to "until" in English (and some programming languages), so you can also read it as "Until we have \n or \t or space, do this".

Mark Byers
@Mark - +1 (in part because you edited the question to imorove it :) )
DVK
+8  A: 

UPDATE: As per your other comment, your expression is wrong - it has nothing to do with "while" having multiple conditions.

ch != '\n' || ch != ' ' is ALWAYS true, no matter what the characters is.

If the character is NOT a space, the second condition is true so the OR is true.

If the character is a space, the first condition is true (since space is not newline) and the OR is true.

The correct way is ch != '\n' && ch != ' ' ...

OLD answer:

Under normal circumstances there's no problem whatsoever with the expression above (assuming you wanted to do just that).

The only issue with yours is that it can sometimes be less than optimal (e.g. if b and c never change throughout the loop, in which case you need to cache the value of b!=1 in a variable).

while with multiple conditions may have a problem in one case - if those multiple conditions actually have intended side effects.

That is due to lazy evaluation of || and && in C, so that if the first expression is true, the rest will NOT be evaluated and thus their side effects will not happen.

DVK
+2  A: 

What problem are you referring to? It is usually make sense as :

while(a == 1 || b == 1 || c == 1)  

Or

while(a != 1 && b != 1 && c != 1)

But there is no problem with the expression you have if that is what you want.

fastcodejava
A: 

thanks a lot for all who answered, I understood my mistake...

lego69
Accept the best answer (click the hollow checkmark) if that helped.
KennyTM