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

  • SEARCH
  • Home
  • 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 8882499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:35:42+00:00 2026-06-14T20:35:42+00:00

I have always wondered about this. Let’s say we have a variable, string weight,

  • 0

I have always wondered about this. Let’s say we have a variable, string weight, and an input variable, int mode, which can be 1 or 0.

Is there a clear benefit to using:

weight = (mode == 1) ? "mode:1" : "mode:0";

over

if(mode == 1)
    weight = "mode:1";
else
    weight = "mode:0";

beyond code readability? Are speeds at all affected, is this handled differently by the compiler (such as the ability of certain switch statements to be converted to jump tables)?

  • 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-06-14T20:35:43+00:00Added an answer on June 14, 2026 at 8:35 pm

    The key difference between the conditional operator and an if/else block is that the conditional operator is an expression, rather than a statement. Thus, there are few places you can use the conditional operator where you can’t use an if/else. For example, initialization of constant objects, like so:

    const double biasFactor = (x < 5) ? 2.5 : 6.432;
    

    If you used if/else in this case, biasFactor would have to be non-const.

    Additonally, constructor initializer lists call for expressions rather than statements as well:

    X::X()
      : myData(x > 5 ? 0xCAFEBABE : OxDEADBEEF)
    {
    }
    

    In this case, myData may not have any assignment operator or non-const member functions defined–its constructor may be the only way to pass any parameters to it.

    Also, note that any expression can be turned into a statement by adding a semicolon at the end–the reverse is not true.

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

Sidebar

Related Questions

I have always wondered about this. So many application setups have a zip file
I've always wondered about this. I have an @property called hitCounter and this works
I have always wondered about this but quite never got to understand why. We
I have always wondered how I should do this. Lets say I have a
I've always wondered about this ... and thankfully, now have a good environment of
This is something that I have always wondered about, but never bothered to profile.
This is something I have always wondered about, and looked up a few times
I have always wondered what's the best way to achieve this task. In most
I've always wondered, lets say you have two asp.net websites running in the same
This is something I've always wondered about. I understand that horizontal scaling is about

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.