tags:

views:

1469

answers:

7

I'm looking for a good tutorial on writing and designing loops. I understand the basics of loops but nested loops give me a lot of trouble. To give you and idea, the following pattern below was kind of difficult for me to figure out.

1
12
123
1234
12345
123456

A: 

I don't remember seeing any "loop design" centred tutorials when I was learning to program. You will get a grasp of loops if you just start tackling on different problems and algorithms. Look for matrix problems, you will need nested loops there for instance...

Jan Hancic
A: 

I'm unaware of any tutorials on this subject, but I suggest you try Google. Also, the fact that you were able to figure out your example probably means that you don't need a tutorial as much as you need practice. Nested loops are somewhat mind-warping when you first encounter them. You might also want to look for references/tutorials pertaining to recursion, which is a related concept. Remember, practice makes perfect!

rmeador
A: 

Check out the MIT Course Material. Also consider getting a Safari subscription, a cheap way of getting some good learning books.

This MIT course points to Loops on the python wiki.

I found that working it out on paper, listing the variables helps in learning how this works.

Robert Wagner
+1  A: 

Check out:

In general (language-neutral) terms, the basic logic is quite straightforward. Where it can get more complex is if an inner loop terminates early & the manner of the break. It may cause the outer loop to move to the next value, or it may completely exit the outer loop as well.

The best way to learn this is to try out different cases to see how they behave, and read up on the ways to exit from loops.

Kevin Haines
+6  A: 

Loops

A loop is a construct that enables a set of instructions to be executed more than once. There are several loop constructions:

zero or more

These loops have the check at the begining of an iteration and as such will be executed 0 or more times. A while loop is an example.

one or more

These loops have the check at the end of the iteration and as such will be executed at least once. A do while loop is an example.

Loops with counters

These loops have a counter that counts from a certain number to an other number. The number can be used inside the loop (for example to access a field of an array).

Loops with an iterator

These loops use an iterator to loop through a certain structure.

Endless loops

These loops have no end. But of course nothing is forever, so the loop often contains a hidden mechanism.

Nested loops

If you understand single loops, nested loops can be difficult. But you need to focus on one loop at a time. Lets take your example:

1
12
123
1234
12345
123456

Ok, lets first look at the lines.

  • The first line has a single 1
  • The second line counts from 1 to 2
  • The third line counts from 1 to 3
  • ...

Generally: the n th line counts from 1 to n.

Great, no we have the individual line. But let's now look at all lines.

  • the first has n=1
  • the second has n=2
  • the third has n=3
  • ...

Hm, so we can use the loop counter of the outer loop as the n in the inner loop:

for n = 1 to 6
  s = ''
  for i = 1 to n // use the loopcounter of the outer loop
    s = s + char(i)
  end for
  out s
end for
Gamecat
+1  A: 

How about these:

Nested Loops

The Power of Nested Loops

or on YouTube "SQL Joins, nested loops and all that in less than 6 minutes" at http://www.youtube.com/watch?v=SmDZaH855qE

DOK
A: 
declare
s varchar2(10);
begin
  for n in 1..5 loop
    s:='';
    for i in 1..n loop
      s:=s||(i);
    end loop;
    dbms_output.put_line(s);
  end loop;
end;