Section 8 Big Ideas/Learning Objectives

  • Understanding What is Iteration
  • Using for and while loops

Section 10 Big Ideas/Learning Objectives

  • Understanding how to edit lists by adding, inserting, and removing data
  • Using loops to iterate through lists and abstract data
  • Determine the results or side effects of iteration statements
  • Write sorting algorithms using iteration

Section 8 Necessary Vocabulary

  • Iteration - Repitition of a Process
  • For Loop - FOR LOOP repeats a function for a set number of times; I is the number of times repeated
  • While Loop - The while loop is used to repeat a section of code an unknown number of times until a specific condition is met
  • Initialization - What sets the counter variable to a starting value. For example (var i = 0) represents an initial value of 0.
  • Condition - Allows the computer to know whether or not to keep repeating the loop.
  • increment/decrement - Modifies the counter variable after each repetition.

Section 10 Necessary Vocabulary

  • Indexing / List Index - The position of an element in a list, starting from 0 append, remove, pop - Various methods, append adds an element to the end, remove removes at an index, and pop removes the last item. -Elements [in a list] - An item in a list.
  • Nesting - Having one data type or function inside another data type or function, such as lists or loops. array, another name for a list

What is Iteration?

Iterative statements are also called loops, and they repeat themselves over and over until the condition for stopping is met.

In College Board's Pseudocode, the first is a REPEAT n TIMES loop, where the n represents some number.

The second type of loop is a REPEAT UNTIL (condition) loop, where the loop will continue to run until a condition is met.

Conceptually, a while loop is very similar to an if conditional, except that a while is continually executed until it's no longer true and an if is only executed once.

Practice

Consider the Following Code Segment. What is the Counter Variable and the Condition set as?

    for (var i = 10; i > 0; i--) {
   println(i);
}
Answer! [Counter Variable: 10, Condition: Greater than 0]

Practice

Consider the Following Code Segment. How Many times will print(ln) be called?

   for (var i = 1; i <= 10; i++) {
  for (var j = 1; j <= 10; j++) {
    println(i * j);
  }
}
Answer! [100 Times.The computer will execute the outer for loop 10 times (starting at i = 1 and ending after i = 10). Each time that it executes the outer loop, it will execute the inner for loop 10 times (starting at j = 1 and ending after j = 10). Each inner execution will call println() once.10 times 10 times 1 is 100, so the computer will call println() 100 times.]

Practice

Consider the Following Code Segment. What is it initial value? What does the while loop check?

   var numRabbits = 2;
var numYears = 0;
while (numRabbits <= 100) {
    numRabbits += (numRabbits/2 * 30);
    numYears++;
}
Answer! [2 Rabbits.The while loop checks that the number of rabbits is less than 100. As long as the population is less the 100, the code inside the loop continues to run.]

Sources

  • AP College Board/Classrom
  • Khan Academy: APCSP

Hacks

Exercise #1

excercise1

list = [1, 2, 3, 4, 5] # Print this list in reverse order
list.reverse()
print(list)
newlist = list.reverse()
print(newlist)
[5, 4, 3, 2, 1]
None

Excercise #2

excerise2

list = [9, 8, 4, 3, 5, 2, 6, 7, 1, 0] # Sort this array using bubble sort
print("List before sort:", list)
def sort_list(list):
    for index in range(1, len(list)):
        value = list[index]
        i= index - 1
        while i>=0:
            if value < list[i]:
                list[i+1]= list[i]
                list[i] = value
                i = i - 1
            else:
                break
IS = sort_list(list)
print("List after sort:", list)
List before sort: [9, 8, 4, 3, 5, 2, 6, 7, 1, 0]
List after sort: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Multiple Choice Quiz and Quiz Corrections

Question 6 wrong1

Explanation: My answer choice is wrong because I got confused with the defintion of what a POP command does. A POP command removes the last item in a list. My confusion is now cleared up after this question, and this will help me better understand what a POP command does and its function.

Question 8 wrong2

Explanation: My answer choice is wrong because the while loop is used to repeat a section of code an unknown number of times until a specific condition is met. A while loop does not iterate over an iterable and for a set amount of time and does not intuitively take an iterable or manipulate it over a set period using pointers.

Reflection

Initially, this lesson was difficult to understand because it was a very fast-paced lesson, which made it hard to follow along. Overall, it was a lesson that was difficult to understand while the information was being presented, however, after applying thr information I learned to the exercises and quizes, it put together all the information and it helped make sense in my mind. The exercises was helpful since it was practical and I had to type the code and watch it for mistakes.