Starting Out with Python CH. 4: Repetition Structures

¡Supera tus tareas y exámenes ahora con Quizwiz!

disadvantages to duplicating code

- makes program large - time consuming - may need to be corrected in many places

using the target variable inside the loop

- purpose of target variable is to reference each item in a sequence as the loop iterates - target variable can be used in calculations or tasks in the body of the loop

programs that calculate the total of a series of numbers typically use two elements:

1. a loop that reads each number in the series 2. a variable that accumulates the total of the numbers as they are read

characteristics of nested loops

1. an inner loop goes through all of its iterations for every single iteration of an outer loop 2. inner loops complete their iterations faster than outer loops. 3. to get the total number of iterations in a nested loop, multiply the number of iterations of all the loops.

two parts of the while loop

1. condition tested for true or false value 2. statements repeated as long as condition is true; if the condition is false the program exits the loop

range characteristics

ONE ARGUMENT: used as ending limit TWO ARGUMENTS: starting value and ending limit THREE ARGUMENTS: third argument is step value

range function

Simplifies the process of writing a FOR loop by creating a type of object known as an iterable, similar to a list, that contains a sequence of values that can be iterated over with something like a loop

list

a comma-separated sequence of data items that are enclosed in a set of brackets

nested loop

a loop that inside another loop

pretest loop

a loops that tests its condition before performing an iteration; It will never execute if condition is false to start with and requires performing some steps prior to the loop; WHILE loops are pretest loops

sentinel

a special value that marks the end of a sequence of values and tells the program to terminate the loop; used when processing a long sequence of values with a loop; must be distinctive enough that it will not be mistaken as a regular value in the sequence

running total

a sum of numbers that accumulates with each iteration of a loop. The variable used to keep the running total is called the accumulator.

infinite loop

continues to repeat until the program is interrupted; usually occurs when the programmer forgets to write code inside the loop that makes the test condition false

iteration

each execution of the body of a loop

count-controlled loop general format

for VARIABLE in [value1, value2, etc.] STATEMENT STATEMENT etc.

repetition structure (loop)

makes computer repeat included code as necessary; includes condition-controlled loops and count-controlled loops

using range with three arguments

range(start, stop, step_value) 1. start: the starting point of the range (inclusive) 2. stop: the ending point (not inclusive) 3. step_value: the distance between each value in the range (if not included, assumed to be 1)

count-controlled loop (for loop)

repeats a specific number of times; uses FOR statement; designed to work with a sequence of data items, iterates once for each item in the sequence

the first step in a running total is to

set the accumulator variable to 0

priming read

the input instruction that appears above the loop that it controls; used to get the first input item from the user that will be tested by the validation loop

input validation loop (error trap or error handler)

the process of inspecting data that has been input into a program, to make sure it is valid before it is used in a computation.

target variable

the variable which is the target of the assignment at the beginning of each iteration

condition-controlled loop (while loop)

uses a true/false condition to control the number of times that it repeats; uses WHILE statement


Conjuntos de estudio relacionados

CSC440 Chapter 9: Software Evolution (Software Engineering, Sommerville, 10th Edition)

View Set

Chapter 15 Prenatal Diagnostic Tests

View Set

First Major Summative: Gross Anatomy Question Set

View Set

1.- Completing the Application, Underwriting and Delivering the Policy

View Set