Notes

3.8

Iteration

  • Iteration is a repeating part of an algorithm that repeats a specfied number of times until a condition is met
  • Iteration statemtns change the sequential flow of control by repeating a set of statements zero or more times, until a stopping condition is met

Example:

  • Trying to find a lost item
    • Determine what the item is
    • Ask every family member/friend who would see it
      • Stop condition: If you find it go to step 5
    • make a full sweep of everywhere you have
      • Stop Condition: If you find it go to step 5
    • Repeat steps 2 and 3 until the item is found
    • If never find it: Go Back to your room and cry
    • If you find item: enjoy the item
  • Example in code

Result of Iteration Statements

  • When using a range function, the last number in the input is not included in the output.
    • Starting value: The number that the range function will start with
    • Incrementing: The number in which the range increments by
    • Ending value: The final number of the range.

Loops

  • While loops use a condition then print something based on a condition.
    • This example prints all even numbers less than 10.

  • the percent sign means modulus so meaning if there is a remaninder when we dvide. So if number divided by 2 has no remainder then it will print number.
  • Then after that it will add 1 to the number and repeat the process untill it reaches 10.

3.10

Lists

  • the process of visiting each element in a list in a sequential order.
    • 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.
  • Traversing allows you to access or search for certain elements

College Board Lists

  • insert( ) allows a value to be inserted into a list at index i
  • append( ) allows a value to be added at the end of a list
  • remove( ) allows an element at index i to be deleted from a list
  • length( ) returns the number of elements currently in a specific list

Here is an example question

  • The answers would be B and D, because they value the responsies rather than the animals

  • The loop above allows the coder to access each item within the list

Hacks

Iteration

  1. Define an Iteration
    • Iteration is a repeating part of an algorithm that repeats a specfied number of times until a condition is met
  2. Make your own example of an iteration with at least 4 steps and a stopping condition(Similar to mine that I did)
    • Hypothetically there are 8 people named Liav (1-8)
    • Each person will say that their name is Liav until they get to the person who’s name is not Liav.
    • Each time the next “Liav” comes the number increases by one
    • Stop using a break function when you get to the person not named Liav (6)
  3. Program a simple iteration.

  • This iteration would have repeated Hi, my name is Liav! 8 times, but I added a break at 6 that prints “your name isnt Liav Anymore”

Iteration Statements

  1. What is an iteration statement, in your own words?
    • Iteration statemtn repeat statements zero or more times, until a stopping condition is met.
  2. Create a descending list of numbers using for loop

  1. Using while loop, make a list of numbers which will form an output of 3,16,29,42,55,68,81

Find the Lowest Value

  • Variable that holds the minimum and sets it to potential minimm value loop check and displays the minimum value.

Quiz

A few notes on reference sheet

  • aList[i] accesses the eleement of aList at index i the dirst element of aList is at index 1 and is accessed using the notation aList[1]
  • x <– aList[i] assigns the value of aList[i] to the variable given (x in this case)
  • Append (aList, value) increases the lenght of aList by 1 at the end of a list
  • Insert/Remove shifts the position to the right (increase) or shifts the position to the left (decrease)
    • Insert increases the value of the list by 1
    • Remove decreases the value of the list by 1