{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# If (elif, else)\n",
    "\n",
    "This is probably the most used conditional structure in programming. \n",
    "Here is the syntax in *Python*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1\n",
    "b = 4\n",
    "\n",
    "if a < b:\n",
    "    print('a is smaller than b')\n",
    "elif a > b:\n",
    "    print('a is larger than b')\n",
    "else:\n",
    "    print('a is equal to b')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Important*: the lines of code after the `if`, `elif`, `else` statement have to be indented by 4 spaces to indicate that they are part of the same instructions block.\n",
    "\n",
    "The table below includes some of the operators that can be used inside the `if` conditions. They all return a boolean variable (True or False).\n",
    "\n",
    "\n",
    "| Operator | Meaning\n",
    "| -------- | ---------\n",
    "|   ==     | Equal to \n",
    "|   !=     | Not equal        \n",
    "|   <      | Smaller than\n",
    "|   >      | Larger than\n",
    "|   <=     | Smaller-equal than\n",
    "|   >=     | Larger-equal than\n",
    "|   not    | Logiccal Negation \n",
    "|   in     | Checks whether an element is in a list\n",
    "|   and    | Checks whether two conditions are True\n",
    "|   or     | Checks whether at least one condition is True\n",
    "|   is     | Checks whether an elemenent is equal to another.\n",
    "\n",
    "Let see some examples:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "5 in [1, 2, 4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "3 in [1, 2, 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "not (2 == 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# while\n",
    "\n",
    "This control structure also checks for a condition to be `True` in order to execute a series of instructions. The 4 space rule for indentantion also applies.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = []\n",
    "while len(a) < 10:\n",
    "    a.append(0) # add elements to the list until I have 10.\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This is an example of random walk.\n",
    "import random\n",
    "\n",
    "position = 0\n",
    "n_steps = 0\n",
    "while abs(position) < 10: # I will walk until I am a distance of 10\n",
    "    step = 2.0*(random.random()-0.5) # this is a random variable between -1 and 1\n",
    "    position += step\n",
    "    n_steps += 1\n",
    "    \n",
    "print(position, n_steps) # this is the final position and the number of steps"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercise 1.1\n",
    "\n",
    "Compute how many times, on average, do you have to throw a die (with only six faces) in order to reach reach a minimum of 100 points. In this game you start with 0 points, you throw the die and if you get a 4 then you add 4 points into your account. Use the results of random.random() to implement the die throw."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}