Thread: logical operator OR not working?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    13

    logical operator OR not working?

    hi, i'm trying to get a loop to end on either of two conditions.

    Code:
    while ((numCorrect < theLength) || (wrongs < 9)) {
    
    // do stuff
    
    }
    both work fine on their own, it's just that when i put them together, the OR operator works like an AND operator; it doesn't stop the loop until BOTH conditions are true!! why is this happening?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you use && then? Say it:

    "While the number correct is less than the length, and wrongs is less than nine, do stuff..."


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    With AND, both things must be true for the equation to stay true. The loop would stop if one term were false.

    With OR, if one thing is true then the whole equation is true, so both sides must be false for the loop to stop.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    good point... lol! I was thinking backwards, so i got them mixed up, coz i was thinking if either of them were false then stop... the "either" made me use OR instead of AND. thanks heaps guys.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It really does help to say it out loud. Just read it in words as you see it, and it's easy to figure that stuff out (most of the time).


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  2. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  3. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  4. Logical Operator not working?
    By xshapirox in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2004, 05:21 AM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM