|
| 1 | +# 🗓️ Day 12 — Iterative Statements (While Loop) |
| 2 | + |
| 3 | +## 🎯 Topics Covered |
| 4 | +- What are Iterative Statements? |
| 5 | +- Understanding the `while` Loop |
| 6 | +- Key Steps: |
| 7 | + - 1. Initialization, |
| 8 | + - 2. Condition, |
| 9 | + - 3. Updating. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## 🔁 What are Iterative Statements? |
| 14 | + |
| 15 | +Iterative statements are used to **repeat a block of code multiple times** based on a condition. |
| 16 | +In Python, we mainly use two types of loops: |
| 17 | +- `while` loop |
| 18 | +- `for` loop (will be covered later) |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## 🌀 The `while` Loop |
| 23 | + |
| 24 | +The **`while` loop** executes a block of code **as long as the given condition is True**. |
| 25 | + |
| 26 | +### 🧠 Syntax: |
| 27 | +```python |
| 28 | +while condition: |
| 29 | + # block of code |
| 30 | +``` |
| 31 | + |
| 32 | +--- |
| 33 | + |
| 34 | +### ⚙️ Components of a While Loop |
| 35 | + |
| 36 | +A while loop generally has three main parts: |
| 37 | + |
| 38 | +| Step | Description | Example | |
| 39 | +| --------------------- | ------------------------------------------------------ | ----------------------- | |
| 40 | +| **1. Initialization** | Assign starting value to variable | `i = 1` | |
| 41 | +| **2. Condition** | Loop continues until condition becomes False | `while i <= 5:` | |
| 42 | +| **3. Updating** | Change variable value to move toward stopping the loop | `i = i + 1` or `i += 1` | |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +### 💻 Example — Print Numbers from 1 to 5 |
| 47 | +```python |
| 48 | +# Initialization |
| 49 | +i = 1 |
| 50 | + |
| 51 | +# Condition |
| 52 | +while i <= 5: |
| 53 | + print(i) |
| 54 | + |
| 55 | + # Updating |
| 56 | + i += 1 |
| 57 | +``` |
| 58 | + |
| 59 | +### 🧠 Output: |
| 60 | +```python |
| 61 | +1 |
| 62 | +2 |
| 63 | +3 |
| 64 | +4 |
| 65 | +5 |
| 66 | +``` |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +### ✅ Explanation: |
| 71 | + |
| 72 | +- i starts at 1. |
| 73 | +- The condition i <= 5 is checked each time. |
| 74 | +- After printing, i is increased by 1. |
| 75 | +- When i becomes 6, the condition fails, and the loop stops. |
| 76 | + |
| 77 | +--- |
| 78 | + |
| 79 | +## ⚠️ Infinite Loop Example |
| 80 | + |
| 81 | +If you forget to update the loop variable, it will run forever. |
| 82 | + |
| 83 | +### 💻 Example: |
| 84 | +```python |
| 85 | +x = 1 |
| 86 | + |
| 87 | +while x <= 5: |
| 88 | + print("Infinite Loop!") |
| 89 | + # Missing update (x += 1) |
| 90 | +``` |
| 91 | + |
| 92 | +### 🧾 Output: |
| 93 | + |
| 94 | +This loop runs endlessly, because x never changes, and the condition x <= 5 is always True. |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +## 🧩 Using Break in While Loop |
| 99 | + |
| 100 | +You can stop a loop anytime using the break statement. |
| 101 | + |
| 102 | +### 💻 Example: |
| 103 | +```python |
| 104 | +i = 1 |
| 105 | + |
| 106 | +while i <= 10: |
| 107 | + if i == 6: |
| 108 | + break |
| 109 | + print(i) |
| 110 | + i += 1 |
| 111 | +``` |
| 112 | + |
| 113 | +### 🧠 Output: |
| 114 | +```python |
| 115 | +1 |
| 116 | +2 |
| 117 | +3 |
| 118 | +4 |
| 119 | +5 |
| 120 | +``` |
| 121 | +✅ The loop stops when i becomes 6. |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +## 🧠 Summary |
| 126 | + |
| 127 | +| Concept | Description | Example | |
| 128 | +| -------------- | ------------------------------- | --------------- | |
| 129 | +| Initialization | Variable setup before loop | `i = 1` | |
| 130 | +| Condition | Checked before each iteration | `while i <= 5:` | |
| 131 | +| Updating | Increment/decrement inside loop | `i += 1` | |
| 132 | +| Infinite Loop | Loop without update | `while True:` | |
| 133 | +| Break | Stops loop immediately | `break` | |
| 134 | + |
| 135 | +--- |
| 136 | + |
| 137 | +📘 *Next step (Day 13):* |
| 138 | + |
| 139 | +I’ll explore **Control Structure - Iterative Statements (for loop)**. |
0 commit comments