Knowledge Card - Loop Types
Loop Types
-
For Loop: Repeats a block of code a specific number of times.
- Example:
for i in range(5): print(i)
prints numbers from0
to4
.
- Example:
-
While Loop: Repeats a block of code while a condition is true.
- Example:
while x < 10: x += 1
incrementsx
until it reaches10
.
- Example:
-
Nested Loops: Loops inside other loops.
- Example:
for i in range(3): for j in range(2): print(i, j)
creates a grid of values.
- Example: