how to check between two values in a loop

Posted on 2013-11-12
Last Modified: 2016-02-17
I'm using the Salesforce APEX language, but I have a basic logic question using a loop structure.   The loop syntax is similar to Java or C#.

I need to iterate over a series of values and check if the value I'm comparing is greater than the value I'm looping over and find the next highest value in the loop that the value is not greater than.

For example, let's say the value I need to check is 37 and the here is the list of values I'm looping over.  Please note, the values can be in a random order and not sorted in ascending or descending order.


I want to stop on the iteration of 48 because 37 is greater than the 36 and less 48.

If the number  was 100, I would want to stop on 144 because 100 is greater than 96 and less than 144.

How can I write a loop to check between values in a list?

Thanks for any help!
Question by:-Dman100-
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
LVL 43

Expert Comment

ID: 39643496
c# code (untested) below.  Kind of what you're after?

int tocheck = 37;
int result = 0;

int[] myarray = new int[] { 36,48,72,96,144,288,500,1000,2500 };
        foreach (int element in myarray) {
             if (tocheck > element) {
result = element;                   
// result holds the corresponding value you got to in the array

Open in new window


Author Comment

ID: 39643623
Hi tagit!  How are you?  I hope all is well!

I appreciate your reply to my post.  Here is a better explanation of the problem I'm trying to resolve.  

I'm looping over a series of records and each record represents a quantity for a specific price.  For example, if you have a quantity of 36 you get a price of $10.  If the quantity is 48 you get a price of $8.75, etc.  The higher the quantity, the lower the price.  So, I'm checking a quantity ordered and comparing it against this list of records.  So, if my quantity is 37, then I need to find the record that is less than 37, which is 36 and the next highest value which is 48.  Then I would know my price should be for the record with a quantity of 36 because the next highest record is 48 with a lower price point.  I don't want to give a lower price point until the next highest quantity is reached.

As I'm describing this, I think I just figured this out.  I do simply check if it is greater than or equal because as soon as it meets that condition I have the right record.  I'm testing it now!

Author Comment

ID: 39643627
I think that resolved it.  I had to talk it thru!! :)  I'm getting the correct results now!  I hope all is going well in your neck of the world! It is good to hear from you! :)
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

LVL 35

Assisted Solution

mccarl earned 250 total points
ID: 39643648
The extended description is better at explaining what you are after. And in light of this, and your statement in the original question that the series of records could be in ANY order, means that the simple loop solution won't be enough. Take this example... what would happen if the value to check for is 37 and the records get returned in this order....


To solve this, you would either need to ensure that the records are sorted, either in whatever queries the data or by coding an explicit sort on the list. Otherwise, you would need to iterate over the whole list (ie. not just "break;" when you find a greater value) and track the "max" quantity so far that is also less than or equal to the value to check for.

By the way, I think @tagit's code is not quite right (just thinking it through in my head, my apologies if I'm missing something). I think that the "tocheck > element" expression should have been "tocheck < element". Regardless, in your clarification, you say that you want the element that is less than the value to check, whereas your original post indicated you wanted the one greater than the value to check, so you would have to modify this code anyway, even if the elements were sorted ascendingly.
LVL 43

Expert Comment

ID: 39643670
@mccarl - you're absolutely right *grins*.  My thought process changed a few times in creating the loop so I've caught myself out.  The original post did in fact indicate that it the element is greater than the search term, to then stop.  I'm glad I put untested :)
LVL 43

Accepted Solution

Rob earned 250 total points
ID: 39643674
@-Dman100-, good to hear from you too!

I really think that @mccarl has a good point about the sorting of the numbers given that it's a break discount structure you're trying to implement.


As for the condition, I believe it is correct given it should break when the element is  greater than the search term, returning the element (unless I've misunderstood).

What I'm getting at is it should be a points split.

Let me know and I'll unaccept the question for you, if you agree.


LVL 43

Expert Comment

ID: 39643686
Finally my brain has kicked in and the condition is incorrect and should be less than (<)

if (tocheck < element)

So regardless I think a points split and another comment selected as the solution (maybe -Dman1000-??)

Author Comment

ID: 39646075
Hey Guys!  Thanks for the further clarification!  I'll be happy to split the points between both of you.  I'm still testing today.  So far, the results look good, but I am chasing down a bug in this and if relates to the looping structure, I'll post back my findings with clarification.

Thanks again to the both of you for the help!!
LVL 35

Expert Comment

ID: 39649908
Thanks Dman, glad that we were able to get you a solution. And thanks, to tagit for sharing the points! :)
LVL 43

Expert Comment

ID: 39650306
Yeah we got there in the end!  No worries about the points.

<rant>I think there's too much competition regarding the points and we'll only build a better site working together than against each other :)</rant>

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java pass by reference 3 74
Can Selenium do Load Testing? 2 59 repeater server posting 5 38
C#line chart with data on Y and time on X-axis 3 32
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question