CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 1999
    Location
    Spain
    Posts
    335

    C++ Question ? (bCondition) ? TRUE : FALSE

    Hi,

    Can anybody exaplain me, what is this:

    (bCondition) ? TRUE : FALSE

    I always see it every where, but I don't know very well for what thing is this ( is better than an "If" command) ?

    Thanks, Bye !
    Braulio


  2. #2
    Join Date
    Jul 1999
    Location
    Romania - Iasi
    Posts
    558

    Re: C++ Question ? (bCondition) ? TRUE : FALSE

    this is ?: operator and work in this way:

    atype a, s, t;

    a = (bool_cond) ? s:t;//if bool_cond ==true a= s, else a = t




    Let me know if this help u
    Regards,
    Ovidiu


  3. #3
    Join Date
    May 1999
    Location
    Spain
    Posts
    335

    Is better than if ?

    Thanks,

    One question more, Is better than a normal "If" ?, Or is a good think to know to understand code from other people .... :-( ?

    Thanks, Bye !
    Braulio


  4. #4
    Guest

    Re: C++ Question ? (bCondition) ? TRUE : FALSE

    Its the tertiary or trinary operator. A specialized version of the more common if-else condition
    u can refer to any book on C or even MSDN to get more information.

    (bCondition) ? TRUE : FALSE

    This means if the condition is TRUE then use the first value (TRUE) and if the condition returns FALSE then use the second value (FALSE)

    instead of TRUE/FALSE one can also have other data types or calculations
    General syntax is
    test ? statement1 : statement2
    this means
    if (test)
    statement1
    else
    statement2



  5. #5
    Join Date
    Jul 1999
    Location
    Romania - Iasi
    Posts
    558

    Re: Is better than if ?

    The ?: operator is a shortcut for an if...else statement. It is typically used as part of a larger expression where an if...else statement would be awkward. For example:

    CString now;
    int h;
    //read h
    var greeting = "Good" + (h != 0) ? " evening." : " day.");




    The example creates a string containing "Good evening." if h != 0. The equivalent code using an if...else statement would look as follows:

    CString now = "Good";
    int h;
    //read h
    if (h!=0)
    greeting += " evening.";
    else
    greeting += " day.";





    Hope this help u
    Regards,
    Ovidiu



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured