{"assignment":{"_schema_version":2,"course_id":37,"date_created":"2022-06-28T19:00:00+00:00","date_modified":"2025-08-17T17:55:03.737624+00:00","extra_instructor_files":"","extra_starting_files":"","forked_id":null,"forked_version":null,"hidden":false,"id":1019,"instructions":"## The Idea of If Statements\n\n![A box labelled \"IF ___\" has two arrows coming out. One arrow points to a box labelled \"THEN do A\" and another arrow points to another box labelled \"THEN do B\". Both of those boxes have arrows that converge on a fourth box labelled \"Continue...\".](bakery_if_syntax_overview.png)\n\nIf an expression is true, then take some action.\nOtherwise, don't take that action.\nEither way, execution continues afterwards.\nThat's the core idea of an IF statement.\n\n## Purpose\n\n![Three lines of text with pictures. The first line reads \"IF\" followed by a raining cloud. The second line reads \"THEN bring\" followed by an umbrella. The third line reads \"OTHERWISE do not.\"](bakery_if_syntax_weather.png)\n\n`if` statements make code smarter.\nThey let you do different things in different situations.\nIn this example, we decide to bring an umbrella or not based on whether it is raining.\nThat's a simple but logical decision that we can make.\n\n## Syntax\n\n![An `if` statement is shown, with the components labelled as \"if keyword\", \"conditional\", \"colon\", \"indentation\", and \"body\".](bakery_if_syntax_diagrammed.png)\n\nThere are four parts to an `if` statement.\nFirst, you have the keyword `if`.\nThen, you have the **conditional expression**.\nThe conditional is followed by a colon.\nThese first three parts together are called the **header**.\nFinally, you have the **body** of the `if` statement.\n\n## Conditionals\n\n```python\nif age > 21:\n    pass\n\nif \"cat\" in animal_name:\n    pass\n\nif rent > 2000 and rooms < 2:\n    pass\n\nif not on_fire():\n    pass\n```\n\nYou already learned about writing conditionals.\nConditional expressions can be variables, comparisons, function calls, or combinations using `and`, `or`, and `not`.\nIn the code shown here, you can see quite a few different kinds of valid conditional expressions.\n\n## Body\n\n```python example-body\nincome = int(input(\"Your income?\"))\n\nif income > 2000:\n    # Body starts here, indented\n    tax = .8\n    adjusted = income * tax\n    print(\"Income:\", adjusted)\n    # Body ends here\n\nprint(\"This line is not in the body!\")\n```\n\nThe body of an `if` statement is a sequence of one or more statements.\nThis body must be indented with **4 spaces**.\nAnything indented below the `if` keyword is said to be \"inside\" the `if` statement.\nThese indented statements will be executed if the conditional evaluates to `True`.\nOtherwise, Python will skip right over those lines of code.\n\n## The \"Else\" block\n\n```python else-example\nprecipitation = 0\n\nif precipitation > 0:\n    print(\"It is raining\")\nelse:\n    print(\"It is NOT raining\")\n```\n\n`if` statements can optionally have an `else` body.\nIf the conditional evaluates to `False`, then the `else` will be executed instead of the `if` body.\nThe `if` body will be skipped over as if its lines of code did not exist.\nOnly one of the two bodies will be executed! Note the colon after the word `else` and that it has its own body.\n\n## Branching\n\n![A program with two `if` statements is shown. In both `if` statements, two lines extend from the top of the control structure down to the bottom. In the first block, one of the lines goes straight down, while the other curves inward to follow the indentation. In the second block, one of the lines goes to the first indented area, while the second line moves down to the second indented area; both lines converge back to the final point. These demonstrate how the program might skip over or go through the body.](bakery_if_syntax_branching.png)\n\nThink about your program as a flowing stream.\nNormally it will go from top to bottom.\nAn `if` statement changes that flow to optionally go around.\nWe call that behavior _branching_.\nEvery time you have another `if` statement, you have another two branches.\nThis also happens if you have an `else` statement.\n\n## Trace Tables\n\n![A program is shown on the left, with a trace table on the right showing off its values.](bakery_if_syntax_trace_tables.png)\n\nLast chapter, we learned how you can make a trace table that follows the execution of a program.\nWe now must add an extra column to this table to differentiate between steps and lines.\nWith IF statements, it is possible that a step may skip a line of code.\nEven though the program has 6 lines of actual code, it only has 4 steps! Notice that on lines with `if` statements, we still have a step, but we simply repeat the values unchanged since the values are only read, not written.\n\n## Possible Initialization Problem\n\n```python\n# Current hour of day\nhour = 7\n\nif hour > 6:\n    time = \"It is daylight\"\n\n# This will cause an error!\n# Did not initialize time on both branches\nprint(time)\n```\n\nKeep in mind that `if` statements allow us to potentially skip lines of code.\nWe have previously seen that we must be careful to write variables before we read them.\nIf we initialize a variable in one branch, then we must initialize that variable in the other branch.\nOr, we should have already initialized the variable earlier in the program and now only update the variable.\n\n## Summary\n\n- `if` statements allow us to execute some lines of code and skip others depending on a conditional expression.\n- An `if` statement is composed of the **header** (`if` keyword, a conditional expression, a colon), and a body.\n- `else` bodies can be attached to `if` statements to run alternative code when the `if` statement evaluates to `False`.\n- Every `if` statement, regardless of whether it has an `else` body, will have at least two branches.\n- You must be careful tracing an `if` statement since some lines can be skipped; this is especially important to make sure that variables are defined along every branch of the program.\n- The body of an `if` statement are lines of code, each indented with four spaces. We say those indented lines are \"inside\" of the body, while any unindented lines are \"after\" the body.\n- The body of an `if` or `else` statement can never be empty; there must always be at least one line.\n\n","ip_ranges":"","name":"3A1) If Statements 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\": \"If Statements\",\n  \"slides\": \"bakery_if_syntax.pdf\",\n  \"youtube\": {\n    \"Bart\": \"wwIRRH78qbA\",\n    \"Amy\": \"FePlwjuZ_3Y\"\n  },\n  \"video\": {\n    \"Bart\": \"https://blockpy.cis.udel.edu/videos/bakery_if_syntax-Bart.mp4\",\n    \"Amy\": \"https://blockpy.cis.udel.edu/videos/bakery_if_syntax-Amy.mp4\"\n  },\n  \"summary\": \"In this lesson, you will learn about IF statements. These affect the flow of your program so that you can do different things at different times. In other words, IF statements make your program smart and adaptable to different situations.\",\n  \"small_layout\": true\n}","starting_code":"","subordinate":true,"tags":[],"type":"reading","url":"bakery_if_syntax_read","version":8},"ip":"216.73.216.157","submission":{"_schema_version":3,"assignment_id":1019,"assignment_version":8,"attempts":0,"code":"","correct":false,"course_id":37,"date_created":"2026-05-20T14:01:49.395266+00:00","date_due":"","date_graded":"","date_locked":"","date_modified":"2026-05-20T14:01:49.395266+00:00","date_started":"","date_submitted":"","endpoint":"","extra_files":"","feedback":"","grading_status":"NotReady","id":2036919,"score":0.0,"submission_status":"Started","time_limit":"","url":"submission_url-4ca5abef-4d3a-4ad7-aa74-86ebc06e5535","user_id":2044668,"user_id__email":"","version":0},"success":true}
