CH 4 Repetition Structures

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/50

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

51 Terms

1
New cards

What is the purpose of repetition structures?

makes computer repeat included code as necessary

2
New cards

Disadvantages of writing code that performs the same task multiple times.

  • makes program large

  • time consuming 

  • may need to be corrected in many places

3
New cards

What are the two broad categories of loops

Condition controlled and Count controlled

4
New cards

What type of loop is a while loop?

condition controlled loop

5
New cards

What type of loop is a for loop?

count controlled loop

6
New cards

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:

7
New cards

what is a count controlled loop/ for loop?

repeats a specific number of times

ex: for days in range (0,5) 

8
New cards

In a while loop, what are the two parts?

  1. test if the condition is true or false 

  2. statements are repeated as may times as the condition is true 

9
New cards
<p>This is the general format for what type of loop?</p>

This is the general format for what type of loop?

Condition controlled/while loop

10
New cards

In a flowchart for a condition controlled loop the line goes

back to the previous part

11
New cards

In a while loop what do we have to do for the loop to stop executing?

make a condition inside the loop false 

12
New cards

define iteration

one execution of the body of the loop

13
New cards

the while loop is known as a

pretest loop

14
New cards

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

15
New cards

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

16
New cards

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

17
New cards

Using the while loop as a count controlled loop by pairing it with a 

counter variable 

18
New cards

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 

19
New cards

What are the three actions a count controlled while loop must perform?

  1. initialization

  2. comparison 

  3. update  

20
New cards

Initialization

the counter variable must be initialized to a suitable starting value before the loop begins

21
New cards

comparison

the loop must compare the counter variable to a suitable ending value, to determine whether the loop should iterate or not 

22
New cards

update

during each iteration, the loop must update the counter variable to a new value

23
New cards

If there is only one statement in the body of a while loop you can

write the entire. loop on one line

24
New cards

what is the general format for a single line while loop

while condition: statement

25
New cards

example of single line while loop

n = 0

while n<10: n+=1 

26
New cards

count controlled loop definition

iterates a specific number of times

27
New cards

What is a for loop designed for?

work with a sequence of data items

  • iterates once for each item in the seqeunce 

28
New cards

What is the general format for a for loop?

for variable in [val1,val2,val3]:

tab—> statements

29
New cards

What is a target variable?

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

30
New cards

What function simplifies the process of writing a for loop?

range

31
New cards

Range will

return a iterable object

32
New cards

what is iterable?

contains a sequence of values that can be iterated over

33
New cards

range characters when there is one argument

ending limit

34
New cards

range characters when there is two arguments

start value and ending limit

35
New cards

range characters when there is three arguments

starting value, ending value, multiple

36
New cards

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

37
New cards

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 

38
New cards

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) 

39
New cards

When calculating a running total make sure to include

  1. a loop that reads each number in series 

  2. include a accumulator variable 

40
New cards

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 

41
New cards

augmented assignment operators 

special set of operators designed for this type of job, shorthand operators 

42
New cards

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 

43
New cards

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 

44
New cards

input validation definition

inspecting input before it is processed by the program, accomplished by a while loop

45
New cards

Walrus operator can be used to

create an assignment expression that combines the priming read with the input validation loop

46
New cards

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 

47
New cards

break statement causes a loop to 

terminate 

48
New cards

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 

49
New cards

else clause in a loop is useful only

when the loop contains a break statement

50
New cards

block statement appears after the

else clause executes only when the loop terminates normally, without encountering a break statement

51
New cards

the else statement will not execute its block of statements if the

loop terminates because of a break statement