Thread: Explanation of code snippet

  1. #1
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55

    Explanation of code snippet

    Hello everyone,

    could somebody tell me what this snippet of code does?

    Code:
        a = cmd & MISSILE_LAUNCHER_CMD_LEFT ? 1 : 0;
        b = cmd & MISSILE_LAUNCHER_CMD_RIGHT ? 1 : 0;
        c = cmd & MISSILE_LAUNCHER_CMD_UP ? 1 : 0;
        d = cmd & MISSILE_LAUNCHER_CMD_DOWN ? 1 : 0;
        e = cmd & MISSILE_LAUNCHER_CMD_FIRE ? 1 : 0;
    I think this is a sort of if statement but even then i dont know how it works.

    Thanks for your help,

    Jelte.
    The Programming Dutchman

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    19
    They are conditional expressions. If the expression before the ? is true, then the value between the ? and : is assigned, if the expression is false, then the value after the : is assigned.

    Code:
    if (a>b)
    {
           c=10;
     }
     else
    {
           c=20;
     }
    Can be replaced with:

    Code:
    (a>b) ? (c=10) : (c=20);
    Which in turn can be written as:

    Code:
    c = (a>b) ? 10 : 20;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why is this code snippet refusing to work ?
    By saraghav in forum C Programming
    Replies: 4
    Last Post: 06-01-2006, 03:25 AM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  3. Code explanation..
    By snapshooter in forum C++ Programming
    Replies: 5
    Last Post: 11-16-2004, 08:22 PM
  4. snippet of for loop code problem
    By rayrayj52 in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2004, 02:36 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM