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 9008719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:00:35+00:00 2026-06-16T02:00:35+00:00

In C++, the usual way of determining if some value, x, is between two

  • 0

In C++, the usual way of determining if some value, x, is between two limits is to:

  //This is (A)
double x = 0.0d;
double lower = -1.0d;
double upper = +1.0d;

if(x > lower && x < upper){
  // Do some stuff
}

But today I discovered by accident that I can do this:

  // This is (B)
double x = 0.0d;
double lower = -1.0d;
double upper = +1.0d;

if(lower < x < upper){
  // Do some stuff
}

It seems to work fine, but I’ve never heard of this being done before, with “lower < x < upper”. Does this produce executable code as you would expect it to? IE, is (A) equivalent to (B)?

I think a lot of people won’t know about this, and I suspect that might be because the compiler interprets (A) differently to (B). It this right?

  • 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-16T02:00:36+00:00Added an answer on June 16, 2026 at 2:00 am

    No, A and B are not equivalent, you cannot do this.

    Or, obviously you can (as you discovered) but you’re not doing what you think you’re doing.

    You’re evaluating (lower < x) < upper, i.e. the value of lower < x (which is false or true, but which convert to int for the comparison) is compared to upper.

    See this table of operator precedence for more information.

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

Sidebar

Related Questions

What is the usual/clearest way to write this in Python? value, _ = func_returning_a_tuple()
The usual way to assign color box functionality on a link is like this:
This is the usual way for declare a Java array: int[] arr = new
This is my usual way to debug javascript. Include alert(0); to break the flow
The usual way to read a file in C++ is this one: std::ifstream file(file.txt,
I know the usual way of finding n-1 factorial iteratively and then checking. But
I'm using asp:requiredfieldvalidator on my page. The usual way of turning this off for
The usual way I do form handling is like this: contact = Contact( name=form.cleaned_data['name'],
The usual way to show\hide block in html is switching it's display property. But
I'm using the nested_form_for in the usual way but I want to add dynamic

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.