Question

I have an 8 bits variable and I need to detect if that variable contains another value different of 'S' or 'G'.

I try the following code

if(XType!='S' || XType!='G')
 {
   Reload();
 }

As I realize now, that didn't work. :(

I'm not allowed to use any library it must be resolved logically cause is very basic C compiler.

Was it helpful?

Solution

if(XType!='S' || XType!='G')

will always evaluate true. (if XType is 'G' then XType!='S' will be true; anything else will mean that XType!='G' will be true. Logical OR || will evaluate true if at least one of its inputs is true so your condition will always be true.)

Try using logical AND && instead. This only evaluates true if both inputs are true.

if(XType!='S' && XType!='G')

OTHER TIPS

It should be

if(XType!='S' && XType!='G')
 {
   Reload();
 }

because

if(XType!='S' || XType!='G')

returns always true

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top