Iteration - AP Computer Science Principles

  1. What happens when 'continue' is executed in a loop?
    Skips the current iteration and continues with the next. It jumps directly to the next loop iteration.
  2. Find the error: for i in [1,2,3]: print(i)
    No error; it prints 1, 2, 3. The syntax correctly iterates over the list.
  3. Identify the purpose of 'range()' in a 'for' loop.
    'range()' specifies the start, stop, and step of iteration. It generates a sequence of numbers for iteration.
  4. What is the output of 'for i in range(2): print('Hi')'?
    Prints 'Hi' twice. The loop executes twice with range(2).
  5. What is the role of an iterator?
    An iterator provides access to elements in a collection. It enables sequential access to data structures.
  6. Choose the correct syntax for a 'while' loop in Python.
    'while condition:'. This is the proper Python while loop syntax.
  7. What is the output of 'for i in range(4, 1, -1): print(i)'?
    Prints 4, 3, 2. Negative step counts down from 4 to 2.
  8. What loop structure is used for counting iterations?
    A 'for' loop is used for counting iterations. It provides precise iteration control.
  9. Which loop structure is best for iterating a known number of times?
    A 'for' loop is best for iterating a known number of times. It allows precise control over the number of iterations.
  10. What is the output of range(5, 1, -1)?
    Outputs 5, 4, 3, 2. Negative step creates descending sequence.
  11. Identify the loop structure that continues until a condition is false.
    A 'while' loop continues until a condition is false. It evaluates the condition before each iteration.
  12. Correct the error: for i in 0..5: print(i)
    Use 'range(0, 5)' instead of '0..5'. Python uses range() function instead of .. notation.
  13. Which Python statement exits a loop prematurely?
    The 'break' statement exits a loop prematurely. It immediately terminates the loop regardless of condition.
  14. Determine the loop type: Iterates with a known count.
    A 'for' loop iterates with a known count. The iteration count is predetermined.
  15. What type of loop would you use for indefinite iteration?
    A 'while' loop is used for indefinite iteration. The condition determines when to stop iterating.
  16. What is iteration in computer programming?
    Iteration is the repetition of a block of code multiple times. It's a fundamental programming concept for repeating tasks.
  17. What does 'for i in range(0,5,2)' iterate over?
    Iterates over 0, 2, 4. The step parameter of 2 skips every other number.
  18. Which loop structure is used for iterating over a sequence?
    A 'for' loop is used for iterating over a sequence. It processes each element in the sequence.
  19. Identify the loop that allows iteration over elements of a list.
    'for element in list:' allows iteration over list elements. This syntax directly accesses each list element.
  20. What will 'while False: print('Hello')' output?
    Outputs nothing. False condition prevents any loop execution.
  21. Identify an example of an iterable in Python.
    Lists are examples of iterables. Lists can be looped through using for loops.
  22. What keyword starts a 'while' loop in Python?
    The 'while' keyword starts a 'while' loop. It's the standard syntax for conditional loops in Python.
  23. In a 'for' loop, what does the loop variable do?
    The loop variable iterates through a sequence. It takes on each value from the sequence being looped over.
  24. Describe 'break' in the context of loops.
    'Break' exits the loop immediately. It terminates the loop completely.
  25. Write a loop that prints numbers 1 to 5.
    for i in range(1, 6): print(i). This creates a sequence from 1 to 5 inclusive.
  26. What is a key characteristic of a 'while' loop?
    Continues as long as the condition is true. It evaluates the condition before each iteration.
  27. Find the error: while True print('Hello')
    Error: Missing colon after 'True'. Python requires a colon after the condition.
  28. What does the 'continue' statement do in a loop?
    The 'continue' statement skips to the next iteration. It bypasses remaining code in the current iteration.
  29. What is the function of 'pass' in a loop?
    'pass' acts as a placeholder and does nothing. It prevents syntax errors in empty code blocks.
  30. What is a common use of iteration in programming?
    Iterating through lists or arrays. It's essential for processing data collections efficiently.
  31. Find the error: for i in range(3): print(i)
    No error; it prints 0, 1, 2. The syntax is valid and executes correctly.
  32. What is an infinite loop?
    An infinite loop runs indefinitely without terminating. Usually occurs when the termination condition is never met.
  33. What is a nested loop?
    A loop inside another loop. Creates multiple levels of iteration depth.
  34. What is the benefit of using 'range()' in loops?
    'range()' provides a sequence of numbers to iterate over. It generates sequences without manual counting.
  35. Identify a common loop structure in programming.
    A 'for' loop is a common loop structure. It iterates over sequences or a specified range.
  36. Find the error: for i in range(1,5): print(i)
    No error; it prints numbers 1 to 4. The syntax is correct and executes properly.
  37. What does 'for i in range(3)' do?
    Iterates with values 0, 1, 2. Single argument creates sequence from 0 to n-1.
  38. What is a loop iteration?
    One complete execution of the loop body. It represents one cycle through the loop body.

Study these flashcards