Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a loop in programming?
A loop is a way to repeat a set of instructions multiple times until a condition is met, like doing the same chore over and over until it's done.
Click to reveal answer
beginner
Name two common types of loops.
Two common loops are: 1. For loop: repeats a set number of times. 2. While loop: repeats while a condition is true.
Click to reveal answer
intermediate
What happens if a loop's condition never becomes false?
The loop runs forever, called an infinite loop. It's like a chore that never ends and can freeze a program.
Click to reveal answer
beginner
Explain a real-life example of a loop.
Imagine washing dishes: you wash one dish, then the next, and keep going until no dishes are left. This is like a loop repeating an action until a condition (no dishes) is met.
Click to reveal answer
beginner
What is the purpose of a loop counter?
A loop counter keeps track of how many times the loop has run, helping to stop the loop after a set number of repeats.
Click to reveal answer
Which loop repeats a fixed number of times?
AFor loop
BWhile loop
CIf statement
DSwitch case
✗ Incorrect
A for loop repeats a set number of times, like counting from 1 to 5.
What is an infinite loop?
AA loop that never stops
BA loop that runs once
CA loop that counts to zero
DA loop that skips steps
✗ Incorrect
An infinite loop keeps running because its stop condition is never met.
Which part of a loop decides when to stop?
ACounter
BFunction
CVariable
DCondition
✗ Incorrect
The condition is checked each time to decide if the loop should continue or stop.
What does a loop counter do?
AChanges the loop condition
BCounts how many times the loop runs
CStops the program
DStarts the loop
✗ Incorrect
The counter tracks the number of repetitions to help control the loop.
Which loop type repeats while a condition is true?
ADo-while loop
BFor loop
CWhile loop
DSwitch loop
✗ Incorrect
A while loop continues as long as its condition remains true.
Describe how a loop works using a real-life example.
Think about doing the same task repeatedly until something changes.
You got /3 concepts.
Explain the difference between a for loop and a while loop.
One counts repeats, the other checks a condition each time.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a loop in programming?
easy
A. To display text on the screen
B. To store data permanently
C. To repeat a set of actions multiple times
D. To create a new variable
Solution
Step 1: Understand the role of loops
Loops are used to repeat actions without writing the same code again and again.
Step 2: Identify the correct purpose
Repeating a set of actions multiple times matches the purpose of loops.
Final Answer:
To repeat a set of actions multiple times -> Option C
Quick Check:
Loops = Repeat actions [OK]
Hint: Loops repeat tasks to save writing code again [OK]
Common Mistakes:
Confusing loops with data storage
Thinking loops create variables
Believing loops only display text
2. Which of the following is the correct syntax for a for loop in Python to repeat 5 times?
easy
A. for i to 5:
B. for i in range(5):
C. loop i from 1 to 5:
D. repeat 5 times:
Solution
Step 1: Recall Python for loop syntax
Python uses for variable in range(number): to repeat actions a set number of times.
Step 2: Match the correct syntax
for i in range(5): correctly repeats 5 times from 0 to 4.
Final Answer:
for i in range(5): -> Option B
Quick Check:
Python for loop = for i in range(5): [OK]
Hint: Python for loops use 'for variable in range():' [OK]
Common Mistakes:
Using 'for i to 5:' which is invalid syntax
Confusing loop syntax with other languages
Missing colon at the end of the loop line
3. What will be the output of this Python code?
sum = 0
for i in range(3):
sum += i
print(sum)
medium
A. 6
B. 3
C. 0
D. SyntaxError
Solution
Step 1: Trace the loop iterations
The loop runs with i = 0, 1, 2. sum starts at 0.
Step 2: Calculate sum after each iteration
Iteration 1: sum = 0 + 0 = 0 Iteration 2: sum = 0 + 1 = 1 Iteration 3: sum = 1 + 2 = 3
Step 3: Sum all values
Sum of 0 + 1 + 2 = 3, but the code adds i each iteration, so final sum is 3.
Final Answer:
3 -> Option A
Quick Check:
0+1+2 = 3 [OK]
Hint: Add all numbers from 0 up to 2 [OK]
Common Mistakes:
Adding 3 instead of stopping at 2
Confusing range(3) with range(1,3)
Expecting SyntaxError due to indentation
4. Identify the error in this loop code:
i = 1
while i < 4
print(i)
i += 1
medium
A. Missing colon after while condition
B. Variable i should start at 0
C. print() should be outside the loop
D. i += 1 should be before print()
Solution
Step 1: Check while loop syntax
Python requires a colon ':' after the while condition to start the loop block.
Step 2: Identify the missing colon
The code misses ':' after while i < 4, causing a syntax error.
Final Answer:
Missing colon after while condition -> Option A
Quick Check:
while condition needs ':' [OK]
Hint: Always put ':' after while condition [OK]
Common Mistakes:
Ignoring missing colon error
Changing variable start unnecessarily
Moving print() outside loop incorrectly
5. You want to print all even numbers from 2 to 10 using a loop. Which code snippet correctly does this?
hard
A. for i in range(2, 11, 2):
print(i)
B. for i in range(1, 11):
if i % 2 == 0:
print(i)
C. i = 2
while i <= 10:
print(i)
i += 2
D. All of the above
Solution
Step 1: Analyze each code snippet
for i in range(2, 11, 2):
print(i) uses for loop with step 2 from 2 to 10. for i in range(1, 11):
if i % 2 == 0:
print(i) uses for loop with condition to print even numbers. i = 2
while i <= 10:
print(i)
i += 2 uses while loop increasing by 2 starting at 2.
Step 2: Confirm all snippets print even numbers 2 to 10
All three snippets correctly print 2, 4, 6, 8, 10.
Final Answer:
All of the above -> Option D
Quick Check:
All methods print even numbers 2 to 10 [OK]
Hint: Multiple loop types can print even numbers correctly [OK]