{
 "metadata": {
  "name": "chapter-3.ipynb"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h1>Chapter 3: The Loop Control Structure <h1>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Simple Interest using While Loop, Page number: 99<h3>\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#Variable declaration\n",
      "count = 1\n",
      "pr = [1000,2000,3500]\n",
      "yr = [5,5,5]\n",
      "intr = [13.5,13.5,3.5]\n",
      "\n",
      "# while loop\n",
      "while count <= 3:\n",
      "    #Input from the user\n",
      "    #p,n,r = raw_input(\"Enter values of p, n and r : \").split()\n",
      "    p = pr[count-1] # principle\n",
      "    n = yr[count-1] # number of years\n",
      "    r = intr[count-1]# rate of interest\n",
      "\n",
      "    #Calculation\n",
      "    si = p * n * r / 100 ; #formula for simple interest\n",
      "\n",
      "    #Result\n",
      "    print \"Simple interest = Rs.\",si \n",
      "\n",
      "    #Increment count\n",
      "    count = count + 1\n",
      "    \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Simple interest = Rs. 675.0\n",
        "Simple interest = Rs. 1350.0\n",
        "Simple interest = Rs. 612.5\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Simple Interest using For Loop , Page number: 109<h3>\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "pr = [1000,2000,3500]\n",
      "yr = [5,5,5]\n",
      "intr = [13.5,13.5,3.5]\n",
      "\n",
      "#for loop\n",
      "for count in range(1, 4):\n",
      "    #Input from the user\n",
      "    #p,n,r = raw_input(\"Enter values of p, n and r : \").split()\n",
      "    p = pr[count-1] # principle\n",
      "    n = yr[count-1] # number of years\n",
      "    r = intr[count-1]# rate of interest\n",
      "    \n",
      "    #Calculation\n",
      "    si = p * n * r / 100 ; #formula for simple interest\n",
      "\n",
      "    #Result\n",
      "    print \"Simple interest = Rs.\",si "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Simple interest = Rs. 675.0\n",
        "Simple interest = Rs. 1350.0\n",
        "Simple interest = Rs. 612.5\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Nested For Loops , Page number: 114<h3>\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#nested for loops\n",
      "for r in range(1,4): #outer loop\n",
      "    for c in range(1,3): #inner loop\n",
      "        s = r + c #find the sum\n",
      "        print \"r = %d c = %d sum = %d\" % (r, c, s) #Display result\n",
      "    \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "r = 1 c = 1 sum = 2\n",
        "r = 1 c = 2 sum = 3\n",
        "r = 2 c = 1 sum = 3\n",
        "r = 2 c = 2 sum = 4\n",
        "r = 3 c = 1 sum = 4\n",
        "r = 3 c = 2 sum = 5\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Do While Loop , Page number: 116<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#do while loop\n",
      "while True:\n",
      "    #num = raw_input(\"Enter a number: \")\n",
      "    num = 11\n",
      "    print \"square of %d is %d\"%(num, num * num )\n",
      "    print \"Want to enter another number y/n: \" \n",
      "    another = 'n'\n",
      "    print another\n",
      "    if another == 'y':\n",
      "        continue\n",
      "    else:\n",
      "        break\n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "square of 11 is 121\n",
        "Want to enter another number y/n: \n",
        "n\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Do While using For Loop, Page number: 117<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#Variable declaration\n",
      "another = 'y'\n",
      "\n",
      "#do while loop\n",
      "import sys\n",
      "for i in range(1,10000): #infinte loop\n",
      "    #num = raw_input(\"Enter a number: \")\n",
      "    num = 11\n",
      "    print \"square of %d is %d\"%(num, num * num )\n",
      "    print \"Want to enter another number y/n: \" \n",
      "    another = 'n'\n",
      "    print another\n",
      "    if another == 'y':\n",
      "        continue\n",
      "    else:\n",
      "        break\n",
      "    \n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "square of 11 is 121\n",
        "Want to enter another number y/n: \n",
        "n\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Do While using While Loop, Page number: 117<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#Variable declaration\n",
      "another = 'y'\n",
      "\n",
      "#do while loop\n",
      "while another == 'y':\n",
      "    #num = raw_input(\"Enter a number: \")\n",
      "    num = 11\n",
      "    print \"square of %d is %d\"%(num, num * num )\n",
      "    print \"Want to enter another number y/n: \" \n",
      "    another = 'n'\n",
      "    print another\n",
      "   \n",
      "    \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "square of 11 is 121\n",
        "Want to enter another number y/n: \n",
        "n\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Prime Number, Page number: 118<h3>\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#Input from user\n",
      "#num = raw_input(\"Enter a number: \")\n",
      "num = 11\n",
      "\n",
      "#Variable declaration\n",
      "i = 2\n",
      "\n",
      "#while loop\n",
      "while i <=(num - 1):\n",
      "    if num % i == 0:\n",
      "        print \"Not a prime number\"  #Display if not prime number\n",
      "        break\n",
      "    i += 1\n",
      "\n",
      "#Display if prime number\n",
      "if i == num:\n",
      "    print  \"Prime number\" "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Prime number\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Break Statement , Page number: 119<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#Variable declaration\n",
      "i = 1\n",
      "j = 1\n",
      "\n",
      "#while loops\n",
      "while i <= 100 : #outer loop\n",
      "    i = i+1\n",
      "    while j <= 200 : #inner loop\n",
      "        j = j+1\n",
      "        if j == 150:\n",
      "            break #break statement in inner loop\n",
      "        else:\n",
      "            print i, j \n",
      "            \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "2 2\n",
        "2 3\n",
        "2 4\n",
        "2 5\n",
        "2 6\n",
        "2 7\n",
        "2 8\n",
        "2 9\n",
        "2 10\n",
        "2 11\n",
        "2 12\n",
        "2 13\n",
        "2 14\n",
        "2 15\n",
        "2 16\n",
        "2 17\n",
        "2 18\n",
        "2 19\n",
        "2 20\n",
        "2 21\n",
        "2 22\n",
        "2 23\n",
        "2 24\n",
        "2 25\n",
        "2 26\n",
        "2 27\n",
        "2 28\n",
        "2 29\n",
        "2 30\n",
        "2 31\n",
        "2 32\n",
        "2 33\n",
        "2 34\n",
        "2 35\n",
        "2 36\n",
        "2 37\n",
        "2 38\n",
        "2 39\n",
        "2 40\n",
        "2 41\n",
        "2 42\n",
        "2 43\n",
        "2 44\n",
        "2 45\n",
        "2 46\n",
        "2 47\n",
        "2 48\n",
        "2 49\n",
        "2 50\n",
        "2 51\n",
        "2 52\n",
        "2 53\n",
        "2 54\n",
        "2 55\n",
        "2 56\n",
        "2 57\n",
        "2 58\n",
        "2 59\n",
        "2 60\n",
        "2 61\n",
        "2 62\n",
        "2 63\n",
        "2 64\n",
        "2 65\n",
        "2 66\n",
        "2 67\n",
        "2 68\n",
        "2 69\n",
        "2 70\n",
        "2 71\n",
        "2 72\n",
        "2 73\n",
        "2 74\n",
        "2 75\n",
        "2 76\n",
        "2 77\n",
        "2 78\n",
        "2 79\n",
        "2 80\n",
        "2 81\n",
        "2 82\n",
        "2 83\n",
        "2 84\n",
        "2 85\n",
        "2 86\n",
        "2 87\n",
        "2 88\n",
        "2 89\n",
        "2 90\n",
        "2 91\n",
        "2 92\n",
        "2 93\n",
        "2 94\n",
        "2 95\n",
        "2 96\n",
        "2 97\n",
        "2 98\n",
        "2 99\n",
        "2 100\n",
        "2 101\n",
        "2 102\n",
        "2 103\n",
        "2 104\n",
        "2 105\n",
        "2 106\n",
        "2 107\n",
        "2 108\n",
        "2 109\n",
        "2 110\n",
        "2 111\n",
        "2 112\n",
        "2 113\n",
        "2 114\n",
        "2 115\n",
        "2 116\n",
        "2 117\n",
        "2 118\n",
        "2 119\n",
        "2 120\n",
        "2 121\n",
        "2 122\n",
        "2 123\n",
        "2 124\n",
        "2 125\n",
        "2 126\n",
        "2 127\n",
        "2 128\n",
        "2 129\n",
        "2 130\n",
        "2 131\n",
        "2 132\n",
        "2 133\n",
        "2 134\n",
        "2 135\n",
        "2 136\n",
        "2 137\n",
        "2 138\n",
        "2 139\n",
        "2 140\n",
        "2 141\n",
        "2 142\n",
        "2 143\n",
        "2 144\n",
        "2 145\n",
        "2 146\n",
        "2 147\n",
        "2 148\n",
        "2 149\n",
        "3 151\n",
        "3 152\n",
        "3 153\n",
        "3 154\n",
        "3 155\n",
        "3 156\n",
        "3 157\n",
        "3 158\n",
        "3 159\n",
        "3 160\n",
        "3 161\n",
        "3 162\n",
        "3 163\n",
        "3 164\n",
        "3 165\n",
        "3 166\n",
        "3 167\n",
        "3 168\n",
        "3 169\n",
        "3 170\n",
        "3 171\n",
        "3 172\n",
        "3 173\n",
        "3 174\n",
        "3 175\n",
        "3 176\n",
        "3 177\n",
        "3 178\n",
        "3 179\n",
        "3 180\n",
        "3 181\n",
        "3 182\n",
        "3 183\n",
        "3 184\n",
        "3 185\n",
        "3 186\n",
        "3 187\n",
        "3 188\n",
        "3 189\n",
        "3 190\n",
        "3 191\n",
        "3 192\n",
        "3 193\n",
        "3 194\n",
        "3 195\n",
        "3 196\n",
        "3 197\n",
        "3 198\n",
        "3 199\n",
        "3 200\n",
        "3 201\n"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Continue Statement , Page number: 120<h3>\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#for loops\n",
      "for i in range(1,3):\n",
      "    for j in range(1,3):\n",
      "        if i==j :\n",
      "            continue # continue statement\n",
      "        print  i , j\n",
      "        \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1 2\n",
        "2 1\n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}