{ "metadata": { "name": "", "signature": "sha256:7c22324d39011030a3b150073070e5711999d5a4173c27c085bd0e0ed1d76d18" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 4: The Case Control Structure

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Switch - Case, Page number: 137

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "i = 2\n", "\n", "#Switch case statements\n", "if i == 1: # case 1\n", " print \"I am in case 1\"\n", "else:\n", " print \"I am in case 2\"# case 2\n", " print \"I am in case 3\"#case 3\n", " print \"I am in default\"# default\n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "I am in case 2\n", "I am in case 3\n", "I am in default\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Switch - Case, Page number: 138

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "i = 2\n", "\n", "#Switch case statements\n", "if i == 1: # case 1\n", " print \"I am in case 1\"\n", "elif i == 2: # case 2\n", " print \"I am in case 2\"\n", "elif i == 3: #case 3\n", " print \"I am in case 3\"\n", "else: # default\n", " print \"I am in default\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "I am in case 2\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

The Tips and Traps a), Page number: 140

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "i = 22\n", "\n", "#Switch case statements\n", "if i == 121: # case 121\n", " print \"I am in case 121\"\n", "elif i == 7: # case 7\n", " print \"I am in case 7\"\n", "elif i == 22: #case 22\n", " print \"I am in case 22\"\n", "else: # default\n", " print \"I am in default\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "I am in case 22\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

The Tips and Traps b), Page number: 140

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "c = 'x'\n", "\n", "#Switch case statements\n", "if c == 'v': # case 'v'\n", " print \"I am in case v\"\n", "elif c == 'a': # case 'a'\n", " print \"I am in case a\"\n", "elif c == 'x': #case 'x'\n", " print \"I am in case x\"\n", "else: # default\n", " print \"I am in default\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "I am in case x\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

The Tips and Traps c), Page number: 141

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "ch = 'a'\n", "\n", "#Switch case statements\n", "if ch == 'a' or ch == 'A' : # case 'a' and case 'A'\n", " print \"a as in ashar\"\n", "elif ch == 'b'or ch == 'B': # case 'b' and case 'B'\n", " print \"b as in brain\"\n", "elif ch == 'c'or ch == 'C': # case 'c' and case 'C'\n", " print \"c as in cookie\"\n", "else: # default\n", " print (\"wish you knew what are alphabets\")\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a as in ashar\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

The Tips and Traps e) , Page number: 143

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "#Input from user\n", "#i = raw_input(\"Enter value of i \")\n", "i = 1\n", "\n", "#Switch case statements\n", "#print \"Hello\"\n", "if i == 1 : # case 1\n", " j = 10\n", "elif i == 2 :# case 2\n", " j = 20\n", "\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Goto , Page number: 146

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "goals = 3\n", "\n", "if goals <= 5 : #goto\n", " print \"To err is human!\" #label sos\n", "else:\n", " print \"About time soccer players learnt C\" \n", " print \"and said goodbye! adieu! to soccer\" \n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "To err is human!\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Goto , Page number: 148

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#nested for loops\n", "for i in range(1,4):\n", " for j in range(1,4):\n", " for k in range(1,4):\n", " if ( i == 3 and j == 3 and k == 3 ): #goto\n", " print \"Out of the loop at last!\" # label out\n", " break\n", " else:\n", " print i, j, k " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 1 1\n", "1 1 2\n", "1 1 3\n", "1 2 1\n", "1 2 2\n", "1 2 3\n", "1 3 1\n", "1 3 2\n", "1 3 3\n", "2 1 1\n", "2 1 2\n", "2 1 3\n", "2 2 1\n", "2 2 2\n", "2 2 3\n", "2 3 1\n", "2 3 2\n", "2 3 3\n", "3 1 1\n", "3 1 2\n", "3 1 3\n", "3 2 1\n", "3 2 2\n", "3 2 3\n", "3 3 1\n", "3 3 2\n", "Out of the loop at last!\n" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }