{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# If statements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What we know so far\n", "\n", "- Expressions\n", "- Variables\n", "- Print Statements\n", "- Types\n", "\n", "We can already do a lot with what we know so far. But what if we wanted to make something that is adaptable to specific situations?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Making Decisions: if, elif, and else\n", "\n", "- How do we compute absolute values?\n", "\n", "**if** the value is *negative*, negate it\n", "\n", "**Otherwise** (else), use the original value" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = -10\n", "\n", "# calculate absolute value of x\n", "\n", "if x < 0:\n", " result = -x\n", "else:\n", " result = x\n", "\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try editing the above code to see if it works for any number! Here are some other ways of writing the same if statement" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = -10\n", "\n", "if x < 0:\n", " print(-x)\n", "else:\n", " print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What are some important components of the above if statements?\n", "\n", "* `val < 0`: This is called a *condition* and must be a boolean expression.\n", "* `result = -x` or `print(-val)`: These lines of code are only run *if* the conditon is true. They must be indented from the if statement.\n", "* `else`: Code to run if the condition is False. This is not required." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multiple if statements can be used inside each other. These are called *nested if statements*" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 0\n", "\n", "# calculate absolute value of x\n", "if x < 0:\n", " print(\"x is negative\")\n", " result = -x\n", "else:\n", " if x == 0:\n", " print(\"x is zero\")\n", " result = 0\n", " else:\n", " print(\"x is positive\")\n", " result = x\n", "\n", "print(x)\n", "print(result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, nested if statements can be unwieldy especially when you have a lot of conditions. In these cases, you can use *elif* to specify more conditions besides the initial *if*." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 0\n", "\n", "# calculate absolute value of val\n", "if x < 0:\n", " print(\"x is negative\")\n", " result = -x\n", "elif x == 0:\n", " print(\"x is zero\")\n", " result = 0\n", "else:\n", " print(\"x is positive\")\n", " result = x\n", "\n", "print(x)\n", "print(result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that all of the `elif`s and `else` should follow the same indentation rules as the first `if`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More Examples" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Version 1\n", "\n", "height = 72\n", "if height > 100:\n", " print(\"space\")\n", "else:\n", " if height > 50:\n", " print(\"mesosphere\")\n", " else:\n", " if height > 20:\n", " print(\"stratosphere\")\n", " else:\n", " print(\"troposphere\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Version 2\n", "\n", "if height > 50:\n", " if height > 100:\n", " print(\"space\")\n", " else:\n", " print(\"mesosphere\")\n", "else:\n", " if height > 20:\n", " print(\"stratosphere\")\n", " else:\n", " print(\"troposphere\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Version 3 (Best)\n", "\n", "if height > 100:\n", " print(\"space\")\n", "elif height > 50:\n", " print(\"mesosphere\")\n", "elif height > 20:\n", " print(\"stratosphere\")\n", "else:\n", " print(\"troposphere\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Broken Version 3\n", "\n", "if height > 20:\n", " print(\"stratosphere\")\n", "elif height > 50:\n", " print(\"mesosphere\")\n", "elif height > 100:\n", " print(\"space\")\n", "else:\n", " print(\"troposphere\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Why does `height = 72` give different results?\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Incomplete Version 3\n", "\n", "if height > 20:\n", " print(\"stratosphere\")\n", "elif height > 50:\n", " print(\"mesosphere\")\n", "elif height > 100:\n", " print(\"space\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case it is possible that nothing is printed at all, when?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if height > 100:\n", " print(\"space\")\n", "if height > 50:\n", " print(\"mesosphere\")\n", "if height > 20:\n", " print(\"stratosphere\")\n", "else:\n", " print(\"troposphere\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now what happens when `height` is 72? Or when `height` is 101?" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 2 }