{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<small><small><i>\n",
    "All the IPython Notebooks in **Python Flow Control Statements** lecture series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/03_Python_Flow_Control)**\n",
    "</i></small></small>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python `if` statement\n",
    "\n",
    "In control statements, The **`if`** statement is the simplest form. It takes a condition and evaluates to either **`True`** or **`False`**.\n",
    "\n",
    "If the condition is **`True`**, then the **`True`** block of code will be executed, and if the condition is **`False`**, then the block of code is skipped, and The controller moves to the next line."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Syntax:\n",
    "\n",
    "```python\n",
    "if(condition):\n",
    "    statement 1\n",
    "    statement 2\n",
    "    statement n\n",
    "```\n",
    "\n",
    "* An **`if`** statement should be preceded by the keyword **`if`** and ended with a colon **`:`**. The condition of an if statement can be written with or without round brackets **`()`**.\n",
    "\n",
    "* In Python, the body of the **`if`** statement is indicated by the indentation. The body starts with an indentation and the first unindented line marks the end.\n",
    "\n",
    "* Python interprets non-zero values as **`True`**. **`None`** and **`0`** are interpreted as **`False`**.\n",
    "\n",
    "<div>\n",
    "<img src=\"img/if2.png\" width=\"250\"/>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Example: of `if` statement:**\n",
    "\n",
    "```python\n",
    ">>> grade = 70\n",
    ">>> if grade >= 65:\n",
    ">>>     print(\"Passing grade\")\n",
    "\n",
    "Passing grade\n",
    "```\n",
    "\n",
    "With this code, we have the variable grade and are giving it the integer value of 70. We are then using the **`if`** statement to evaluate whether or not the variable grade is **greater than or equal `>=`** to 65. If it does meet this **condition**, we are telling the program to print out the string Passing grade. \n",
    "\n",
    "Again, if we change the grade to 50 or < 65 number, we will receive no output."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-10T20:03:33.690723Z",
     "start_time": "2021-06-10T20:03:33.674172Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Passing grade\n"
     ]
    }
   ],
   "source": [
    "# Example 1:\n",
    "grade = 70\n",
    "if grade >= 65:\n",
    "    print(\"Passing grade\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-10T20:03:34.440566Z",
     "start_time": "2021-06-10T20:03:34.422066Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 is a positive number.\n",
      "This is always printed.\n",
      "This is also always printed.\n"
     ]
    }
   ],
   "source": [
    "# Example 2: If the number is positive, we print an appropriate message\n",
    "\n",
    "num = 3\n",
    "if (num > 0):  # if condition is TRUE: enter the body of if\n",
    "    print(num, \"is a positive number.\")\n",
    "print(\"This is always printed.\")\n",
    "\n",
    "num = -1\n",
    "if num > 0:  # if condition is FALSE: do not enter the body of if\n",
    "    print(num, \"is a negative number.\")\n",
    "print(\"This is also always printed.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Explanation**:\n",
    "\n",
    "In the above example, **`num > 0`** is the test expression.\n",
    "\n",
    "The body of **`if`** is executed only if this evaluates to **`True`**.\n",
    "\n",
    "When the variable **`num`** is equal to 3, test expression is true and statements inside the body of **`if`** are executed.\n",
    "\n",
    "If the variable **`num`** is equal to -1, test expression is false and statements inside the body of **`if`** are skipped.\n",
    "\n",
    "The **[print()](https://github.com/milaan9/04_Python_Functions/blob/main/002_Python_Functions_Built_in/051_Python_print%28%29.ipynb)** statement falls outside of the **`if`** block (unindented). Hence, it is executed regardless of the test expression."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-10T20:03:35.580430Z",
     "start_time": "2021-06-10T20:03:35.571663Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "81\n",
      "Next lines of code\n"
     ]
    }
   ],
   "source": [
    "# Example 3: Calculate the square of a number if it greater than 6\n",
    "\n",
    "number = 9\n",
    "if number > 6:\n",
    "    # Calculate square\n",
    "    print(number * number)\n",
    "print('Next lines of code')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-10T20:03:36.158393Z",
     "start_time": "2021-06-10T20:03:36.145731Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "num1 is less than num2\n"
     ]
    }
   ],
   "source": [
    "# Example 4: Check if num1 is less than num2\n",
    "\n",
    "num1, num2 = 6, 9\n",
    "if(num1 < num2):\n",
    "    print(\"num1 is less than num2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-10T20:03:36.548892Z",
     "start_time": "2021-06-10T20:03:36.529417Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello\n"
     ]
    }
   ],
   "source": [
    "x = 12\n",
    "if x > 10:\n",
    "    print(\"Hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-10T20:03:37.032393Z",
     "start_time": "2021-06-10T20:03:37.017791Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hooray!\n"
     ]
    }
   ],
   "source": [
    "if 5 != 3 * 6:\n",
    "    print (\"Hooray!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-10T20:03:38.014494Z",
     "start_time": "2021-06-10T20:03:37.995993Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Horray!\n"
     ]
    }
   ],
   "source": [
    "if 5 == 15/3:\n",
    "    print (\"Horray!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Shortcut for `if` statement (Short-hand `if` or one-line `if`)\n",
    "\n",
    "If you have only one statement to execute, then you can put it on the same line as the `if` statement. Let's try doing this for the above example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-10T20:03:38.795011Z",
     "start_time": "2021-06-10T20:03:38.778459Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "num1 is less than num2\n"
     ]
    }
   ],
   "source": [
    "num1, num2 = 5, 6\n",
    "if(num1 < num2): print(\"num1 is less than num2\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The Difference....\n",
    "\n",
    "Now, try to execute the below-given code and observe the difference."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-10T20:03:44.293701Z",
     "start_time": "2021-06-10T20:03:44.275199Z"
    }
   },
   "outputs": [],
   "source": [
    "#program to check if num1 is less than num 2\n",
    "num1, num2 = 6, 5\n",
    "if (num1 < num2):\n",
    "    print(\"num1 is less than num2\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Explanation:**\n",
    "\n",
    "**Did your Python IDE print anything?** No, it will not.\n",
    "\n",
    "In the above-given code, the value 6 is greater than 5, this means the condition is False. We have instructed the interpreter to print only when the condition is True and hence it in this case, nothing gets printed. However, **you can also instruct the interpreter what to do when the condition is False and this can be done using else statement**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-10T20:03:48.495255Z",
     "start_time": "2021-06-10T20:03:48.478701Z"
    }
   },
   "outputs": [],
   "source": [
    "if 5 == 18/3:\n",
    "    print (\"Hooray!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "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.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}