whats wrong with this if statement?

Status
Not open for further replies.

psyb0rg

Baseband Member
Messages
39
if ( a !=12)||(a) !=5 ||(a) !=7
||(a) !=10||(a) !=12||(a) !=15||(a) !=17
||(a) !=20||(a) !=22||(a) !=25||(a) !=28
||(a) !=30||(a) !=33||(a) !=35||(a) !=36
||(a) !=38||(a) !=40)



Isn't working. i tried in VC++ 6:angry:
 
you have closed the if bracket too soon, try starting it with,
if(( a!=12.............(a)...(a)..........(a)!=40)
make sure you have closed all your opened brackets, and that whole expression is inside the if( )
 
oh sorry. that was a little mis type here. the code wont work even if i fix the bracket thing

if ((a) !=12||(a) !=5 ||(a) !=7
||(a) !=10||(a) !=12||(a) !=15||(a) !=17
||(a) !=20||(a) !=22||(a) !=25||(a) !=28
||(a) !=30||(a) !=33||(a) !=35||(a) !=36
||(a) !=38||(a) !=40)
 
I would try wrapping the quotes differently. It should work fine the way you have it, but this makes it easier to read without having to ask yourself if the != operator has a higher precedence than the || operatior. I also removed the dublicate a != 12.

Code:
if ((a !=5) ||(a !=7)||(a !=10)||(a != 12)||(a !=15)
||(a !=17) ||(a !=20)||(a !=22)||(a !=25)||(a !=28)
||(a !=30)||(a !=33)||(a !=35)||(a !=36)||(a !=38)||(a !=40))

I'm assuming you want to check that a doesn't euqal any of the numbers in this set {5, 7, 10, 12, 15, 17, 20, 22, 25, 28, 30, 33, 35, 36, 38, 40}
 
yup, i think this will do it, keepin the single conditions in seperate brackets will help.
 
Do you want to enter the statement only if a is not equal to one of those values? If a is supposed to not equal all of them then replace || with &&.
 
What gab00n is saying is that if a doesn't equal one of the numbers, even if it equals another, the if will return false. If you want to check it doesn't equal any of them replace the ||s with &&s. If that's not what you're trying to do I suggest following furtivefelon's advice.
 
Thanks for the help. i think the && would work. but i changed the code form != to ==, so that it checks for values which i DO need. thanks everyone.
 
Status
Not open for further replies.
Back
Top Bottom