Ray Bell wrote: I'm still reusing code when I need a loop.
How can you explain the logic of a loop in 50 words or less?
Hey Ray, welcome to the Ranch. Not sure about the 50 words. Let me try my best to explain.
The nested loops are useful when you want to iterate over two levels. Say for example, a 2D (Two dimensional) array which will be across rows and columns, for which you need two rounds ofi iteration. At first you would iterate on the row level. Inside each row, you still need to iterate each column on the same row (think of an excel spreadsheet or a table). You need to hold the position of row at the outer layer, until all the columns are visited in the inner layer. The looping is done here at more than one level for doing any operation on the data which is of more than one dimension.
Say for example, you have a two dimensional array of 3 rows 3 columns (3 X 3 array) having 9 elements in total. You need to visit each cell (an intersection of a row and column is called a cell) to find out the occurrence of a particular number. For which you need to do the following.
Set Count = 0
Set NumberToSearch = 5
Iterate Rows from 1 To M
-- OUTER LOOP controlling the rows
Iterate Columns from 1 to N
-- INNER loop controlling the columns per row
If Value at the cell (R, C) = 5 Then
Increment count (set count = count + 1)
Else
Do Nothing. Continue with the next column in the same row if any pending
End.
Hope this helps you get the concept. Let us know if you have any other questions.