Iterative Statements Complete Notes
Iterative Statements Complete Notes
**What is Iteration?**
---
1. **For Loop**
- **Syntax:**
```python
```
- **Parameters of range():**
- **Example:**
```python
```
**Output:** 1, 2, 3, 4, 5
---
2. **While Loop**
- Used when the number of iterations is not fixed and depends on a condition.
- **Syntax:**
```python
while condition:
```
- **Example:**
```python
count = 1
print(count)
count += 1
```
**Output:** 1, 2, 3, 4, 5
- **Use Case:** Waiting for user input, processing data until a condition is met.
---
3. **Nested Loops**
- **Example:**
```python
print(f"i={i}, j={j}")
```
---
1. **Break Statement**
- **Example:**
```python
for i in range(5):
if i == 3:
break
print(i)
```
**Output:** 0, 1, 2
2. **Continue Statement**
- Skips the current iteration and proceeds to the next.
- **Example:**
```python
for i in range(5):
if i == 2:
continue
print(i)
```
**Output:** 0, 1, 3, 4
- **Example:**
```python
for i in range(3):
print(i)
else:
print("Loop finished!")
```
---
```python
if i % 2 == 0:
print(f"{i} is even")
else:
print(f"{i} is odd")
```
2. **Infinite Loops**
- **Example:**
```python
while True:
```
- **Example:**
```python
if j == 2:
break
print(f"i={i}, j={j}")
```
---