Lesson 3.8 and 3.10 - Boolean Iteration and Lists
Notes, questions, and hacks
Notes
- Iteration: a repeating portion of an algorithm, repeats a specified number of times or until a given condition is met
- Iteration Statements: change the sequential flow of control by repeating a set of statements zero or more times, until a stopping condition is met
- Repeat Until: if the condition evaluates to true initially, the loop body is not executed at all, due to the condition being checked before the loop
- Range function: use variable to represent what the range of numbers the output can show. Use with for loop
- when using the range function, the last number, or the ending value, is not included in the output, therefore we would have to add 1 to receive the correct output
- For loop: process stops if next element meets statement
- While loop: process is repeated until statement is met
- In lists:
- append(): adding element to the end of the list
- insert(): adding an element in a specific position
- remove(): remove an item from a list
- len(): returns number of items in a list
- sort(): permanently changes order of list
- sorted(): returns copy of list, leaving original copy unchanged
- range(): range of number able to use, makes working with numbers more efficient
- min(): find the least value in a list
- max(): find highest value in a list
- sum(): sum of all values in a list
- Traversing a list is process of visiting each element in a list in a sequential order. Used to access, search for, and modify elements in the list
- Complete Traversal: All elements in a list are assessed
- Partial Traversal: Only a given portion of elements are assessed
- Iterative Traversal: When loops are used to iterate through a list and to access each single element at a time.
Hacks
Section 3.8.1
Iteration: part of an algorithm that is repeated until a certain condition is met
- Start
- The number of pretzels in the pack is 6
- Eat one fruit snack, number of pretzels in pack goes down by 1
- How many pretzels are left?
- Repeat step 3 until number of pretzels is 0
- Display that pack is empty
- Finish
pretzel = 6
while (pretzel > 0):
pretzel -= 1
print(pretzel)
if pretzel == 0:
print("All done!")
nums = [5, 4, 3, 2, 1]
for i in nums:
if i <= 2:
break
print(i)
number = 3
while number <= 81:
print(number)
number += 13
number = 0
nums = [38, 45, 67, 83, 78]
min = 40
for num in nums:
if num < min:
min = num
print(min)
APCSP Reference Page Notes
- PROCEDURE procName(parameter1,
parameter2, ...)
{
<# block of statements>
}- procName: a procedure that takes zero or more arguments
- procedure procName can be called using "procName(arg1, arg2, ...)", where arg1 is assigned to parameter1, arg2 is assigned to parameter2, etc.
- PROCEDURE procName(parameter1,
parameter2, ...)
{
<# block of statements>
RETURN(expression)
}- return statement may appear at any point inside the procedure and causes an immediate return from the procedure back to the calling statement.
- value returned by the procedure procName can be assigned to the variable result using "result ← procName(arg1, arg2, ...)"
- APPEND(aList, value)
- length of aList is increased by 1, and value is placed at the end of aList
- length of aList is increased by 1, and value is placed at the end of aList
- REPEAT UNTIL(condition)
{
<# block of statements>
}- code is repeated until the Boolean condition evaluates to true