{
 "metadata": {
  "name": "",
  "signature": "sha256:c88e4872fc4418c2962652967a7ca0a93c345eb9f3bd38488ba817f74e5fc2eb"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "CHAPTER 4: THE CASE CONTROL STRUCTURE"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "EXAMPLE ON PAGE:126"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "i=2\n",
      "def switch(i):\n",
      "    return {True: 'I am in default\\n',                  #default case\n",
      "            i==1: 'I am in case1\\n',\n",
      "            i==2: 'I am in case2\\n',\n",
      "            i==3: 'I am in case3\\n',\n",
      "            }[True]\n",
      "print switch(i)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "I am in case2\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "EXAMPLE ON PAGE:128"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "i=22\n",
      "def switch(i):\n",
      "    return{True: 'I am in default\\n',\n",
      "           i==121: 'I am in case 121\\n',\n",
      "           i==7: 'I am in case 7\\n',\n",
      "           i==22: 'I am in case 22\\n',\n",
      "           }[True]\n",
      "print switch(i)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "I am in case 22\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "EXAMPLE ON PAGE:128-129"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "c='x'\n",
      "def switch(c):\n",
      "    return {True: 'I am in default\\n',\n",
      "            c=='v': 'I am in case v\\n',\n",
      "            c=='a': 'I am in case a\\n',\n",
      "            c=='x': 'I am in case x\\n',\n",
      "            }[True]\n",
      "print switch(c)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "I am in case x\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "EXAMPLE ON PAGE:129-130"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "print \"Enter any one of the alphabets a,b,or c\"\n",
      "ch=raw_input()\n",
      "def casea():\n",
      "    print \"\"\n",
      "def caseb():\n",
      "    print \"\"\n",
      "def casec():\n",
      "    print \"\"\n",
      "def switch(ch):\n",
      "    return {True: 'wish you knew what are alphabets\\n',\n",
      "            ch=='a' or ch=='A': 'a as in ashar\\n',\n",
      "            ch=='b' or ch=='B': 'b as in brain\\n',\n",
      "            ch=='c' or ch=='C': 'c as in cookie\\n',\n",
      "            }[True]\n",
      "print switch(ch)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter any one of the alphabets a,b,or c\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "B\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "b as in brain\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "EXAMPLE ON PAGE:130"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "print \"Enter value of i\"\n",
      "i=eval(raw_input())\n",
      "def switch(i):\n",
      "    print \"Hello\\n\"                              #It's python,not C :)\n",
      "    try:\n",
      "        return {1: a,\n",
      "                2: b,\n",
      "                }[i]()\n",
      "    except:\n",
      "        print \"wrong choice\"                     #print message in default case\n",
      "def a():\n",
      "    j=10\n",
      "    print j                                      #print j as 10 if i=1\n",
      "def b():\n",
      "    j=20 \n",
      "    print j                                      #print j as 20 if i=2\n",
      "switch(i)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter value of i\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Hello\n",
        "\n",
        "10\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "EXAMPLE ON PAGE:133"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "import sys\n",
      "print \"Enter the number of goals scored against india\"\n",
      "goals=eval(raw_input())\n",
      "if goals<=5:\n",
      "    print \"To err is human!\\n\"\n",
      "else:\n",
      "    print \"About time soccer players learnt C\\n\"\n",
      "    print \"and said goodbye! adieu! to soccer\\n\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the number of goals scored against india\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "7\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "About time soccer players learnt C\n",
        "\n",
        "and said goodbye! adieu! to soccer\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "EXAMPLE ON PAGE:134-135"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "for i in range(1,4,1):\n",
      "    for j in range(1,4,1):\n",
      "        for k in range(1,4,1):\n",
      "            if (i==3 and j==3 and k==3):\n",
      "                print \"Out of the loop at last!\\n\"\n",
      "            else:\n",
      "                print \"%d %d %d\\n\" % (i,j,k)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1 1 1\n",
        "\n",
        "1 1 2\n",
        "\n",
        "1 1 3\n",
        "\n",
        "1 2 1\n",
        "\n",
        "1 2 2\n",
        "\n",
        "1 2 3\n",
        "\n",
        "1 3 1\n",
        "\n",
        "1 3 2\n",
        "\n",
        "1 3 3\n",
        "\n",
        "2 1 1\n",
        "\n",
        "2 1 2\n",
        "\n",
        "2 1 3\n",
        "\n",
        "2 2 1\n",
        "\n",
        "2 2 2\n",
        "\n",
        "2 2 3\n",
        "\n",
        "2 3 1\n",
        "\n",
        "2 3 2\n",
        "\n",
        "2 3 3\n",
        "\n",
        "3 1 1\n",
        "\n",
        "3 1 2\n",
        "\n",
        "3 1 3\n",
        "\n",
        "3 2 1\n",
        "\n",
        "3 2 2\n",
        "\n",
        "3 2 3\n",
        "\n",
        "3 3 1\n",
        "\n",
        "3 3 2\n",
        "\n",
        "Out of the loop at last!\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 12
    }
   ],
   "metadata": {}
  }
 ]
}