ch. 4 loops

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/14

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 2:44 PM on 9/30/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

15 Terms

1
New cards

while loop

compound interest algorithm

planning:

  • add 1 to years

  • calculate interest

  • add interest to balance

report the final year value

the loop execute instructions repeatedly while a condition is True


2
New cards

type i: count-controlled loops

  • called definite loops

  • a while loop that is controlled by a counter variable


3
New cards

type ii: event-controlled loops

  • indefinite loops

  • a while loop is controlled by a loop variable


4
New cards

processing sentinel values

  • sentinel values are often used when taking input and

    • you don’t know how many input items are there

    • use a ‘special’ character or value to signal the end of input

    • for numeric input of positive numbers it is common to use the value -1

sentinel value denotes the end of a data set, but it’s not part of the data

5
New cards

priming read

some programmers don’t like the trick of initializing the input variable with a value other than a sentinel


an alternative is to change the variable with a read before the loop

6
New cards

modification read

the input operation at the bottom of the loop is used to obtain the next input

7
New cards

generating random numbers

random() returns a floating number that is >= 0 and <1

8
New cards

randint() function

a random integer in a given range

9
New cards

loop algorithm: sum values

initialize total to 0

use while loop with sentinel

10
New cards

loop algorithm: average values

keep a running total

initialize count to 0

increment total and count per input

check for count 0 before divide!

11
New cards

loop algorithm: counting matches

initialize negatives to 0

use a while loop

add to negatives per match

12
New cards

loop algorithm: prompt until a match is found

initialize boolean flag to false

test sentinel in while loop

  • get input and compare to valid range

  • if input is in range change flag to true

  • loop will stop executing when flag changes


13
New cards

loop algorithm: maximum/minimum

get first input value

  • the largest/smallest

get another input

loop as long as the input is valid (non-sentinel):

  • compare new input to largest/smallest and update largest/smallest if needed

  • get another input value


14
New cards

loop algorithm: comparing adjacent values

get first input and store it in value

get another input

loop as long as the input is valid (non-sentinel):

  • copy value to previous variable

  • store the new input in value variable

  • compare value to previous and output message if same

  • get another input


15
New cards