|
| 1 | +{ |
| 2 | + "title": "Flow Control in Loops", |
| 3 | + "description": "", |
| 4 | + "content": [ |
| 5 | + { |
| 6 | + "type": "paragraph", |
| 7 | + "text": "In the previous unit, you learned how to use <code class=\"language-python\">while</code> and <code class=\"language-python\">for</code> to repeat instructions. Now you will learn how to modify the internal behavior of a loop while it is running." |
| 8 | + }, |
| 9 | + { |
| 10 | + "type": "paragraph", |
| 11 | + "text": "Python offers three special statements:" |
| 12 | + }, |
| 13 | + { |
| 14 | + "type": "list", |
| 15 | + "items": [ |
| 16 | + "<code class=\"language-python\">break</code>", |
| 17 | + "<code class=\"language-python\">continue</code>", |
| 18 | + "<code class=\"language-python\">pass</code>" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "type": "paragraph", |
| 23 | + "text": "These allow you to have more precise control over how and when repetitions are executed." |
| 24 | + }, |
| 25 | + { |
| 26 | + "type": "subtitle1", |
| 27 | + "text": "<code class=\"language-python\">break</code> — Stop the Loop" |
| 28 | + }, |
| 29 | + { |
| 30 | + "type": "paragraph", |
| 31 | + "text": "The <code class=\"language-python\">break</code> statement is used to immediately terminate a loop, regardless of whether the condition is still true." |
| 32 | + }, |
| 33 | + { |
| 34 | + "type": "paragraph", |
| 35 | + "text": "When Python encounters a <code class=\"language-python\">break</code>, it exits the loop and continues with the code that comes after the loop." |
| 36 | + }, |
| 37 | + { |
| 38 | + "type": "subtitle2", |
| 39 | + "text": "Example with <code class=\"language-python\">while</code>" |
| 40 | + }, |
| 41 | + { |
| 42 | + "type": "code", |
| 43 | + "text": "counter = 1\n\nwhile counter <= 10:\n\tif counter == 5:\n\t\tbreak\n\tprint(counter)\n\tcounter += 1" |
| 44 | + }, |
| 45 | + { |
| 46 | + "type": "paragraph", |
| 47 | + "text": "Output:" |
| 48 | + }, |
| 49 | + { |
| 50 | + "type": "code", |
| 51 | + "text": "1\n2\n3\n4" |
| 52 | + }, |
| 53 | + { |
| 54 | + "type": "paragraph", |
| 55 | + "text": "In this case, the loop stops when the counter reaches 5." |
| 56 | + }, |
| 57 | + { |
| 58 | + "type": "test-button", |
| 59 | + "text": "Try >>", |
| 60 | + "code": "counter = 1\n\nwhile counter <= 10:\n\tif counter == 5:\n\t\tbreak\n\tprint(counter)\n\tcounter += 1" |
| 61 | + }, |
| 62 | + { |
| 63 | + "type": "subtitle2", |
| 64 | + "text": "Example with <code class=\"language-python\">for</code>" |
| 65 | + }, |
| 66 | + { |
| 67 | + "type": "code", |
| 68 | + "text": "for letter in \"Python\":\n\tif letter == \"h\":\n\t\tbreak\n\tprint(letter)" |
| 69 | + }, |
| 70 | + { |
| 71 | + "type": "paragraph", |
| 72 | + "text": "Output:" |
| 73 | + }, |
| 74 | + { |
| 75 | + "type": "code", |
| 76 | + "text": "P\ny\nt" |
| 77 | + }, |
| 78 | + { |
| 79 | + "type": "paragraph", |
| 80 | + "text": "The loop is interrupted when it finds the letter <code class=\"language-python\">\"h\"</code>." |
| 81 | + }, |
| 82 | + { |
| 83 | + "type": "test-button", |
| 84 | + "text": "Try >>", |
| 85 | + "code": "for letter in \"Python\":\n\tif letter == \"h\":\n\t\tbreak\n\tprint(letter)" |
| 86 | + }, |
| 87 | + { |
| 88 | + "type": "note", |
| 89 | + "text": "📌 <strong>Note:</strong><br><code class=\"language-python\">break</code> completely stops the loop, not just a single iteration." |
| 90 | + }, |
| 91 | + { |
| 92 | + "type": "tip", |
| 93 | + "text": "💡 <strong>Tip:</strong><br>Use <code class=\"language-python\">break</code> when you find what you are looking for and don’t need to continue iterating." |
| 94 | + }, |
| 95 | + { |
| 96 | + "type": "subtitle1", |
| 97 | + "text": "<code class=\"language-python\">continue</code> — Skip an Iteration" |
| 98 | + }, |
| 99 | + { |
| 100 | + "type": "paragraph", |
| 101 | + "text": "The <code class=\"language-python\">continue</code> statement does not stop the loop; instead, it skips the rest of the code in the current iteration and moves directly to the next iteration." |
| 102 | + }, |
| 103 | + { |
| 104 | + "type": "paragraph", |
| 105 | + "text": "Example:" |
| 106 | + }, |
| 107 | + { |
| 108 | + "type": "code", |
| 109 | + "text": "for number in range(1, 6):\n\tif number == 3:\n\t\tcontinue\n\tprint(number)" |
| 110 | + }, |
| 111 | + { |
| 112 | + "type": "paragraph", |
| 113 | + "text": "Output:" |
| 114 | + }, |
| 115 | + { |
| 116 | + "type": "code", |
| 117 | + "text": "1\n2\n4\n5" |
| 118 | + }, |
| 119 | + { |
| 120 | + "type": "paragraph", |
| 121 | + "text": "Here, the number 3 is not printed, but the loop continues with the remaining numbers." |
| 122 | + }, |
| 123 | + { |
| 124 | + "type": "test-button", |
| 125 | + "text": "Try >>", |
| 126 | + "code": "for number in range(1, 6):\n\tif number == 3:\n\t\tcontinue\n\tprint(number)" |
| 127 | + }, |
| 128 | + { |
| 129 | + "type": "note", |
| 130 | + "text": "📌 <strong>Note:</strong><br><code class=\"language-python\">continue</code> does not end the loop; it only skips a specific iteration." |
| 131 | + }, |
| 132 | + { |
| 133 | + "type": "tip", |
| 134 | + "text": "💡 <strong>Tip:</strong><br>Use it when you want to skip certain cases within the loop without stopping it completely." |
| 135 | + }, |
| 136 | + { |
| 137 | + "type": "subtitle1", |
| 138 | + "text": "<code class=\"language-python\">pass</code> — Empty Statement" |
| 139 | + }, |
| 140 | + { |
| 141 | + "type": "paragraph", |
| 142 | + "text": "The <code class=\"language-python\">pass</code> statement does nothing. It is used when you need the structure to be complete but don’t want to implement any logic yet." |
| 143 | + }, |
| 144 | + { |
| 145 | + "type": "paragraph", |
| 146 | + "text": "Example:" |
| 147 | + }, |
| 148 | + { |
| 149 | + "type": "code", |
| 150 | + "text": "for i in range(5):\n\tpass" |
| 151 | + }, |
| 152 | + { |
| 153 | + "type": "paragraph", |
| 154 | + "text": "Output:" |
| 155 | + }, |
| 156 | + { |
| 157 | + "type": "code", |
| 158 | + "text": "" |
| 159 | + }, |
| 160 | + { |
| 161 | + "type": "test-button", |
| 162 | + "text": "Try >>", |
| 163 | + "code": "for i in range(5):\n\tpass" |
| 164 | + }, |
| 165 | + { |
| 166 | + "type": "paragraph", |
| 167 | + "text": "It is also often used with conditional statements:" |
| 168 | + }, |
| 169 | + { |
| 170 | + "type": "code", |
| 171 | + "text": "if True:\n\tpass" |
| 172 | + }, |
| 173 | + { |
| 174 | + "type": "paragraph", |
| 175 | + "text": "Output:" |
| 176 | + }, |
| 177 | + { |
| 178 | + "type": "code", |
| 179 | + "text": "" |
| 180 | + }, |
| 181 | + { |
| 182 | + "type": "test-button", |
| 183 | + "text": "Try >>", |
| 184 | + "code": "if True:\n\tpass" |
| 185 | + }, |
| 186 | + { |
| 187 | + "type": "note", |
| 188 | + "text": "📌 <strong>Note:</strong><br><code class=\"language-python\">pass</code> prevents syntax errors when a block is empty." |
| 189 | + }, |
| 190 | + { |
| 191 | + "type": "tip", |
| 192 | + "text": "💡 <strong>Tip:</strong><br>It is useful when you are planning your program and haven’t decided yet what code will go there." |
| 193 | + }, |
| 194 | + { |
| 195 | + "type": "subtitle1", |
| 196 | + "text": "Comparison of the Three Statements" |
| 197 | + }, |
| 198 | + { |
| 199 | + "type": "table", |
| 200 | + "headers": [ |
| 201 | + "Instrucción", |
| 202 | + "Stops the Loop?", |
| 203 | + "Skips an Iteration?", |
| 204 | + "Does Something Visible?" |
| 205 | + ], |
| 206 | + "rows": [ |
| 207 | + ["<code class=\"language-python\">break</code>", "Yes", "No", "Yes"], |
| 208 | + ["<code class=\"language-python\">continue</code>", "No", "Yes", "Yes"], |
| 209 | + ["<code class=\"language-python\">pass</code>", "No", "No", "No"] |
| 210 | + ] |
| 211 | + }, |
| 212 | + { |
| 213 | + "type": "subtitle1", |
| 214 | + "text": "Combined Examples" |
| 215 | + }, |
| 216 | + { |
| 217 | + "type": "subtitle2", |
| 218 | + "text": "Example 1: Searching for a Specific Letter" |
| 219 | + }, |
| 220 | + { |
| 221 | + "type": "code", |
| 222 | + "text": "for letter in \"Programming\":\n\tif letter == \"g\":\n\t\tprint(\"Letter found\")\n\t\tbreak" |
| 223 | + }, |
| 224 | + { |
| 225 | + "type": "paragraph", |
| 226 | + "text": "Output:" |
| 227 | + }, |
| 228 | + { |
| 229 | + "type": "code", |
| 230 | + "text": "Letter found" |
| 231 | + }, |
| 232 | + { |
| 233 | + "type": "test-button", |
| 234 | + "text": "Try >>", |
| 235 | + "code": "for letter in \"Programming\":\n\tif letter == \"g\":\n\t\tprint(\"Letter found\")\n\t\tbreak" |
| 236 | + }, |
| 237 | + { |
| 238 | + "type": "subtitle2", |
| 239 | + "text": "Example 2: Display Only Odd Numbers" |
| 240 | + }, |
| 241 | + { |
| 242 | + "type": "code", |
| 243 | + "text": "for number in range(1, 10):\n\tif number % 2 == 0:\n\t\tcontinue\n\tprint(number)" |
| 244 | + }, |
| 245 | + { |
| 246 | + "type": "paragraph", |
| 247 | + "text": "Output:" |
| 248 | + }, |
| 249 | + { |
| 250 | + "type": "code", |
| 251 | + "text": "1\n3\n5\n7\n9" |
| 252 | + }, |
| 253 | + { |
| 254 | + "type": "test-button", |
| 255 | + "text": "Try >>", |
| 256 | + "code": "for number in range(1, 10):\n\tif number % 2 == 0:\n\t\tcontinue\n\tprint(number)" |
| 257 | + }, |
| 258 | + { |
| 259 | + "type": "subtitle1", |
| 260 | + "text": "Practice Exercises" |
| 261 | + }, |
| 262 | + { |
| 263 | + "type": "subtitle2", |
| 264 | + "text": "Exercise 1: Stop When a Number Is Found" |
| 265 | + }, |
| 266 | + { |
| 267 | + "type": "paragraph", |
| 268 | + "text": "Create bucle <code class=\"language-pythin\">for</code> loop that goes through the numbers from 1 to 10.<br>When the number is 7, the program should completely stop the loop using <code class=\"language-python\">break</code>.<br>Print each number before the loop stops." |
| 269 | + }, |
| 270 | + { |
| 271 | + "type": "test-button", |
| 272 | + "text": "Practice >>", |
| 273 | + "code": "# Create bucle for loop that goes through the numbers\n# from 1 to 10.\n# When the number is 7, the program should completely\n# stop the loop using break.\n# Print each number before the loop stops" |
| 274 | + }, |
| 275 | + { |
| 276 | + "type": "collapsible", |
| 277 | + "title": "View possible solution", |
| 278 | + "content": [ |
| 279 | + { |
| 280 | + "type": "code", |
| 281 | + "text": "for number in range(1, 11):\n\tif number == 7:\n\t\tbreak\n\tprint(number)" |
| 282 | + }, |
| 283 | + { |
| 284 | + "type": "test-button", |
| 285 | + "text": "Try Solution >>", |
| 286 | + "code": "for number in range(1, 11):\n\tif number == 7:\n\t\tbreak\n\tprint(number)" |
| 287 | + } |
| 288 | + ] |
| 289 | + }, |
| 290 | + { |
| 291 | + "type": "subtitle2", |
| 292 | + "text": "Exercise 2: Skip a Specific Letter" |
| 293 | + }, |
| 294 | + { |
| 295 | + "type": "paragraph", |
| 296 | + "text": "Create a variable called <code class=\"language-python\">word</code> with the value <code class=\"language-python\">\"computer\"</code>.<br>Use a <code class=\"language-python\">for</code> loop to display each letter, except the letter <code class=\"language-python\">\"o\"</code>.<br>Use <code class=\"language-python\">continue</code>." |
| 297 | + }, |
| 298 | + { |
| 299 | + "type": "test-button", |
| 300 | + "text": "Practice >>", |
| 301 | + "code": "# Create a variable called word with the value\n# \"computer\".\n# Use a for loop to display each letter, except the\n# letter \"o\".\n# Use continue" |
| 302 | + }, |
| 303 | + { |
| 304 | + "type": "collapsible", |
| 305 | + "title": "View possible solution", |
| 306 | + "content": [ |
| 307 | + { |
| 308 | + "type": "code", |
| 309 | + "text": "word = \"computer\"\n\nfor letter in word:\n\tif letter == \"o\":\n\t\tcontinue\n\tprint(letter)" |
| 310 | + }, |
| 311 | + { |
| 312 | + "type": "test-button", |
| 313 | + "text": "Try Solution >>", |
| 314 | + "code": "word = \"computer\"\n\nfor letter in word:\n\tif letter == \"o\":\n\t\tcontinue\n\tprint(letter)" |
| 315 | + } |
| 316 | + ] |
| 317 | + }, |
| 318 | + { |
| 319 | + "type": "subtitle2", |
| 320 | + "text": "Exercise 3: Detect Negative Number" |
| 321 | + }, |
| 322 | + { |
| 323 | + "type": "paragraph", |
| 324 | + "text": "Create a simulated list using <code class=\"language-python\">range()</code> from -3 to 3.<br>Loop through the values using a <code class=\"language-python\">for</code> loop.<br>If you find a negative number, print <code class=\"language-python\">\"Negative number detected\"</code> and stop the loop using <code class=\"language-python\">break</code>." |
| 325 | + }, |
| 326 | + { |
| 327 | + "type": "test-button", |
| 328 | + "text": "Practice >>", |
| 329 | + "code": "# Create a simulated list using range() from -3 to 3.\n# Loop through the values using a for loop.\n# If you find a negative number, print \"Negative number\n# detected\" and stop the loop using break" |
| 330 | + }, |
| 331 | + { |
| 332 | + "type": "collapsible", |
| 333 | + "title": "View possible solution", |
| 334 | + "content": [ |
| 335 | + { |
| 336 | + "type": "code", |
| 337 | + "text": "for number in range(-3, 4):\n\tif number < 0:\n\t\tprint(\"Negative number detected\")\n\t\tbreak" |
| 338 | + }, |
| 339 | + { |
| 340 | + "type": "test-button", |
| 341 | + "text": "Try Solution >>", |
| 342 | + "code": "for number in range(-3, 4):\n\tif number < 0:\n\t\tprint(\"Negative number detected\")\n\t\tbreak" |
| 343 | + } |
| 344 | + ] |
| 345 | + }, |
| 346 | + { |
| 347 | + "type": "subtitle2", |
| 348 | + "text": "Exercise 4: Incomplete Structure With <code class=\"language-python\">pass</code>" |
| 349 | + }, |
| 350 | + { |
| 351 | + "type": "paragraph", |
| 352 | + "text": "Create <code class=\"language-python\">for</code> loop that iterates through the numbers from 1 to 5.<br>Inside the loop, add an <code class=\"language-python\">if</code> condition that checks whether the number is greater than 3.<br>For now, do not perform any action inside that condition; use <code class=\"language-python\">pass</code>." |
| 353 | + }, |
| 354 | + { |
| 355 | + "type": "test-button", |
| 356 | + "text": "Practice >>", |
| 357 | + "code": "# Create for loop that iterates through the numbers\n# from 1 to 5.\n# Inside the loop, add an if condition that checks\n# whether the number is greater than 3.\n# For now, do not perform any action inside that\n# condition; use pass" |
| 358 | + }, |
| 359 | + { |
| 360 | + "type": "collapsible", |
| 361 | + "title": "View possible solution", |
| 362 | + "content": [ |
| 363 | + { |
| 364 | + "type": "code", |
| 365 | + "text": "for number in range(1, 6):\n\tif number > 3:\n\t\tpass\n\tprint(number)" |
| 366 | + }, |
| 367 | + { |
| 368 | + "type": "test-button", |
| 369 | + "text": "Try Solution >>", |
| 370 | + "code": "for number in range(1, 6):\n\tif number > 3:\n\t\tpass\n\tprint(number)" |
| 371 | + } |
| 372 | + ] |
| 373 | + }, |
| 374 | + { |
| 375 | + "type": "subtitle1", |
| 376 | + "text": "Unit Conclusions" |
| 377 | + }, |
| 378 | + { |
| 379 | + "type": "paragraph", |
| 380 | + "text": "In this unit, you learned that:" |
| 381 | + }, |
| 382 | + { |
| 383 | + "type": "list", |
| 384 | + "items": [ |
| 385 | + "<code class=\"language-python\">break</code> allows you to stop a loop immediately.", |
| 386 | + "<code class=\"language-python\">continue</code> skips a specific iteration without ending the loop.", |
| 387 | + "<code class=\"language-python\">pass</code> acts as an empty statement to keep structures syntactically valid.", |
| 388 | + "These tools provide greater control and flexibility within loops.", |
| 389 | + "Knowing when to use each one improves the clarity and efficiency of your program." |
| 390 | + ] |
| 391 | + } |
| 392 | + ] |
| 393 | +} |
0 commit comments