Thread: Saving IF senteces

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    1

    Wink Saving IF senteces

    Hello everyone, I am following the Tips about C++ and I dont understand the tip, to avoid the use of the IF clausule, concretly the 2nd example:

    Code:
    Method 2: 
    ========= 
    
    int main()
    {
      int no;
      scanf( \"%d\", &no );
      no&1 ? printf(\"odd\"):printf(\"Even\");
    }
    The operator & is to access to memory isn't? Can you explain me how is use it here?

    Thanks you very much.

    David.

  2. #2
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    The & operator is a binary operation in this example, and it's a quicker modulus operation (remainder is how you determine even/odd - % operator).

    http://en.wikipedia.org/wiki/Bit_manipulation

    The ? : is a ternary operation, and it's short-form for if/else statement (true/false).

    http://en.wikipedia.org/wiki/Conditional_operator
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by dayerman View Post
    Code:
      no&1 ? printf(\"odd\"):printf(\"Even\");
    That doesn't compile. You don't put a backslash before a double-quote except where you're embedding that double-quote into the string.
    However don't use ?: as a whole is statement anyway. Just use it for part of an expression that could be this or that:
    Code:
      printf(no&1 ? "odd" : "Even");
    This avoids duplication.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I am following the Tips about C++ and I dont understand the tip, to avoid the use of the IF clausule,
    What kind of stupid "tips" are those? "How to write unreadable code"?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    I was going to ask the same thing. This is another case of 'clever tricks' without really giving a good understanding of how it works or in what cases it's really useful in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Saving and Loading Files
    By renanmzmendes in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2008, 08:46 AM
  2. New to C++ problem saving files
    By wesm in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2005, 02:00 PM
  3. Saving corrupts my database.
    By JamesM in forum C Programming
    Replies: 5
    Last Post: 09-29-2003, 03:53 AM
  4. File saving problem in DevC++ pls help ???
    By intruder in forum C Programming
    Replies: 3
    Last Post: 12-17-2002, 01:17 AM
  5. saving a game in console mode
    By Leeman_s in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2002, 06:30 PM