Like Tree3Likes
  • 1 Post By FillYourBrain
  • 1 Post By twomers
  • 1 Post By Dino

Usage of a Question Mark

This is a discussion on Usage of a Question Mark within the C++ Programming forums, part of the General Programming Boards category; I have no idea what the question mark does here. Code: cout << bit? "1": "0"; Why not simply write ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    197

    Usage of a Question Mark

    I have no idea what the question mark does here.
    Code:
    cout << bit? "1": "0";
    Why not simply write
    Code:
    cout << bit;
    Would that not be the same thing?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    if the bit is an int but can only be one or zero, then I suppose the output would be the same. But are you asking what the '?' does? That's the tertiary operator. It is basically an "if/else" on the condition to the left of it.
    abady likes this.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    It's called the ternary operator. It's not quite the same thing in this case as bit is more than likely a bool type, while "1" and "0" are strings (character if had been '1'). The effect is the same, however, visually. Programmers like to use it... makes us feel good, I guess.
    abady likes this.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It would make more sense if the example was:

    Code:
    cout << bit? "I am non-zero": "nothing here";
    Then you'd get either of the messages printed depending on whether bit is true (non-zero) or false (zero).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    This:
    Code:
    out << bit? "1": "0";
    for explanation purposes, would be better wrtten as:
    Code:
    out << bit ? "1" : "0";
    If "bit" is true, then the expression after the ? will be evaluated.
    If "bit" is false, then the expression after the : will be evaluated.
    abady likes this.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ternary operators often lead to sloppy looking code. So just be certain you make their use clear.

    Example:
    Code:
    int getRed(int color, int colorFormat)
    {
      return (colorFormat == RED8GREEN8BLUE8)?color & 0xFF:(colorFormat == RED5GREEN6BLUE5)?color & 0x5:(colorFormat == RED10GREEN12BLUE10)?color & 0x3FF:0;
    }
    Nasty... With some proper tabbing and multi-lining it may be acceptable, however typically for stuff like this more often you may want to consider using a switch case statement instead.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Isn't this exactly like the discussion about how you shouldn't build together big, long complex lines we had before?
    It's fine on its own. But if you do things like above, you're abusing it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    As a person who does like some of the more nasty options available in C++ syntax, I do like to issue conventional wisdom about how to not do as I do. Though I do crazy stuff that would drive you up the wall, Elysia, I do it in a very readable and understandable way.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    I wasn't necessarily implying that you were writing unreadable code...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. friend function and friend classes...usage question??
    By actionbasti in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2003, 09:53 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. I have a Question about memory usage for C.
    By bobthefish3 in forum C Programming
    Replies: 34
    Last Post: 12-24-2001, 03:37 PM

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