Iteration - AP Computer Science Principles
- 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.
- 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.
- 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.
- What is the output of 'for i in range(2): print('Hi')'?
- Prints 'Hi' twice. The loop executes twice with range(2).
- What is the role of an iterator?
- An iterator provides access to elements in a collection. It enables sequential access to data structures.
- Choose the correct syntax for a 'while' loop in Python.
- 'while condition:'. This is the proper Python while loop syntax.
- 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.
- What loop structure is used for counting iterations?
- A 'for' loop is used for counting iterations. It provides precise iteration control.
- 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.
- What is the output of range(5, 1, -1)?
- Outputs 5, 4, 3, 2. Negative step creates descending sequence.
- 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.
- 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.
- Which Python statement exits a loop prematurely?
- The 'break' statement exits a loop prematurely. It immediately terminates the loop regardless of condition.
- Determine the loop type: Iterates with a known count.
- A 'for' loop iterates with a known count. The iteration count is predetermined.
- 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.
- 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.
- 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.
- 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.
- 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.
- What will 'while False: print('Hello')' output?
- Outputs nothing. False condition prevents any loop execution.
- Identify an example of an iterable in Python.
- Lists are examples of iterables. Lists can be looped through using for loops.
- 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.
- 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.
- Describe 'break' in the context of loops.
- 'Break' exits the loop immediately. It terminates the loop completely.
- 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.
- What is a key characteristic of a 'while' loop?
- Continues as long as the condition is true. It evaluates the condition before each iteration.
- Find the error: while True print('Hello')
- Error: Missing colon after 'True'. Python requires a colon after the condition.
- 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.
- 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.
- What is a common use of iteration in programming?
- Iterating through lists or arrays. It's essential for processing data collections efficiently.
- Find the error: for i in range(3): print(i)
- No error; it prints 0, 1, 2. The syntax is valid and executes correctly.
- What is an infinite loop?
- An infinite loop runs indefinitely without terminating. Usually occurs when the termination condition is never met.
- What is a nested loop?
- A loop inside another loop. Creates multiple levels of iteration depth.
- What is the benefit of using 'range()' in loops?
- 'range()' provides a sequence of numbers to iterate over. It generates sequences without manual counting.
- Identify a common loop structure in programming.
- A 'for' loop is a common loop structure. It iterates over sequences or a specified range.
- 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.
- What does 'for i in range(3)' do?
- Iterates with values 0, 1, 2. Single argument creates sequence from 0 to n-1.
- What is a loop iteration?
- One complete execution of the loop body. It represents one cycle through the loop body.