how to check between two values in a loop

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.

36
48
72
96
144
288
500
1000
2500

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!
-Dman100-Software ConsultantAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
RobConnect With a Mentor Owner (Aidellio)Commented:
@-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.

**EDIT**

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.

Cheers,

Tagit
0
 
RobOwner (Aidellio)Commented:
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;                   
break;
              }
        }
}
// result holds the corresponding value you got to in the array

Open in new window

0
 
-Dman100-Software ConsultantAuthor Commented:
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!
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
-Dman100-Software ConsultantAuthor Commented:
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! :)
0
 
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
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....

25
144
36
48

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.
0
 
RobOwner (Aidellio)Commented:
@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 :)
0
 
RobOwner (Aidellio)Commented:
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-??)
0
 
-Dman100-Software ConsultantAuthor Commented:
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!!
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Thanks Dman, glad that we were able to get you a solution. And thanks, to tagit for sharing the points! :)
0
 
RobOwner (Aidellio)Commented:
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>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.