re: [Lookin' for advice] How should I use loop effectively? VIEW POST


Loops are useful and common, and in most cases are fine. Performance comes down to big-O notation most of the time, and a loop is O(n), which is perfectly acceptable performance in most cases.

What you want to avoid are nested loops, whenever possible, as those are usually O(n^2) or worse. There are, again, cases where a nested loop has a place, but use them sparingly and with great caution.

For any given problem, however, there may be more efficient ways of doing things. You'll need to research means of optimizing that particular solution. It helps to make yourself familiar with common design patterns, data structures, and algorithms, so you can better decide when to use a loop, and when to use something else.

All in all, it's on a case-by-case basis. There's no one-size-fits-all answer here.

code of conduct - report abuse