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.
7 Antworten
+ 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.
0
@David A but it even takes me longer than usual to understand the code if I encounter somewhere.
0
@Manoj Singh can you give an example?
0
int i,j;
for(i=0; i<=5; i++){
for(j=0; j<=5; j++){
cout<<i<<j<<"\t";
}cout<<endl;}
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.
0
that's right, but every line will break after printing 5 sets. Thanks :)
0
@David Questions in basic programming interview revolve around loops mostly. Hence we are void to master them for interview sake.