PDA

Click to See Complete Forum and Search --> : Or?


quipy
May 1st, 2003, 06:56 AM
if ((vtype != 1) || (vtype != 2) || (vtype != 3))
System.out.println("Invalid input\n");

i input 3, and it prints out the message... what gives?

si_the_geek
May 1st, 2003, 07:33 AM
or course it prints out the message!!

3 <> 1, so the first part will let it through! (whatever value you put in, it will ALWAYS show the message)


ie:

if ((vtype != 1) || (vtype != 2) || (vtype != 3))

which becomes: if (TRUE) or (TRUE) or (FALSE)

which becomes: if (TRUE)



try use AND instead ;)

Dillinger4
May 2nd, 2003, 01:45 AM
Haven't done this in a while. :D Yeah looks like what ever value is used the message will always be printed. Since you are using the short circuit || operator you would need F F F

First True would skip all other evaluations.
T || T || T == True

Second True would skip the third
F || T || T == True

F || F || T == True

You would need F || F || F == False which seems impossible to get.

In fact i cant ever see the third operand ever gettting evaluated.

If the input is 1, 1 != 1 would be false so the next evaluation would be 1 != 2 and that would be true. Clearly not what you want.

If the input is any number != to 1 then the expression would evaluate to true. Again clearly not what you want.

Right?? or am i staying up too late. :confused: :D

si_the_geek
May 2nd, 2003, 04:46 AM
Originally posted by Dilenger4
Right?? or am i staying up too late. :confused: :D [/B]
absolutely right - and a better explanation than mine too :eek:

Dillinger4
May 2nd, 2003, 01:16 PM
Conditional operators give me a headache. :p