Part 4, loops Flashcards
what are the two terms used for a block of code repeating over and over within a program
- looping
2. repetition
what term is given to the block of code that exists within a loop
loop contents
what are the three main types of loops
- fixed loop
- infinite loop
- conditional loop
explain what a fixed loop is
a fixed loop will loop a set amount of times
explain what an infinite loop is
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
what two ways might you set the number of loops on a fixed loop
- variable
2. constant
name two ways you might you stop an infinite loop
- stop the program
2. have a separate script stop the script containing the infinite loop
explain what a conditional loop is
a conditional loop is a type of loop that will loop until a condition has been reached
that is the condition reports true
explain what text processing is
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
give four examples of text processing
- finding the first occurrence of a word
- finding all occurrences of a word
- finding spelling mistakes
- appending puncuation
explain what an index variable is
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
what is a sentinel value used for
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.
give an example where a sentinel value might be useful
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
what is a parallel list
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
when might parallel lists be useful
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