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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:24:38+00:00 2026-05-22T22:24:38+00:00

I have a general question – regarding testing of values, I have run into

  • 0

I have a general question – regarding testing of values, I have run into this issue multiple time and I end up with some unsightly long code to accomplish something that logically seems simple.

the issue – I have one or multiple values I want to test against other values….SUCH THAT my code ends up looking like this (conceptually)

IF (A > B 
AND A < C 
AND A < D
AND A < E)
THEN print "Yes"

LOGICALLY I can express this as IF A > b,c,d,e etc.

If I also need to check other values in that list – you can see that the conditional statement to test against could become significantly larger and messier than the code the condition is intended to yield…

there MUST be a way problematically OR mathematically to express this common instance.

I know each language has ‘syntax’ specific ways to to this but .. I’ll take suggestions and try em.

  • 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-22T22:24:38+00:00Added an answer on May 22, 2026 at 10:24 pm

    You have two questions here: 1) is there a cleaner way to write this code? and 2) is this computationally efficient?

    murgatroid99 and τεκ both have good answers to how to produce cleaner code. But both of these methods are actually (at least on the face of things, assuming the compiler doesn’t optimize under the hood) less computationally efficient.

    Think about it: the statement if a > max(b,c,d,e) tells the system to call max() — which adds to the stack, copies arguments, etc. The call will probably get inlined one way or another, so you’re not talking about a huge loss in performance, but there is some overhead. And the max() function will have to compare each of the numbers with each other number, taking O(n) time to complete.

    On the other hand, the statement you’ve originally written, IF (A > B AND A < C AND A < D AND A < E) will short-circuit out the first time it comes across a comparison that does not evaluate to true. So what you really want, if you clean up this code, is a function that does the same. Something like

    boolean smallerThanElements(int a, int[] elements) {
        for (int elt : elements) {
            if (a > elt) return false;
        }
        return true;
    }
    

    Now your list can grow to arbitrary size, but will still short-circuit out. You still have some overhead from the function call, but you’ve cut your expected performance to O(n/2) (the average of the best case O(1) and the worst case O(n)). And it simplifies your if statement down to if (a > b AND smallerThanElements(a, eltArray)).

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

Sidebar

Related Questions

I have general question regarding the use of pointers vs. references in this particular
I have a general question regarding Android's separation of layouts into layout.xml files: My
Ok another WPF question, well I guess this is just general .NET. I have
I've done some research into server push with javascript and have found the general
I have a general question about System.Threading.Threadpool when run in a webapplication on IIS.
I have a general question regarding UITableViews. here is my situation. I have a
All, I have a very general question regarding a future project. I need to
This is going probably be a general question regarding checksum used for error detection.
I have a general question regarding table indices on foreign keys in database modeling.
This is a general question, I have two pages, a main and a backgound

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.