Quiz: For and While Loop

Practice Python's for and while loops. This quiz covers loop syntax, break, continue, range(), else, and nested loop logic with real-world examples.

1.

What is the primary purpose of a for loop in Python?

2.

What will this code output?

python
1
2
3
vegetables = ["carrot", "potato", "tomato"]  
for vegetable in vegetables:  
    print(vegetable)
3.

Which keyword is used to exit a loop early?

4.

What does the continue keyword do in a loop?

5.

How many times will this loop run?

python
1
2
3
4
counter = 1
while counter < 4:
    print(counter)
    counter += 1