Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6984777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:35:36+00:00 2026-05-27T18:35:36+00:00

An example using the Conditional Operator. void setSelected( bool selected ) { a =

  • 0

An example using the Conditional Operator.

void setSelected( bool selected )
{
    a = selected? SELECTED_VALUE_A: DEFAULT_VALUE_A;
    b = selected? SELECTED_VALUE_B: DEFAULT_VALUE_B;
    c = selected? SELECTED_VALUE_C: DEFAULT_VALUE_C;
}

An example using an if and temporaries.

void setSelected( bool selected )
{
    a = DEFAULT_VALUE_A;
    b = DEFAULT_VALUE_B;
    c = DEFAULT_VALUE_C;
    if ( selected )
    {
        a = SELECTED_VALUE_A;
        b = SELECTED_VALUE_B;
        c = SELECTED_VALUE_C;
    }
}

An example using functions

void setLook( int nA, float nB, std::string nC )
{
    a = nA;
    b = nB;
    c = nC;
}

void setSelected( bool selected )
{
    if ( selected )
        setLook( DEFAULT_VALUE_A, DEFAULT_VALUE_B, DEFAULT_VALUE_C );
    else
        setLook( SELECTED_VALUE_A, SELECTED_VALUE_B, SELECTED_VALUE_C );
}

An alternate example using functions. Still uses setLook

void setLookSelected()
{
    setLook( SELECTED_VALUE_A, SELECTED_VALUE_B, SELECTED_VALUE_C );
}
void setLookNormal()
{
    setLook( DEFAULT_VALUE_A, DEFAULT_VALUE_B, DEFAULT_VALUE_C );
}
void setSelectedAF( bool selected )
{
    if ( selected )
        setLookSelected();
    else
        setLookNormal();
}
//Or just call
setLookSelected();
setLookNormal();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:35:37+00:00Added an answer on May 27, 2026 at 6:35 pm

    Let’s simplify the requirement:

    Set variable a to c if b is true or d otherwise

    Using the conditional operator:

    a = b ? c : d;
    

    IMHO, as clear as it can get

    Using temporaries and if:

    a = c;
    if (b)
       a = d;
    

    Really ugly. You assign c to a, even if !b. Besides the awful readability, you encounter performance drop, as you have a double assignment. The optimizer could take care of that, but don’t count on it.

    Using functions:

    void setA(T x)
    {
       a = x;
    }
    
    //....
    
    if (b)
       setA(c);
    else
       setA(d);
    

    This could be useful if you have a lot of members that you want to set. Code maintenability will be improved. You’d only need to change in one place if you want different functionality. Not bad!

    Alternate functions:

    void setAC()
    {
       a = c;
    }
    void setAD()
    {
       a = d;
    }
    
    //...
    
    if (b)
       setAC();
    else
       setAD();
    

    Another option I’d advise against. What if you don’t want to check only for b in the future? What if the two variants aren’t exclusive, i.e. you want to combine ID_NODE_GLOW_SELECT with ID_NODE_COLOR_NORMAL for some other condition?

    Conclusion: I’d use the first variant for simple, small cases, and the functions with parameters for large classes, where you set a lot of members. I’d definetely stay away from options 2 & 4.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to express the cases using the conditional operator? For Example :
What is an example of a compound select statement using an AND operator, similar
I am using these rules and conditions in .htaccess to turn these http://www.example.com/index.php/about/history http://www.example.com/about/history/index.php
For example: using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; using System.Web.Script.Serialization;
Does anyone have an example using the table object in YUI library. More specifically,
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
Let's have an example: using (var someObject = new SomeObject()) { var someOtherObject =
I saw this C# using statement in a code example: using StringFormat=System.Drawing.StringFormat; What's that
I'm trying to do a simple example using jQuerys fadeTo method. It works as
I have looked at this example using php and GD to piecewise-render a spiral

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.