1/50
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
What is the purpose of repetition structures?
makes computer repeat included code as necessary
Disadvantages of writing code that performs the same task multiple times.
makes program large
time consuming
may need to be corrected in many places
What are the two broad categories of loops
Condition controlled and Count controlled
What type of loop is a while loop?
condition controlled loop
What type of loop is a for loop?
count controlled loop
What is a condition controlled loop/ while loop?
uses a true/false condition to control the number of times the loop iterates
ex:
count = 0
while count > 0:
what is a count controlled loop/ for loop?
repeats a specific number of times
ex: for days in range (0,5)
In a while loop, what are the two parts?
test if the condition is true or false
statements are repeated as may times as the condition is true

This is the general format for what type of loop?
Condition controlled/while loop
In a flowchart for a condition controlled loop the line goes
back to the previous part
In a while loop what do we have to do for the loop to stop executing?
make a condition inside the loop false
define iteration
one execution of the body of the loop
the while loop is known as a
pretest loop
What is a pretest loop?
a test condition before performing an iteration meaning it will never execute if condition is false to start with
it requires performing some steps prior to the loop
What is an infinite loop?
loop that does not have a way of stopping
repeated until program is interrupted
normally occurs when programmer forgets a stopping code in loop
Infinite loops in while loops
loop must contain within themselves a way to terminate
something inside a while loop must eventually make the condition false
Using the while loop as a count controlled loop by pairing it with a
counter variable
What is a counter variable?
assigned a unique value during each iteration of a loop
used to count the number of times the loop iterates
What are the three actions a count controlled while loop must perform?
initialization
comparison
update
Initialization
the counter variable must be initialized to a suitable starting value before the loop begins
comparison
the loop must compare the counter variable to a suitable ending value, to determine whether the loop should iterate or not
update
during each iteration, the loop must update the counter variable to a new value
If there is only one statement in the body of a while loop you can
write the entire. loop on one line
what is the general format for a single line while loop
while condition: statement
example of single line while loop
n = 0
while n<10: n+=1
count controlled loop definition
iterates a specific number of times
What is a for loop designed for?
work with a sequence of data items
iterates once for each item in the seqeunce
What is the general format for a for loop?
for variable in [val1,val2,val3]:
tab—> statements
What is a target variable?
the variable which is the target of the assignment at the beginning of each iteration
What function simplifies the process of writing a for loop?
range
Range will
return a iterable object
what is iterable?
contains a sequence of values that can be iterated over
range characters when there is one argument
ending limit
range characters when there is two arguments
start value and ending limit
range characters when there is three arguments
starting value, ending value, multiple
What is the purpose of a target variable?
reference each item in the sequence as the loop iterates
used in calculations or tasks in the body of the loop
When the programmer does not know how many times to loop we can
ask the user and receive range inputs from the user and place them in a variable
When descending order in a range function, make sure the starting number is
larger than end limit, and the step value is negative
ex: range(10,0,-1)
When calculating a running total make sure to include
a loop that reads each number in series
include a accumulator variable
known as program that keeps a running total means
accumulate total and reads in series, where the end of the loop, accumulator will reference the total
augmented assignment operators
special set of operators designed for this type of job, shorthand operators
Define sentinel
special value that marks the end of a sequence of items
program will know that the end of the sequence of items was reaches and loop terminates
must be distinctive from regular value in sequence
Can computer tell the difference between good and bad data?
no, so design a program that bad input is never accepted
GIGO garbage in garbage out
input validation definition
inspecting input before it is processed by the program, accomplished by a while loop
Walrus operator can be used to
create an assignment expression that combines the priming read with the input validation loop
nested loop
loop that is contained inside another loop
inner loops goes through all iterations for each iteration of outer loop
inner loop completes iterations faster
total number of iterations in nested loop is number of outer loops x inner loops
break statement causes a loop to
terminate
continue statement causes the current iteration of a loop to
end early
all statements in the body of the loop that appear after it are ignores, and loop begins its next iteration
else clause in a loop is useful only
when the loop contains a break statement
block statement appears after the
else clause executes only when the loop terminates normally, without encountering a break statement
the else statement will not execute its block of statements if the
loop terminates because of a break statement