Loops
When the same operation needs completion multiple times, a loop statement is required. For example, if you have a list variable and want to do the same operation on each member of the list. There are two types of loop statements the While statement which requires that a counter be incremented and the For statement which will look at every member of a list variable.
The same operation is done to each member in the indented section of code; variables are different for each iteration. The process is repeated when the indented content gets to the end unless the process has reached the specified number of iterations, such as using all the elements in a list variable. Loop statements can be nested together, which means that one loop statement can have another loop inside it, which requires additional care on indentation.
The same operation is done to each member in the indented section of code; variables are different for each iteration. The process is repeated when the indented content gets to the end unless the process has reached the specified number of iterations, such as using all the elements in a list variable. Loop statements can be nested together, which means that one loop statement can have another loop inside it, which requires additional care on indentation.
While
In Figure 6 the flow chart is represented and the code is visible in Figure 7.
|
For
The For statement is similar to the While statement but the counter will not be a numerical counter but instead will be a list variable. In Figure 10 the For statement code is displayed and corresponds to the diagram on Figure 9. The code contains only four functional lines. The usage of the colon and the indentation of the text are critical.
The nameKCTCS list variable contains 16 members but only a part of the list is displayed in Figure 10. While the last print statement is not required, it is used as a troubleshooting method to make sure that the entire process is being completed. It should be removed after the script is fully tested. Code in Figure 10 based on the flow chart in Figure 9
|
Nesting
Nesting For statements occurs when one For statement is executed and a second statement is contained within the first. This is accomplished by using two different amounts of indentation. The inner statement would be indented a greater amount than the outer. The inner loop is executed until it has meet the criteria and then returned to the outer loop. The next step of the outer loop is executed and then the inner loop is executed again. This process continues until the outer loop’s criteria is met.
The code for the nested For Statement is shown in Figure 12.
The code for the nested For Statement is shown in Figure 12.
- The first two statements define two string list variables, the first variables are the traditional college semesters and second variable is the colleges within KCTCS, the first list variable is composed of three members and the second is composed of 16 members (Note, not all members are visible in the screen capture). The variable definitions are outside of the For statements.
- The For statement is using a variable ‘college’ which will have the value of one member of the list variable nameKCTCS each time through. The first value in this list is Jefferson. The next line of code will print the value of the variable college and the first time through that will be Jefferson.
- The second For statement is nested inside the first (note the indention), the semester variable is used with the collegeSemester list variable. The first time the variable semester would contain the value fall. The action to be taken in this For statement is printing the semester name. When the print command is executed, it will return to the inner For statement and determine if any additional members are in the collegeSemester list, the next value for the semester variable is spring. This is repeated until all members of the collegeSemester list have been used.
- Once the inner For statement is complete the script returns to the first For statement increments the variable college to be the next member which is Elizabethtown, prints its value and then does the inner For statement three time.
- This process is repeated until all colleges have been displayed along with the semesters.
- Since there are 16 colleges and 3 semesters, the inner loop is completed a total of 48 times. A partial list of the results are shown in Figure 13.