Interview Question

Difference between for and while loop?

for iterates over values; while repeats according to a condition.

💡 Concept ✅ Quick Revision 🐍 Python

Answer

A for loop consumes items from an iterable, while a while loop tests a condition before each iteration. • Use for when iteration follows an iterable or known sequence. • Use while when repetition depends on changing state. • Both support break, continue, and an else clause.

💡 Simple Example

items = ['a', 'b'] for item in items: print(item) count = 0 while count < 2: count += 1

Output

a b

⚡ Quick Revision

for iterates over values; while repeats according to a condition.