Although with several conditions

Can someone explain why the expression (I'm learning C), for example,

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

causes problems.

In particular, I have this code:

while (ch != '\n' || ch != '\t' || ch != ' ') { ... } 
+3
source share
4 answers

UPDATE . According to your other comment, your expression is wrong - it has nothing to do with "while" with several conditions.

ch != '\n' || ch != ' ' ALWAYS true, regardless of what these characters are.

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

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

The right way: ch != '\n' && ch != ' ' ...

OLD answer:

( ).

, (, b c , b!=1 ).

while - , .

|| && C, , , , .

+7

. , , , && ||

+4

, . De Morgan Laws. . :

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

, , , , (ch == '\ r' && ch == '\n') .

, :

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

:

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

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

+3
source

What problem do you mean? This usually makes sense like:

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

or

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

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

0
source

Source: https://habr.com/ru/post/1743652/


All Articles