Confused with specific && and || logic

This is a discussion on Confused with specific && and || logic within the Windows Programming forums, part of the Platform Specific Boards category; I'm not understanding why the following statements are working the way they are: Code: while ( GetAsyncKeyState(VK_F4) || GetAsyncKeyState(VK_F6)) Here, ...

  1. #1
    Loom Weaver the dead tree's Avatar
    Join Date
    Apr 2004
    Posts
    140

    Confused with specific && and || logic

    I'm not understanding why the following statements are working the way they are:


    Code:
    while ( GetAsyncKeyState(VK_F4) || GetAsyncKeyState(VK_F6))
    Here, while F4 or F6 are being held, the code will run. So far so good.

    Code:
    while ( !GetAsyncKeyState(VK_F4) || !GetAsyncKeyState(VK_F6))
    But here, the way I see it, the code should run until F4 or F6 are pressed, but it will only stop after I hold each of them a couple of times(?).
    And if I change the || for &&, only one button is needed to stop the loop, while in this case both should be pressed.
    What am I missing?
    I'm sorry if this is something obvious.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    || gives true if at least 1 of its arguments are true. !GetAsyncState() gives true if the button is NOT held (notice the !). The entire while loop loops while F4 is not held, F6 is not held or if both are not held.

    If I understand your request correctly, you want:
    Code:
    while(!GetAsync(F4) && !GetAsync(F6))
    That will run until F4 or F6 is pressed.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Loom Weaver the dead tree's Avatar
    Join Date
    Apr 2004
    Posts
    140
    Well, that was obvious

    When I asked my friend who knows a bit of programming he got confused too for some reason. I should just have looked better at the code.
    Thanks Magos

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  2. process killer on a loop
    By Anddos in forum Windows Programming
    Replies: 8
    Last Post: 01-11-2006, 01:50 AM
  3. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  4. Massive Function Problem
    By Marc Sharp in forum C Programming
    Replies: 10
    Last Post: 11-19-2003, 08:49 PM
  5. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 12:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21