+ 3

Is it good to use more than 10 nested loop?

23rd Feb 2017, 11:50 AM
hargun das
hargun das - avatar
36 Answers
+ 14
You can! But, time complexity increases with level of nesting. So, try to avoid high levels of nesting.
23rd Feb 2017, 12:55 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 5
hargun is asking about nested loops - presumably meaning along the lines of (psuedo code): for i > 10 for j > 10 for k > 20 etc c++ isnt one of my languages (atm anyway) but usually switch replaces ifs why are people talking about switch???
2nd Mar 2017, 8:12 PM
Phil
Phil - avatar
+ 4
I can't be sure if it's necessarily bad, but it's definitely not good. You should use switch instead.
23rd Feb 2017, 12:35 PM
Alireza M
Alireza M - avatar
+ 4
It is possible but not good? I forbid more than three because - nobody will understand three days later - it is normally an indicator for bad structured programs. - memory problems (well those were the days) the solution are indeed modules. you might still have hidden deep nested loops finally but... well nothing is perfect
28th Feb 2017, 10:29 AM
Oma Falk
Oma Falk - avatar
+ 4
A recursive function would be much better in terms of time execution, think about it, let's say with two loops it takes O^2 time. For 10 loops it's O^10. For example, let's say it takes you 2 seconds to compute with a single loop. With 10 loops it takes 2^10 seconds (more precisely 1024 seconds).
2nd Mar 2017, 7:18 AM
Teo
Teo - avatar
+ 4
It is possible but it may confuse you and may cause bug if logic isn't right
8th Mar 2017, 5:21 PM
keivan Ipchi
keivan Ipchi - avatar
+ 3
I'm sorry but I disagree to those people who say a switch statement is equivalent to 10 loops....Both have different functionality.But making 10 loops increasing runtime speed and I don't see where u can make use of it
25th Feb 2017, 5:23 PM
Prerit Kumar
Prerit Kumar - avatar
+ 3
it will definitely slow down the execution speed of your program
28th Feb 2017, 1:28 PM
Yash Raghava
Yash Raghava - avatar
+ 3
Try write Your problem on a paper sheet and reduce it matematically. Try to look on every loop like on dimention. Maybe You need read something about permutations also?
5th Mar 2017, 8:29 AM
Michał Bujakowski
Michał Bujakowski - avatar
+ 2
NOOOOO, hope that helps.
8th Mar 2017, 12:42 AM
Elfren Authorlee
Elfren Authorlee - avatar
+ 1
try to use switch, conditional statement,and recursion to avoid complexity
25th Feb 2017, 7:40 AM
Neeraj Roy
Neeraj Roy - avatar
+ 1
You can .... but it is not good because it will be as a stuff for compiler or we can say for CPU.👀
28th Feb 2017, 12:14 PM
Shiv Mishra🎸
Shiv Mishra🎸 - avatar
+ 1
No. Complexity will be n^10. Try to achieve with recursion why you required 10 nested loops?
2nd Mar 2017, 2:57 AM
Mohd Shahid Ameen
Mohd Shahid Ameen - avatar
+ 1
Don't use anested loop too much as it increases complexity . You won't be able to understand your own program. Use switch with for loop . I think it would be easy and help you.
6th Mar 2017, 4:21 PM
Shubhendra Nath Singh
+ 1
Actually it is not advisable to use more than 10 nested loops because of the following reasons: ->It leads to exponential time which makes your code unusable ->It leads to cyclomatic complexity which means it is very hard to maintain this code. ->It leads to polynomial complexity that means it takes many iterations which may be or may be not required at all. It's ok to use 3 nested loops because it's easy and popular also and also can be considered but increasing the nesting is not a good idea. You can go for better options like hash tables which has better time complexity. You can also go for switch statements,conditionals or recursions as well.
9th Mar 2017, 7:42 AM
Sunny Saurabh
Sunny Saurabh - avatar
+ 1
it increase the program complexity and decrease performance also
28th Dec 2017, 4:19 PM
Ranjit Singh
Ranjit Singh - avatar
0
I do agree with the fellows who advised to use Switch case. It makes more sense to use a switch once you start having more than 2 conditions to check. A switch also allows better human readability (As opposed to having a lot of loops).
27th Feb 2017, 11:34 PM
Sadrak Souffrant
Sadrak Souffrant - avatar
0
Where do you use 10 dimensional array?
28th Feb 2017, 11:56 AM
Zoltán Bolodár
Zoltán Bolodár - avatar
0
It is not a good programming practice. I will not recommend more than three. Try functional programming. Create functions and pass values as argument. this makes your code readable, easy to maintain and also reusable
28th Feb 2017, 8:17 PM
Alex A. Adusei
Alex A. Adusei - avatar
0
I think it depends on why do u need it. If you have only solution to solve the matter then it is good for use but if you find other way to solve then you have to consider performance matter as other says switch or other methods.
1st Mar 2017, 1:00 AM
David Lee