0

What are the best way to master nested loops?

I am having a really very hard time mastering the loops. So please people! help me to make my concepts more clear.

19th Jan 2017, 2:11 PM
Manoj Singh
Manoj Singh - avatar
7 Answers
+ 1
Nested loops are undesirable within a program. They are considered quadratic: work_done = list_size ^ 2 Ideally you want to avoid nested loops if at all possible. Therefore there is no real reason to master them.
19th Jan 2017, 2:18 PM
David A
0
@David A but it even takes me longer than usual to understand the code if I encounter somewhere.
19th Jan 2017, 2:20 PM
Manoj Singh
Manoj Singh - avatar
0
@Manoj Singh can you give an example?
19th Jan 2017, 2:26 PM
David A
0
int i,j; for(i=0; i<=5; i++){ for(j=0; j<=5; j++){ cout<<i<<j<<"\t"; }cout<<endl;}
19th Jan 2017, 2:33 PM
Manoj Singh
Manoj Singh - avatar
0
For that example, every value of i will produce a full set of j. Output : 00 01 02 03 04 05 10 11 12 13 14 15 ... .. . My original point still stands however, this is an inefficient method of programming and should be avoided at all costs.
19th Jan 2017, 2:38 PM
David A
0
that's right, but every line will break after printing 5 sets. Thanks :)
19th Jan 2017, 2:41 PM
Manoj Singh
Manoj Singh - avatar
0
@David Questions in basic programming interview revolve around loops mostly. Hence we are void to master them for interview sake.
6th Aug 2019, 6:17 AM
Arun Kumar