E_Blue
E_Blue

Reputation: 1151

C syntax - I need to make a simple if branch to detect a specific condition

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.

Upvotes: 0

Views: 59

Answers (2)

MOHAMED
MOHAMED

Reputation: 43548

It should be

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

because

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

returns always true

Upvotes: 1

simonc
simonc

Reputation: 42185

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')

Upvotes: 5

Related Questions