0

I've recently realized the power of object oriented programming, but don't know how to get around using a nested loops (I've read it is bad programming practice) that takes to much time to execute and is inefficient. I just want to read through objects say Apples, Oranges, and Tomatos. Each object would have a vector of prices for them. My main class would simply loop through all the vector prices and try to find the combinations that are less than say $1,000. This would require three for loops. The real program which I haven't yet wrote would have 10 nested loops.

3
  • One alternative would be to use recursive function calls. I find that this can be pretty helpful if you have an arbitrary number of nested looks that are pretty much doing the same thing (i.e., looping through several levels of a directory structure). Commented Aug 9, 2021 at 16:12
  • I appreciate your help and advice. I'll have to go back through some of my programming books because its been a long time since I used recursive functions. My computer uses the 5960x processor which I think is still very powerful. I think my program would take months to execute with my computer using 10 nested for loops; that's including using multithreading. Commented Aug 9, 2021 at 16:35
  • Loop over an array quantities indexed by product. Not really object oriented. Its difficulty is an intelligent incrementing,. Which puzzle you might try first manually, with pen and paper. Commented Aug 9, 2021 at 16:56

1 Answer 1

1

Not always nested loops are a bad programming practice. As an example consider a game with a board of 10x10 cells. Each cell store an information. If you need to count all the cells having a particular value doing a nested loop can be necessary and it is not a bad practice and it is not inefficient, because you need a way to check all the 100 existing cells (10x10 that can be looped as 10 rows of 10 cells, with a 2 level nested loops).

In your case if you have 3 lists of prices and you would like to find all possible combinations of prices so that you have no more than $1,000 you need to do the nested loops. It is possible to limit the number of checks for example sorting the prices, but the code would be a lot more complicated to be read. And if you don't have to do with special performance issues having a clean code is better than having a more performant code. So don't spend your time trying to have a more performant code before knowing that you need a more performant code. Remember that 80% of execution time is spent in less than 20% of code.

Consider that the 3 nested loops are possible only if you need to find the combinations with 1 apple, 1 orange, and 1 tomato. If you need also other combinations (like 4 apples and 2 oranges and 0 tomatoes) the algorithm will complicate a lot and can not be handled with just 3 nested loops.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.