diff options
Diffstat (limited to 'Let_us_C/chapter-4.ipynb')
-rw-r--r-- | Let_us_C/chapter-4.ipynb | 361 |
1 files changed, 361 insertions, 0 deletions
diff --git a/Let_us_C/chapter-4.ipynb b/Let_us_C/chapter-4.ipynb new file mode 100644 index 00000000..d6b348c1 --- /dev/null +++ b/Let_us_C/chapter-4.ipynb @@ -0,0 +1,361 @@ +{
+ "metadata": {
+ "name": "chapter-4.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 4: The Case Control Structure<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Switch - Case, Page number: 137<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate switch-case control structure(without break)'''\n",
+ "\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": [
+ "<h3>Switch - Case, Page number: 138<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to illustrate switch-case control structure'''\n",
+ "\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": [
+ "<h3>The Tips and Traps a), Page number: 140<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''An example of scrambled case order'''\n",
+ "\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": [
+ "<h3>The Tips and Traps b), Page number: 140<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''An example which shows the use of 'char' values in case and switch'''\n",
+ "\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": [
+ "<h3>The Tips and Traps c), Page number: 141<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''An example which shows how to execute a common set of statements for\n",
+ "multiple cases.'''\n",
+ "\n",
+ "#Input from user\n",
+ "#ch = raw_input(\"Enter any of the alphabet a, b, or c : \")\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": [
+ "<h3>The Tips and Traps e) , Page number: 143<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''An example that shows every statement in a switch must belong to some \n",
+ "case or the other. If it doesn\u2019t,the compiler won\u2019t report an error.\n",
+ "However, the statement would never get executed.'''\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": [
+ "<h3>Goto , Page number: 146<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program shows how to use goto statement'''\n",
+ "'''Python doesnt support statements like 'goto' '''\n",
+ "#Input from user\n",
+ "#goals = raw_input(\"Enter the number of goals scored against India: \")\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": [
+ "<h3>Goto , Page number: 148<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''The following program illustrates the usage of 'goto'\n",
+ "when we want to take the control out of the loop that is contained in\n",
+ "several other loops.'''\n",
+ "\n",
+ "\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": {}
+ }
+ ]
+}
\ No newline at end of file |