For Loop
Another fundamental concept in most programming languages is the use of a looping statement. A looping statement is a redundant function executed with different values of variables. A ‘for’ loop will go through a process a set number of times and then will exit the loop. The logic behind it is, after each process it will check to see if it has met the criteria for the number of operations needed. When these conditions are met it will exit the loop and continue the program. As with the if statement indentions are critical to proper functionality, a colon is used to define the loop. Any processes contained within the indented text will be executed a set number of times before exiting the loop.
Loops can be nested inside other loops, so that the outside loop might go through the process 10 times and the inner loop might be done 5 times, thus the total effect will be 50 operations. As was discovered with the if statement, the use of a colon at the end of the line of the for statement will need an indention that is required for proper operation. A ‘while’ statement is similar to the ‘for’ statement and will be covered in future discussions.
Loops can be nested inside other loops, so that the outside loop might go through the process 10 times and the inner loop might be done 5 times, thus the total effect will be 50 operations. As was discovered with the if statement, the use of a colon at the end of the line of the for statement will need an indention that is required for proper operation. A ‘while’ statement is similar to the ‘for’ statement and will be covered in future discussions.
The format of the for statement is:
for variable in range (x,y):
In Figure 15, the left side shows the results of the operation and the right side shows the code.
for variable in range (x,y):
In Figure 15, the left side shows the results of the operation and the right side shows the code.
Prior to starting the for command the variable x is initialized with a value of 5. The value of the y variable will initially be 1 (because of the range in the ‘for’ statement). The variable will be incremented by one for each time going through the for loop. The loop is only executed 29 times since when the value of 30 is reached the loop is exited. Since the print statement is located before the algebraic statement, the initial value of x is printed. The first time through the algebraic statement x =5 and y=1, the next time x=5 and y=2 which gives x a new value of 10, the next multiplication would have x=10 and y =3. This would continue for 29 times and it is amazing how fast the value of x grows.
Instead of a number for the range it could be a string. The range would proceed through the string until all members of the string have been used in the for loop and then would exit the loop. The first line of code in figure 16 shows the ‘for’ statement to be used, notice the string is not stored as a variable but instead is directly inputted. Note the colon after the string, which will create the appropriate indention and everything indented will be completed prior to returning to the ‘for’ statement. The variable ‘word’ will initially have the value of ‘G’, the print statement will print the string ‘Current Letter” and then value of the variable ‘word’. It will return to the ‘for’ statement and move one character to the right thus making the variable ‘word’ equal to ‘e’ and then go through the print statement again.
Quiz
There is a required quiz for this module (only for those enrolled in the class), which will cover some of the basic Python scripts discussed in this module. The quiz will require that the learner provide images of short pieces of code. Each image needs to use the following naming convention: quiz_question number_last name first initial. For example, Quiz_3_DiNotoV.