![Alok mishra](https://www.crazyengineers.com/img/avatar.jpg)
Member • Oct 18, 2013
Member • Oct 18, 2013
Member • Oct 19, 2013
It depends on the no of times the loop runs, and the amount of nesting,(of course not considering hardware related factors) and if both these parameters are large, it really affects the execution time badly.Alok mishraAs i was asked to write a program to generate all possible anagram for a given word , the logic that has struck my mind at first was using nested loops but i didnt care about execution time and complexity . So i want to be clear if nested loops affect programs' execution badly .
Member • Oct 19, 2013
for(int i = 0; i < l.length; i++) { for(int j = 0; j <= i; j++) { //your code } } and : for(int i = 0; i < l.length; i++) { for(int j = 0; j < l.length; j++) { //your code } }
Member • Oct 21, 2013
Member • Oct 22, 2013
the first one !! but why did you ask this ?ianoopYes, Loops add complexity and execution time very badly specially nested loops. They should be avoided as much as possible, using collections (Data structure containers).
Here complexity is Cyclomatic complexity, which is in one line - Number of independent paths in programs.
Loops/nested doesn't much add in complexity but performance is degraded to worse. while nested looping you must consider what data you want and when to break the loop.
See the following code, do you find any execution difference between them? which one you will go?
Member • Oct 22, 2013
Member • Oct 22, 2013
the first one !!ianoopYes, Loops add complexity and execution time very badly specially nested loops. They should be avoided as much as possible, using collections (Data structure containers).
if i say i am using 8 nested loops in a program , does it sound impractical (not correct word) or what would you suggest about that ?ianoopYes, 1st one is more efficient. I asked because whenever looping , be precise how many iteration it gonna take.
Member • Oct 22, 2013
Member • Oct 26, 2013