Results 1 to 5 of 5

Thread: Or?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    103

    Or?

    if ((vtype != 1) || (vtype != 2) || (vtype != 3))
    System.out.println("Invalid input\n");

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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    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

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Haven't done this in a while. 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.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Originally posted by Dilenger4
    Right?? or am i staying up too late. [/B]
    absolutely right - and a better explanation than mine too

  5. #5

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width