user405398
user405398

Reputation:

Nested For Loops?

I was a Computer Science Student. But i didn't coded much. For loops are quite simple to me and if it was nested for loop, my head starts to spin.

Tell me how can i master in nested for loops. Where shall i start to practice?

Once my class staff told us that any application which uses less for loops is the most efficient one. Is that real?

Suggest me any book or paper on for loops or links!!

deceze:How about you give us an example nested loop and tell us what you don't understand about it?

I cannot able to explain it fully. Whenever i start to think of a function which needs 2 for loops(nested), firts two iterations goes smooth. After that if i add any other calculation inside the inner loop, after a second its all gone. I cannot continue. So, then i have to run the loop in PC as smaller one, then i build it to a fully functional one.

Ok, Leave me. What you all do, i mean, how you start if you have to build a function which needs more than 2 nested for loops.

Upvotes: 1

Views: 1063

Answers (4)

george kyaw naing
george kyaw naing

Reputation: 11

Quoted from "Code Complete" in a sort of summary form " 16.3. Creating Loops Easily - From the Inside Out You can use a simple technique to get it right the first time.

Start with one case. Code that case with literals.

Then indent it, put a loop around it, and replace the literals with loop indexes or computed expressions.

Put another loop around that, if necessary, and replace more literals. Continue the process as long as you have to.

When you finish, add all the necessary initializations. Since you start at the simple case and work outward to generalize it, you might think of this as coding from the inside out.

"

Upvotes: 1

irrelephant
irrelephant

Reputation: 4111

Suggest me any book or paper on for loops or links!!

To solidify your understanding of 2 for loops, you can try practicing at http://www.codingbat.com/java - moreover, there's instant and (mostly) thorough grading! String-3 or Array-3 problems can use 2 for loops.

If you have a 2D array, and you need to reach all the indices, you could use 2 for loops. Thus, similarly, if you have a 3D array, you can use 3 nested loops. Remember that the inside of a loop ends before the outside -- so if you have 2 loops, the inside code would loop for the amount of time the inner loop specifies, for the number of times that the outside loop specifies. You can draw a diagram:

for(int i = 0; i < 2; i++) {
  for(int j = 0; j < 2; j++) {
    for(int k = 0; k < 2; k++) {
      //do something
    }
  }
}

The visual representation:

i:      0-----------1
        |           |
j:   0-----1     0-----1
     |     |     |     |
k: 0---1 0---1 0---1 0---1

If I assign letters:

i:      A-----------B
        |           |
j:   C-----D     E-----F
     |     |     |     |
k: G---H I---J K---L M---N

with X1 being the first part of X (before the nested for loop) and X2 the second part (after the nested for loop), the order of execution is:

A1, C1, G, H, C2, D1, I, J, D2, A2, B1, E1, K, L, E2, F1, M, N, F2, B2

So you don't need to picture n-dimensional arrays when you're looking at for loops.

Once my class staff told us that any application which uses less for loops is the most efficient one. Is that real?

If your for loop involves calculating a value like a formula, it would be faster and probably more efficient to just plug things into the formula. But generally the slowest Big O algorithm would affect the program's efficiency the most - so a stand-alone for loop that depends on n (as its stop condition) wouldn't slow down a program much more if it also had nested for loops that depend on n.

Upvotes: 5

Camilo Martin
Camilo Martin

Reputation: 37908

(I hope I've understood the question well)

You'll see, these nested loops are quite simple when you get the basic idea they represent and when you indent properly.

They are a way to represent sequences semantically.

For instance, this:

for (i = 1; i <= 5; i++)
{
    document.write("The number is " + i + "<br />"); 
    //(The "<br />" above is an HTML newline, this is in javascript)
}

Is the same as this:

document.write("The number is 1" + "<br />");
document.write("The number is 2" + "<br />");
document.write("The number is 3" + "<br />");
document.write("The number is 4" + "<br />");
document.write("The number is 5" + "<br />");

But the former is more clear about what it's doing. It's also respecting the DRY principle (i.e., it's horrible practice to copy-paste code ).

Now, on to nested for loops. You have to think on abstractions if you want to be a good programmer, so think of the for loop as a sequence of operations.
So what's a nested for loop? sequence of sequences! :)

For example, this repeats the same repetition thrice:

for (i = 1; i <= 3; i++)
{
    for (j = 1; j <= 3; j++)
    {
        document.write(j);
    }
    document.write("<br />");
}

And outputs:

123
123
123

More levels of nesting would represent more complex sequences. Generally, though, you shouldn't nest your code unless you're writing some complex algorithm - One method should only do one thing and do it well.

Upvotes: 1

user166390
user166390

Reputation:

Once my class staff told us that any application which uses less for loops is the most efficient one. Is that real?

Of course not.

However, an algorithm that uses nested loops may be O(n1 * n2 * n3 ...) -- which can end up like O(n^c) or worse (imagine a naive approach to find out if one array has all the same elements as another array, but in any order -- O(n^2)). This doesn't mean it is and a more specific case would be needed to be argue for one case or another. At some form, loops (no matter how they are hidden) are an integral part of imperative programming (even including most functional languages).

Upvotes: 1

Related Questions