{"assignment":{"_schema_version":2,"course_id":37,"date_created":"2022-06-28T19:00:00+00:00","date_modified":"2023-09-10T14:19:34.949218+00:00","extra_instructor_files":"","extra_starting_files":"","forked_id":null,"forked_version":null,"hidden":false,"id":1131,"instructions":"## Another Kind of Iteration\n\n```python\nwhile condition:\n    pass\n```\n\n`for` loops allow us to iterate through each element of a list.\nThey are the most common form of iteration in Python.\nHowever, there is another kind of iteration that is useful called `while` loops.\nThese loops allow us to iterate until any kind of condition that we want is met.\n\n\n## Syntax\n\n![A while loop is annotated with the terms \"While keyword\", \"Conditional\", \"Colon\", and \"Body\":](bakery_time_while_loop_syntax.png)\n\nThis diagram explains the pieces of a `while` loop.\nNotice how the syntax for a `while` loop is like that of an `if` statement.\nWe have the word `while`, followed by a condition, ending with a colon.\nThen, we can have any number of statements inside the body.\nRemember that this body, like a FOR loop or an IF statement, is indented with 4 spaces.\n\n## A Simple Example\n\n```python runnable-while\ncount_down = 4\n\nwhile count_down > 0:\n    count_down -= 1\n    print(count_down)\n    \nprint(\"Reached the end\")\n```\n\nIn this runnable example, you can see initialization before the loop, the condition of the loop, the body of the loop, and then output after the loop is complete.\nMost `while` loops need some kind of variable that is defined before the loop, used in the conditional, and updated inside the loop.\nOtherwise, the loop will continue forever.\nIn this example, we are repeatedly decreasing the `count_down` variable, printing its value each time, until the variable's value is less than or equal to zero.\n\n## Like IF Statements\n\n![An arrow traces the flow in the code below, with a branch at the end of the loop's body: one branch moves back up to the start of the loop, and the other moves further downward.](bakery_time_while_loop_flow.png)\n\n`while` loops are actually very similar to `if` statements.\nHowever, instead of executing once or none, they will execute repeatedly until the condition is false.\nNote that a loop won't end until the condition evaluates to false; even if the condition would be false during the loop body, the loop can't end until the end of the body (when the condition is checked again.)\n\n## Tracing the Flow\n\n![The code from before is shown with arrows indicating how many time each chunk of code executes, with the `while` loop arrow indicating it loops four times.](bakery_time_while_tracing_the_flow.png)\n\nWhen a `while` loop is executed, the program follows a similar looping behavior as a `for` loop.\nIn the previous example, we can see that behavior in a `while` loop to count down from 4.\nThe program tests the condition, and if it is true, it moves through each statement in the body.\nThen, it returns to the top and tests the condition again.\nIf it is still true, it repeats the previous loop.\nOtherwise, if the condition is ever false, before the body is executed the first time, the program skips to the end of the `while` body.\nAgain, remember that the condition is only checked at the start of a new iteration of the loop\u2014never during the middle of the loop's body's execution.\n\n## Looping Forever\n\n![An annotation points out that the first line of the loop only increases the variable, so it never reaches 0.](bakery_time_while_loop_bad.png)\n\nA tricky thing about WHILE loops is that it is easy to loop forever by accident.\nConsider the incorrect code here.\nBecause the variable `count_down` is only ever increasing, it will never reach zero and the loop will never end.\n\nWhat happens when code loops forever? Eventually, the system might try to stop the execution.\nHowever, depending on the loop, the system might freeze up instead, which makes it very hard to fix the mistake.\nThe system might also run out of memory, which can cause the environment to crash.\nInfinite loops are a very serious kind of problem.\n\n## Unnecessary While Loops\n\n```python while-vs-for\n# While loop version\nindex = 0\nwhile index < 10:\n    index += 1\n    print(index)\n\n# For loop version\nfor index in range(10):\n    print(index)\n```\n\nA major advantage of `for` Loops instead of `while` Loops is that they terminate based on a list, so they are very unlikely to loop forever.\nWhen possible, you should probably use a `for` loop instead of a `while` loop.\nAs an example, consider the counting code shown here.\nThat same code can be efficiently replaced with a `for` loop that calls the `range` function to achieve the same effect.\n\n## Unexecuted While Loops\n\n```python unexecuted-while\ncount_down = 0\nwhile count_down:\n    count_down -= 1\n\nprint(count_down)\n```\n\nAnother common mistake with a `while` loop is choosing a condition that is initially false.\nSince `while` loops begin by checking their condition, having a `False` value means the loop will never execute.\nMost of the time, this indicates that you made a mistake since you usually want the loop to execute at least once.\n\n## Power of While\n\n- **For loops:** A safe way to iterate a limited number of times over a list or range.\n- **While loops:** A powerful way to iterate an infinite number of times so long as a condition is true.\n\nSo why would you want to have `while` loops if `for` loops are so convenient and safe? The tradeoff is that `while` loops can handle more forms of loops.\nBecause a `while` loop can work with an arbitrary condition, it is not limited to the length of a list or a known range of numbers.\nWhile a `for` loop is a kind of finite iteration, a `while` loop can potentially last infinitely and iterate in other ways.\nAlthough there are advanced techniques to make `for` loops more powerful, the reality is that many kinds of problems are more naturally solved with a `while` than with a `for` loop.\nThat said, the real power lies in knowing when to use one or the other.\nIf all you need to do is go through a list or a range of numbers, stick with `for` loops.\nIn the next section, we will learn other uses for `while` loops.\n\n## Summary\n\n- A `while` loop is another control structure that allows you to repeat a body of code multiple times.\n- The body of the `while` loop will be repeatedly executed as long as the conditional of the `while` loop is true.\n- The conditional of the `while` loop is checked at the start of each iteration but not during the execution of the body\u2014only once the body is finished.\n- A `while` loop can execute zero times (if the conditional is initially false) and infinite times (if the conditional never becomes false.)\n- All `for` loops can be replaced by an equivalent `while` loop, but not all `while` loops can be replaced by a `for` loop.\n- However, usually, if you can replace a `while` loop with a `for` loop, that is preferable; a `for` loop is a safer form of iteration that has fewer opportunities to loop forever or incorrectly.","ip_ranges":"","name":"10A1) While Loops Reading","on_change":"","on_eval":"","on_run":"","owner_id":1,"owner_id__email":"acbart@udel.edu","points":0,"public":true,"reviewed":false,"sample_submissions":[],"settings":"{\n  \"header\": \"While Loops\",\n  \"youtube\": {\n    \"Bart\": \"VA64IYVPPUU\",\n    \"Amy\": \"-Voqgfi7UxE\"\n  },\n  \"video\": {\n    \"Bart\": \"https://blockpy.cis.udel.edu/videos/bakery_time_while-Bart.mp4\",\n    \"Amy\": \"https://blockpy.cis.udel.edu/videos/bakery_time_while-Amy.mp4\"\n  },\n  \"small_layout\": true,\n  \"summary\": \"In this lesson, you will learn about a new kind of loop called WHILE loops. These loops are somewhat different from FOR loops, and in fact act more like IF statements. Although more powerful than FOR loops, they have some disadvantages - we will use them sparingly.\"\n}","starting_code":"","subordinate":true,"tags":[],"type":"reading","url":"bakery_time_while_read","version":8},"ip":"216.73.216.157","submission":{"_schema_version":3,"assignment_id":1131,"assignment_version":8,"attempts":0,"code":"","correct":false,"course_id":37,"date_created":"2026-05-20T12:42:00.117158+00:00","date_due":"","date_graded":"","date_locked":"","date_modified":"2026-05-20T12:42:00.117158+00:00","date_started":"","date_submitted":"","endpoint":"","extra_files":"","feedback":"","grading_status":"NotReady","id":2036758,"score":0.0,"submission_status":"Started","time_limit":"","url":"submission_url-f2713565-d359-41c9-94b2-b85625236711","user_id":2044660,"user_id__email":"","version":0},"success":true}
