Thread: Saving IF senteces

  1. #1
    Registered User
    Join Date
    Mar 2009

    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:

    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.


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

    The ? : is a ternary operation, and it's short-form for if/else statement (true/false).
    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
    New Zealand
    Quote Originally Posted by dayerman View Post
      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:
      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
    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!

    "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
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Charles Town, WV
    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.
    - Leeor

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