Solved

how to check between two values in a loop

Posted on 2013-11-12
11
412 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-
  • 5
  • 3
  • 2
11 Comments
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
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
 
LVL 35

Assisted Solution

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

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 42

Expert Comment

by:Rob Jurd, EE MVE
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE 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.

**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 42

Expert Comment

by:Rob Jurd, EE MVE
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 35

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 42

Expert Comment

by:Rob Jurd, EE MVE
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now