mdh said:
One of the things I have yet to fully grasp ( of many things) is when
to use, for example
if (c !=b || c !=e || c !=f)....etc
Generally this is not what you want. Its almost always true. The only time
its false is if all the variables have the same value....
versus
if ( (c !=b && c !=e && c !=f)
This is more usually what you are after. It will only be true if "c" does
not match any of the others.
What is the real practical difference between these, and if this is not
a good example of the problem, could someone perhaps give a better
example and explanation.
Try some examples in a table:-
Say you want to check for a character NOT being a vowel:-
if ( a != 'A' && a != 'E' && a != 'I' && a != 'O' && a != 'U' ) { /* not a
vowel */ }
so if the variable a contains 'X' , substituting the results of the
compares we get a logical statement:-
if ( true && true && true && true && true )
which as you can see is true, which is what we would hope as 'X' is not a
vowel.
On the other hand if the variable a contains 'E' , substituting the results
of the compares we get a logical statement:-
if ( true && false && true && true && true )
as we have one false, the statement is false, which again is what we hoped
as 'E' is not not a vowel (double negative, E is a vowel)
Note that if we use the logical OR, as in your first example, then both the
above will be true, in both cases....
Thats OK.
Thats OK