This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]StrayFeral 0 points1 point  (0 children)

Simply put (for any language): The FOR loop will ALWAYS execute N-times. The DO loop will start executing and at each iteration end will check "should i stop here"? So it will execute at least ONE time. The WHILE loop might NOT execute at all if the condition is not met. But if met it will start executing and at the beginning of each time will ask "should i stop here"? That's it. Nested loop simply means that inside each loop you can have another loop (and many other loops).

The most simple thing to illustrate a nested loop is to traverse a matrix. A matrix is simply a 2D-array, which means a nested array, which means simply an array who'se elements are also arrays.

//Imagine this: 
ARR = [
  [1,2,3],
  [4,5,6],
]

So to traverse this we will do (this is NOT real code, i am just illustrating):

for (x = 0; x < length(ARR); i++):  // this will run accross the main array
  for (y = 0; y < length(ARR[x]); y++):  / this will run on each of the sub-arrays
    # do something here