{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___\n",
    "\n",
    "<a href='https://www.udemy.com/user/joseportilla/'><img src='../Pierian_Data_Logo.png'/></a>\n",
    "___\n",
    "<center><em>Content Copyright by Pierian Data</em></center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# while Loops\n",
    "\n",
    "The <code>while</code> statement in Python is one of most general ways to perform iteration. A <code>while</code> statement will repeatedly execute a single statement or group of statements as long as the condition is true. The reason it is called a 'loop' is because the code statements are looped through over and over again until the condition is no longer met.\n",
    "\n",
    "The general format of a while loop is:\n",
    "\n",
    "    while test:\n",
    "        code statements\n",
    "    else:\n",
    "        final code statements\n",
    "\n",
    "Let’s look at a few simple <code>while</code> loops in action. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x is currently:  0\n",
      " x is still less than 10, adding 1 to x\n",
      "x is currently:  1\n",
      " x is still less than 10, adding 1 to x\n",
      "x is currently:  2\n",
      " x is still less than 10, adding 1 to x\n",
      "x is currently:  3\n",
      " x is still less than 10, adding 1 to x\n",
      "x is currently:  4\n",
      " x is still less than 10, adding 1 to x\n",
      "x is currently:  5\n",
      " x is still less than 10, adding 1 to x\n",
      "x is currently:  6\n",
      " x is still less than 10, adding 1 to x\n",
      "x is currently:  7\n",
      " x is still less than 10, adding 1 to x\n",
      "x is currently:  8\n",
      " x is still less than 10, adding 1 to x\n",
      "x is currently:  9\n",
      " x is still less than 10, adding 1 to x\n"
     ]
    }
   ],
   "source": [
    "x = 0\n",
    "\n",
    "while x < 10:\n",
    "    print('x is currently: ',x)\n",
    "    print(' x is still less than 10, adding 1 to x')\n",
    "    x+=1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice how many times the print statements occurred and how the <code>while</code> loop kept going until the True condition was met, which occurred once x==10. It's important to note that once this occurred the code stopped. Let's see how we could add an <code>else</code> statement:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x is currently:  0\n",
      " x is still less than 10, adding 1 to x\n",
      "x is currently:  1\n",
      " x is still less than 10, adding 1 to x\n",
      "x is currently:  2\n",
      " x is still less than 10, adding 1 to x\n",
      "x is currently:  3\n",
      " x is still less than 10, adding 1 to x\n",
      "x is currently:  4\n",
      " x is still less than 10, adding 1 to x\n",
      "x is currently:  5\n",
      " x is still less than 10, adding 1 to x\n",
      "x is currently:  6\n",
      " x is still less than 10, adding 1 to x\n",
      "x is currently:  7\n",
      " x is still less than 10, adding 1 to x\n",
      "x is currently:  8\n",
      " x is still less than 10, adding 1 to x\n",
      "x is currently:  9\n",
      " x is still less than 10, adding 1 to x\n",
      "All Done!\n"
     ]
    }
   ],
   "source": [
    "x = 0\n",
    "\n",
    "while x < 10:\n",
    "    print('x is currently: ',x)\n",
    "    print(' x is still less than 10, adding 1 to x')\n",
    "    x+=1\n",
    "    \n",
    "else:\n",
    "    print('All Done!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# break, continue, pass\n",
    "\n",
    "We can use <code>break</code>, <code>continue</code>, and <code>pass</code> statements in our loops to add additional functionality for various cases. The three statements are defined by:\n",
    "\n",
    "    break: Breaks out of the current closest enclosing loop.\n",
    "    continue: Goes to the top of the closest enclosing loop.\n",
    "    pass: Does nothing at all.\n",
    "    \n",
    "    \n",
    "Thinking about <code>break</code> and <code>continue</code> statements, the general format of the <code>while</code> loop looks like this:\n",
    "\n",
    "    while test: \n",
    "        code statement\n",
    "        if test: \n",
    "            break\n",
    "        if test: \n",
    "            continue \n",
    "    else:\n",
    "\n",
    "<code>break</code> and <code>continue</code> statements can appear anywhere inside the loop’s body, but we will usually put them further nested in conjunction with an <code>if</code> statement to perform an action based on some condition.\n",
    "\n",
    "Let's go ahead and look at some examples!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x is currently:  0\n",
      " x is still less than 10, adding 1 to x\n",
      "continuing...\n",
      "x is currently:  1\n",
      " x is still less than 10, adding 1 to x\n",
      "continuing...\n",
      "x is currently:  2\n",
      " x is still less than 10, adding 1 to x\n",
      "x==3\n",
      "x is currently:  3\n",
      " x is still less than 10, adding 1 to x\n",
      "continuing...\n",
      "x is currently:  4\n",
      " x is still less than 10, adding 1 to x\n",
      "continuing...\n",
      "x is currently:  5\n",
      " x is still less than 10, adding 1 to x\n",
      "continuing...\n",
      "x is currently:  6\n",
      " x is still less than 10, adding 1 to x\n",
      "continuing...\n",
      "x is currently:  7\n",
      " x is still less than 10, adding 1 to x\n",
      "continuing...\n",
      "x is currently:  8\n",
      " x is still less than 10, adding 1 to x\n",
      "continuing...\n",
      "x is currently:  9\n",
      " x is still less than 10, adding 1 to x\n",
      "continuing...\n"
     ]
    }
   ],
   "source": [
    "x = 0\n",
    "\n",
    "while x < 10:\n",
    "    print('x is currently: ',x)\n",
    "    print(' x is still less than 10, adding 1 to x')\n",
    "    x+=1\n",
    "    if x==3:\n",
    "        print('x==3')\n",
    "    else:\n",
    "        print('continuing...')\n",
    "        continue"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note how we have a printed statement when x==3, and a continue being printed out as we continue through the outer while loop. Let's put in a break once x ==3 and see if the result makes sense:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x is currently:  0\n",
      " x is still less than 10, adding 1 to x\n",
      "continuing...\n",
      "x is currently:  1\n",
      " x is still less than 10, adding 1 to x\n",
      "continuing...\n",
      "x is currently:  2\n",
      " x is still less than 10, adding 1 to x\n",
      "Breaking because x==3\n"
     ]
    }
   ],
   "source": [
    "x = 0\n",
    "\n",
    "while x < 10:\n",
    "    print('x is currently: ',x)\n",
    "    print(' x is still less than 10, adding 1 to x')\n",
    "    x+=1\n",
    "    if x==3:\n",
    "        print('Breaking because x==3')\n",
    "        break\n",
    "    else:\n",
    "        print('continuing...')\n",
    "        continue"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note how the other <code>else</code> statement wasn't reached and continuing was never printed!\n",
    "\n",
    "After these brief but simple examples, you should feel comfortable using <code>while</code> statements in your code.\n",
    "\n",
    "**A word of caution however! It is possible to create an infinitely running loop with <code>while</code> statements. For example:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# DO NOT RUN THIS CODE!!!! \n",
    "while True:\n",
    "    print(\"I'm stuck in an infinite loop!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "A quick note: If you *did* run the above cell, click on the Kernel menu above to restart the kernel!"
   ]
  }
 ],
 "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
