?
Solved

how to check between two values in a loop

Posted on 2013-11-12
11
Medium Priority
?
439 Views
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.

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!
0
Comment
Question by:-Dman100-
[X]
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
11 Comments
 
LVL 43

Expert Comment

by:Rob
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;                   
break;
              }
        }
}
// result holds the corresponding value you got to in the array

Open in new window

0
 

Author Comment

by:-Dman100-
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!
0
 

Author Comment

by:-Dman100-
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! :)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 36

Assisted Solution

by:mccarl
mccarl earned 1000 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....

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
 
LVL 43

Expert Comment

by:Rob
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 :)
0
 
LVL 43

Accepted Solution

by:
Rob earned 1000 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.

**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
 
LVL 43

Expert Comment

by:Rob
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-??)
0
 

Author Comment

by:-Dman100-
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!!
0
 
LVL 36

Expert Comment

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

Expert Comment

by:Rob
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>
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

719 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