Let the For Multilayer loop nesting never bothers the novice again

Source: Internet
Author: User
[Original] [small experience] let for Multilayer loop nesting never bothers novice

Little experience Topic: Let for multi-layer loop nesting never bothers the novice again

Small experience Keywords: multiple loop nesting like a small alarm clock

The importance of the problem: for birds like this dish, when they first touch a complex for-multi-loop nesting in a VBA program (such as a multi-variable multidimensional array program), it is very confusing to know how the program executes each detailed step of the loop, without a clue. Without knowing the correct execution steps of each loop, it is difficult to really understand the whole process of running the program, and in programming it is very likely that you do not know where to place each variable and where the variable should appear, where one of the loops should be placed, Therefore, it is very important to understand the multi-loop nested-nesting method in depth.

Little Experience Center content: When multiple for loop bodies are nested in a program, see the three simple for loop bodies in this sample program:

Sub Xunhuai ()
Dim a1%, a2%, a3%
For a1 = 1 to 3

For a2 = 2 to 4
For a3 = 1 to 3
Cells (a1, a2) = A1 * A2 * A3
Next A3
Next A2
Next A1
End Sub
When the program starts running, first executes for a1 = 1 to 3, the program assigns the first element value 1 to A1 in 1 to 3, and then executes for a2=2 to 4, the program assigns 2 to 4 the first element value 2 to A2, then executes for a3 = 1 to 3, the program 1 to 3 of the three elements of the value of the three-way all traversed once, the value assigned to the A3 variable is 1, 2, 3, and then executes the element value 4 for the second variable of a2=2 to 3, in turn, cells (a1, a2) = A1 * A2 * A3 statement get the corresponding cell value.
The way cells (A1,A2) will eventually produce several cell values is: A1 from 1 to 3 a total of 3 variable values, indicating that the final result of cells (A1,A2) is 3 lines; A2 from 2 to 4 has 3 variable element values, stating that the final result of cells (A1,A2) is 3 columns, All in all, cells (A1,A2) will eventually produce a number of cells that are assigned the formula: A1*A2 (in this case, 3*3=9 cells), where the number of cells that eventually display the result equals A1, and the number of columns equals A2 (in this case, 3 rows and 3 columns). For example, cells (x, y), assuming x=1 to 500;y=2 to 200, the number of cells (x, y) will eventually be assigned a total of 500*199=99500, where cells (x, y) eventually show the result of data in total 500 rows and 199 columns.
Small Experience Summary: multiple for loop shape such as a small alarm clock, the operation of the small alarm clock is: When the second hand is finished 60 times, the minute hand automatically increases by 1, then the hour hand is unchanged; When the second hand is 60 times, the minute hand is increased by 1, and the hour hand is not changed until the second hand runs continuously and the hand This is the same with the For loop nesting. Ha ha. Remember the small alarm clock, you may also forget the multilayer loop.


Small experience test method is: Enter the VBE code editing interface (can press ALT+F11 combination), continuously press the F8 key, execute code by statement, see the cursor back and forth situation, and then move the mouse to each variable a1, A2, A3, observe the current value of the variable, By observing what has changed in cell Sheet1, it is clear to know the current value of the current Center statement cells (A1, a2) = A1 * A2 * A3.
The last note: Small experience means that it is personal understanding, since it is a personal understanding, there must be deviations, since there are deviations, it is necessary to sincerely hope that the teacher to correct; but I confidently say, for the loop nesting is also relatively vague altar friends, will certainly follow the principle of small alarm clock, for the Loop nest for a lifetime.

Small experience Digression: Sometimes a program of a small doubt, it is likely to confuse the beginner for a few days or dozens of days, I always believe that each altar friend's personal experience is the most valuable wealth resources, I boldly put their little experience out of caught dead, the purpose is to let more and better experience in this forum has unveiled, Make the rookie very useful.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.