Python Unit 5 - Loops
flag variable
A flag variable is a variable that is used to control a loop and determine when it should terminate. Often a flag variable stores the Boolean values True or False and flag variables are used with indefinite while loops rather than definite for loops.
for loop
A for loop is a definite loop that iterates a specified number of times. A for loop is often used with the range function as in for i in range(1, 5): that would create a for loop that iterates exactly 4 times (high - low = 5 - 1 = 4.)
nested loop
A nested loop is a loop that is placed inside of another loop. While we are not studying nested loops in this course during this unit (in 2018-19 at least), you should be able to identify nested loops when you see them especially in graphical programs like the Advanced Drawing project. Here's an example of nested for loops: for row in range(1, 3): for col in range(1, 4): print("*") where the for loop with the loop variable col is indented and therefore placed inside the body of the for loop with the loop variable row.
while loop
A while loop is an indefinite loop. It causes one or more statements to execute many times. Those statements are indented under the while statement and are considered to be the body of the while loop. while loops, unlike for loops, are usually used when a user must input numbers or words.
accumulator statement
An accumulator statement adds a number to a variable. Usually accumulator statements are found in the body of loops. An accumulator statement does not simply add one to a variable like a counter statement. An example of an accumulator statement is sum = sum + i or sum = sum + userInput where i and userInput would vary each time. Those example could be rewritten as sum += i and sum += userInput.
definite loop
In Python, for loops are definite loops. A definite loop can be traced by a programmer to determine exactly how many times it will iterate.
Boolean value
In Python, the Boolean values are True and False. A variable can be set equal to a Boolean value as in done = False where the flag variable done is set equal to False but changed to True during the execution of a while loop to indicate that the loop should terminate.
indefinite loop
In Python, while loops are indefinite loops. An indefinite loop cannot be traced by a programmer to determine exactly how many times it will iterate since it usually depends on input by a user.
sentinel value
Often -1 is used as a sentinel value. A sentinel value is a special number or word that the user is prompted to input when he/she is finished inputting a sequence of numbers or words. If a user is prompted to input a number of quiz scores, -1 would be a logical sentinel value because it would not be confused with an actual quiz score.
increment
To increment a variable means to add 1 to its value.
loop variable
a variable that controls the execution of a for loop & is often named i
counter statement
A counter statement is a statement that increments a variable by by a fixed amount like 1 or 2. Usually counter statements are placed inside of loops. An example of a counter statement is sum = sum + 1 though it could be shortened as sum += 1
range
The range function is often used with for loops & specifies the beginning & ending values of the loop variable. In the statement for i in range(1, 5): the loop variable i begins at 1 and ends at 4.
