Part 4, loops Flashcards

1
Q

what are the two terms used for a block of code repeating over and over within a program

A
  1. looping

2. repetition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what term is given to the block of code that exists within a loop

A

loop contents

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what are the three main types of loops

A
  1. fixed loop
  2. infinite loop
  3. conditional loop
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

explain what a fixed loop is

A

a fixed loop will loop a set amount of times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

explain what an infinite loop is

A

an infinite loop will have no set number of times to loop or any condition to break out the loop

note: although this type of loop is potentially infinite there are a few ways to stop it running

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what two ways might you set the number of loops on a fixed loop

A
  1. variable

2. constant

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

name two ways you might you stop an infinite loop

A
  1. stop the program

2. have a separate script stop the script containing the infinite loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

explain what a conditional loop is

A

a conditional loop is a type of loop that will loop until a condition has been reached

that is the condition reports true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

explain what text processing is

A

text processing is the act of a program manipulating or searching through text

note: features in a word processor such as word count is an example of text processing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

give four examples of text processing

A
  1. finding the first occurrence of a word
  2. finding all occurrences of a word
  3. finding spelling mistakes
  4. appending puncuation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

explain what an index variable is

A

an index variable is used to select an item in a list, array or string

index variables are helpful when iterating over the entirety of a list, array or string and needing to pick out an items position

when incrementing the index variable with each iteration of a loop. the index variable can be used as the position inside the string or array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is a sentinel value used for

A

a sentinel value is used to stop a loop, it can be used when a loop will stop but the amount of times to loop is unknown and there is no condition to stop the loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

give an example where a sentinel value might be useful

A

a sentinel value would be useful when taking user input

a loop could take the user input piece by piece and when the user is finished they could enter “Q”

which would be the sentinel value used to quit the loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what is a parallel list

A

a parallel list is two lists that have a relationship,

where the position of an item in one list is directly related to an item in another list with the same position

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

when might parallel lists be useful

A

a parallel list may be useful in a situation where we need to store multiple pieces of data while maintaining the relationship of the data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

you have a specification containing words such as

“for each”, “how many”, “every”, “until”, “total”, “while”,

how might you translate these words into your algorithm

A

“for each”, “how many”, “every”, “until”, “total”, “while”,

all suggest that some sort of looping may be needed

17
Q

name 6 words you might see in a specification that suggest looping may be needed

A
  1. “for each”,
  2. “how many”,
  3. “every”,
  4. “until”,
  5. “total”,
  6. “while”,
18
Q

how might “how many” be translated into an algorithm”

A

“how many” assumes we need to count something so might be re written as

repeat (n) times {
if x occurs add one to the count
}

19
Q

how might “until” be translated into an algorithm

A

“until” might be re written as

repeat until (x=y)
    add 1 to y
20
Q

name 4 tests that can be used when debugging/testing lists

A
  1. test lists of different lengths
  2. test an empty list
  3. test the first, last and mid item in the list
  4. test for different sized outputs (only one output expected) (three outputs expected)