diff options
Diffstat (limited to 'ANSI_C_Programming/chapter3.ipynb')
-rw-r--r-- | ANSI_C_Programming/chapter3.ipynb | 1590 |
1 files changed, 1590 insertions, 0 deletions
diff --git a/ANSI_C_Programming/chapter3.ipynb b/ANSI_C_Programming/chapter3.ipynb new file mode 100644 index 00000000..18b9c2f4 --- /dev/null +++ b/ANSI_C_Programming/chapter3.ipynb @@ -0,0 +1,1590 @@ +{
+ "metadata": {
+ "name": "chapter3.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "CHAPTER 3: THE LOOP CONTROL STRUCTURE"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:89-90"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Calculation of simple interest for 3 sets of p,n and r\n",
+ "count=1\n",
+ "while count<=3: #while loop codition check\n",
+ " print \"Enter values of p,n and r\"\n",
+ " p=eval(raw_input())\n",
+ " n=eval(raw_input())\n",
+ " r=eval(raw_input())\n",
+ " si=p*n*r/100\n",
+ " print \"Simple interest=Rs.%f\" % (si)\n",
+ " count=count+1 #increment"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter values of p,n and r\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1000\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "13.5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Simple interest=Rs.675.000000\n",
+ "Enter values of p,n and r\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2000\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "13.5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Simple interest=Rs.1350.000000\n",
+ "Enter values of p,n and r\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3500\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3.5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Simple interest=Rs.612.500000\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:92"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#a program for indefinite loop\n",
+ "i=1\n",
+ "while i<=10:\n",
+ " print \"%d\\n\" % (i) #there is no increment/decrement thats why indefinite loop"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:93"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#printing numbers from 1 to 10\n",
+ "i=1\n",
+ "while i<=10:\n",
+ " print \"\\n\" ,i\n",
+ " i=i+1"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1\n",
+ "\n",
+ "2\n",
+ "\n",
+ "3\n",
+ "\n",
+ "4\n",
+ "\n",
+ "5\n",
+ "\n",
+ "6\n",
+ "\n",
+ "7\n",
+ "\n",
+ "8\n",
+ "\n",
+ "9\n",
+ "\n",
+ "10\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:93"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#printing a message repeatedly\n",
+ "i=5\n",
+ "while i>=1:\n",
+ " print \"Make the computer literate!\\n\"\n",
+ " i=i-1"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Make the computer literate!\n",
+ "\n",
+ "Make the computer literate!\n",
+ "\n",
+ "Make the computer literate!\n",
+ "\n",
+ "Make the computer literate!\n",
+ "\n",
+ "Make the computer literate!\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:93"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#printing a message repeatedly by taking float loop counter\n",
+ "a=10.0\n",
+ "while a<=10.5:\n",
+ " print \"Raindrops on roses...\"\n",
+ " print \"...and whiskers on kittens\\n\"\n",
+ " a=a+0.1"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Raindrops on roses...\n",
+ "...and whiskers on kittens\n",
+ "\n",
+ "Raindrops on roses...\n",
+ "...and whiskers on kittens\n",
+ "\n",
+ "Raindrops on roses...\n",
+ "...and whiskers on kittens\n",
+ "\n",
+ "Raindrops on roses...\n",
+ "...and whiskers on kittens\n",
+ "\n",
+ "Raindrops on roses...\n",
+ "...and whiskers on kittens\n",
+ "\n",
+ "Raindrops on roses...\n",
+ "...and whiskers on kittens\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:94"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#This is not indefinite loop in python because range of int in python is larger than 32767\n",
+ "i=1\n",
+ "while i<=32767: #print numbers from 1 to 32767\n",
+ " print \"%d\\n\" ,i \n",
+ " i=i+1"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:95"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Method 1:print numbers from 1 to 10\n",
+ "i=1 \n",
+ "while i<=10:\n",
+ " print i\n",
+ " i=i+1"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n",
+ "5\n",
+ "6\n",
+ "7\n",
+ "8\n",
+ "9\n",
+ "10\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:96"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Method 2:print numbers from 1 to 10\n",
+ "i=1\n",
+ "while i<=10:\n",
+ " print \"\\n\" ,i\n",
+ " i+=1 #increment short hand operator"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1\n",
+ "\n",
+ "2\n",
+ "\n",
+ "3\n",
+ "\n",
+ "4\n",
+ "\n",
+ "5\n",
+ "\n",
+ "6\n",
+ "\n",
+ "7\n",
+ "\n",
+ "8\n",
+ "\n",
+ "9\n",
+ "\n",
+ "10\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:98"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Calculation of simple interest for 3 sets of p,n and r using \"for loop\"\n",
+ "for count in range(1,4,1): #for loop\n",
+ " print \"Enter values of p,n and r\"\n",
+ " p=eval(raw_input())\n",
+ " n=eval(raw_input())\n",
+ " r=eval(raw_input())\n",
+ " si=p*n*r/100\n",
+ " print \"Simple Interest=Rs.%f\" % (si)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter values of p,n and r\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1000\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "13.5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Simple Interest=Rs.675.000000\n",
+ "Enter values of p,n and r\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2000\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "13.5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Simple Interest=Rs.1350.000000\n",
+ "Enter values of p,n and r\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3500\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3.5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Simple Interest=Rs.612.500000\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:101"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#print numbers from 1 to 10 using for loop\n",
+ "for i in range(1,11,1):\n",
+ " print \"%d\\n\" % (i)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n",
+ "\n",
+ "2\n",
+ "\n",
+ "3\n",
+ "\n",
+ "4\n",
+ "\n",
+ "5\n",
+ "\n",
+ "6\n",
+ "\n",
+ "7\n",
+ "\n",
+ "8\n",
+ "\n",
+ "9\n",
+ "\n",
+ "10\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:101"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#print numbers from 1 to 10 with incrementation in the body\n",
+ "for i in range(1,11):\n",
+ " print \"\\n\" ,i\n",
+ " i=i+1"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1\n",
+ "\n",
+ "2\n",
+ "\n",
+ "3\n",
+ "\n",
+ "4\n",
+ "\n",
+ "5\n",
+ "\n",
+ "6\n",
+ "\n",
+ "7\n",
+ "\n",
+ "8\n",
+ "\n",
+ "9\n",
+ "\n",
+ "10\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:101"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#print numbers from 1 to 10 with initialisation above for loop\n",
+ "i=1\n",
+ "for i in range(i,11,1):\n",
+ " print \"%d\\n\" % (i)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n",
+ "\n",
+ "2\n",
+ "\n",
+ "3\n",
+ "\n",
+ "4\n",
+ "\n",
+ "5\n",
+ "\n",
+ "6\n",
+ "\n",
+ "7\n",
+ "\n",
+ "8\n",
+ "\n",
+ "9\n",
+ "\n",
+ "10\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:102"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#print 1 to 10 with initialisation above for loop and incrementation in the body\n",
+ "i=1\n",
+ "for i in range(i,11):\n",
+ " print \"%d\\n\" % (i)\n",
+ " i=i+1"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n",
+ "\n",
+ "2\n",
+ "\n",
+ "3\n",
+ "\n",
+ "4\n",
+ "\n",
+ "5\n",
+ "\n",
+ "6\n",
+ "\n",
+ "7\n",
+ "\n",
+ "8\n",
+ "\n",
+ "9\n",
+ "\n",
+ "10\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:103"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Demonstration of nested loops\n",
+ "for r in range(1,4,1): #outer loop\n",
+ " for c in range(1,3,1): #inner loop \n",
+ " sum=r+c\n",
+ " print \"r=%d c=%d sum=%d\\n\" % (r,c,sum)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "r=1 c=1 sum=2\n",
+ "\n",
+ "r=1 c=2 sum=3\n",
+ "\n",
+ "r=2 c=1 sum=3\n",
+ "\n",
+ "r=2 c=2 sum=4\n",
+ "\n",
+ "r=3 c=1 sum=4\n",
+ "\n",
+ "r=3 c=2 sum=5\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:104-105"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Execution of a loop an unknown number of times\n",
+ "#Implementing do-while loop using while loop\n",
+ "while(1): #do-while loop is not present in python\n",
+ " print \"Enter a number\"\n",
+ " num=eval(raw_input())\n",
+ " print \"square of %d is %d\\n\" % (num,num*num)\n",
+ " print \"Want to enter another number y/n\"\n",
+ " another=raw_input()\n",
+ " if(another!='y'):\n",
+ " break"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "square of 5 is 25\n",
+ "\n",
+ "Want to enter another number y/n\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "y\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "square of 7 is 49\n",
+ "\n",
+ "Want to enter another number y/n\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:105"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#odd loop using a for loop\n",
+ "another='y'\n",
+ "for i in range(1,1000): #check if another is in word\n",
+ " if another=='y':\n",
+ " print \"Enter a number\"\n",
+ " num=eval(raw_input())\n",
+ " print \"square of %d is %d\\n\" % (num,num*num)\n",
+ " print \"Want to enter another number y/n\"\n",
+ " another=raw_input()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "square of 5 is 25\n",
+ "\n",
+ "Want to enter another number y/n\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "y\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "square of 7 is 49\n",
+ "\n",
+ "Want to enter another number y/n\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:105-106"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#odd loop using a while loop\n",
+ "another='y'\n",
+ "while(another=='y'):\n",
+ " print \"Enter a number\"\n",
+ " num=eval(raw_input())\n",
+ " print \"square of %d is %d\\n\" % (num,num*num)\n",
+ " print \"Want to enter another number y/n\"\n",
+ " another=raw_input()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "square of 5 is 25\n",
+ "\n",
+ "Want to enter another number y/n\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "y\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "square of 7 is 49\n",
+ "\n",
+ "Want to enter another number y/n\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:106"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program to check prime number using break in while loop\n",
+ "print \"Enter a number\"\n",
+ "num=eval(raw_input())\n",
+ "i=2\n",
+ "while i<=num-1:\n",
+ " if num%i==0:\n",
+ " print \"Not a prime number\\n\"\n",
+ " break #exit the loop\n",
+ " i+=1\n",
+ "if i==num:\n",
+ " print \"Prime number\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "11\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Prime number\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:107"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#program using break statement\n",
+ "i=1\n",
+ "j=1\n",
+ "while (i<=100):\n",
+ " i+=1\n",
+ " while (j<=200):\n",
+ " j+=1\n",
+ " if (j==150):\n",
+ " break #it will terminate the inner loop only\n",
+ " else:\n",
+ " print \"%d %d\\n\" % (i,j)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2 2\n",
+ "\n",
+ "2 3\n",
+ "\n",
+ "2 4\n",
+ "\n",
+ "2 5\n",
+ "\n",
+ "2 6\n",
+ "\n",
+ "2 7\n",
+ "\n",
+ "2 8\n",
+ "\n",
+ "2 9\n",
+ "\n",
+ "2 10\n",
+ "\n",
+ "2 11\n",
+ "\n",
+ "2 12\n",
+ "\n",
+ "2 13\n",
+ "\n",
+ "2 14\n",
+ "\n",
+ "2 15\n",
+ "\n",
+ "2 16\n",
+ "\n",
+ "2 17\n",
+ "\n",
+ "2 18\n",
+ "\n",
+ "2 19\n",
+ "\n",
+ "2 20\n",
+ "\n",
+ "2 21\n",
+ "\n",
+ "2 22\n",
+ "\n",
+ "2 23\n",
+ "\n",
+ "2 24\n",
+ "\n",
+ "2 25\n",
+ "\n",
+ "2 26\n",
+ "\n",
+ "2 27\n",
+ "\n",
+ "2 28\n",
+ "\n",
+ "2 29\n",
+ "\n",
+ "2 30\n",
+ "\n",
+ "2 31\n",
+ "\n",
+ "2 32\n",
+ "\n",
+ "2 33\n",
+ "\n",
+ "2 34\n",
+ "\n",
+ "2 35\n",
+ "\n",
+ "2 36\n",
+ "\n",
+ "2 37\n",
+ "\n",
+ "2 38\n",
+ "\n",
+ "2 39\n",
+ "\n",
+ "2 40\n",
+ "\n",
+ "2 41\n",
+ "\n",
+ "2 42\n",
+ "\n",
+ "2 43\n",
+ "\n",
+ "2 44\n",
+ "\n",
+ "2 45\n",
+ "\n",
+ "2 46\n",
+ "\n",
+ "2 47\n",
+ "\n",
+ "2 48\n",
+ "\n",
+ "2 49\n",
+ "\n",
+ "2 50\n",
+ "\n",
+ "2 51\n",
+ "\n",
+ "2 52\n",
+ "\n",
+ "2 53\n",
+ "\n",
+ "2 54\n",
+ "\n",
+ "2 55\n",
+ "\n",
+ "2 56\n",
+ "\n",
+ "2 57\n",
+ "\n",
+ "2 58\n",
+ "\n",
+ "2 59\n",
+ "\n",
+ "2 60\n",
+ "\n",
+ "2 61\n",
+ "\n",
+ "2 62\n",
+ "\n",
+ "2 63\n",
+ "\n",
+ "2 64\n",
+ "\n",
+ "2 65\n",
+ "\n",
+ "2 66\n",
+ "\n",
+ "2 67\n",
+ "\n",
+ "2 68\n",
+ "\n",
+ "2 69\n",
+ "\n",
+ "2 70\n",
+ "\n",
+ "2 71\n",
+ "\n",
+ "2 72\n",
+ "\n",
+ "2 73\n",
+ "\n",
+ "2 74\n",
+ "\n",
+ "2 75\n",
+ "\n",
+ "2 76\n",
+ "\n",
+ "2 77\n",
+ "\n",
+ "2 78\n",
+ "\n",
+ "2 79\n",
+ "\n",
+ "2 80\n",
+ "\n",
+ "2 81\n",
+ "\n",
+ "2 82\n",
+ "\n",
+ "2 83\n",
+ "\n",
+ "2 84\n",
+ "\n",
+ "2 85\n",
+ "\n",
+ "2 86\n",
+ "\n",
+ "2 87\n",
+ "\n",
+ "2 88\n",
+ "\n",
+ "2 89\n",
+ "\n",
+ "2 90\n",
+ "\n",
+ "2 91\n",
+ "\n",
+ "2 92\n",
+ "\n",
+ "2 93\n",
+ "\n",
+ "2 94\n",
+ "\n",
+ "2 95\n",
+ "\n",
+ "2 96\n",
+ "\n",
+ "2 97\n",
+ "\n",
+ "2 98\n",
+ "\n",
+ "2 99\n",
+ "\n",
+ "2 100\n",
+ "\n",
+ "2 101\n",
+ "\n",
+ "2 102\n",
+ "\n",
+ "2 103\n",
+ "\n",
+ "2 104\n",
+ "\n",
+ "2 105\n",
+ "\n",
+ "2 106\n",
+ "\n",
+ "2 107\n",
+ "\n",
+ "2 108\n",
+ "\n",
+ "2 109\n",
+ "\n",
+ "2 110\n",
+ "\n",
+ "2 111\n",
+ "\n",
+ "2 112\n",
+ "\n",
+ "2 113\n",
+ "\n",
+ "2 114\n",
+ "\n",
+ "2 115\n",
+ "\n",
+ "2 116\n",
+ "\n",
+ "2 117\n",
+ "\n",
+ "2 118\n",
+ "\n",
+ "2 119\n",
+ "\n",
+ "2 120\n",
+ "\n",
+ "2 121\n",
+ "\n",
+ "2 122\n",
+ "\n",
+ "2 123\n",
+ "\n",
+ "2 124\n",
+ "\n",
+ "2 125\n",
+ "\n",
+ "2 126\n",
+ "\n",
+ "2 127\n",
+ "\n",
+ "2 128\n",
+ "\n",
+ "2 129\n",
+ "\n",
+ "2 130\n",
+ "\n",
+ "2 131\n",
+ "\n",
+ "2 132\n",
+ "\n",
+ "2 133\n",
+ "\n",
+ "2 134\n",
+ "\n",
+ "2 135\n",
+ "\n",
+ "2 136\n",
+ "\n",
+ "2 137\n",
+ "\n",
+ "2 138\n",
+ "\n",
+ "2 139\n",
+ "\n",
+ "2 140\n",
+ "\n",
+ "2 141\n",
+ "\n",
+ "2 142\n",
+ "\n",
+ "2 143\n",
+ "\n",
+ "2 144\n",
+ "\n",
+ "2 145\n",
+ "\n",
+ "2 146\n",
+ "\n",
+ "2 147\n",
+ "\n",
+ "2 148\n",
+ "\n",
+ "2 149\n",
+ "\n",
+ "3 151\n",
+ "\n",
+ "3 152\n",
+ "\n",
+ "3 153\n",
+ "\n",
+ "3 154\n",
+ "\n",
+ "3 155\n",
+ "\n",
+ "3 156\n",
+ "\n",
+ "3 157\n",
+ "\n",
+ "3 158\n",
+ "\n",
+ "3 159\n",
+ "\n",
+ "3 160\n",
+ "\n",
+ "3 161\n",
+ "\n",
+ "3 162\n",
+ "\n",
+ "3 163\n",
+ "\n",
+ "3 164\n",
+ "\n",
+ "3 165\n",
+ "\n",
+ "3 166\n",
+ "\n",
+ "3 167\n",
+ "\n",
+ "3 168\n",
+ "\n",
+ "3 169\n",
+ "\n",
+ "3 170\n",
+ "\n",
+ "3 171\n",
+ "\n",
+ "3 172\n",
+ "\n",
+ "3 173\n",
+ "\n",
+ "3 174\n",
+ "\n",
+ "3 175\n",
+ "\n",
+ "3 176\n",
+ "\n",
+ "3 177\n",
+ "\n",
+ "3 178\n",
+ "\n",
+ "3 179\n",
+ "\n",
+ "3 180\n",
+ "\n",
+ "3 181\n",
+ "\n",
+ "3 182\n",
+ "\n",
+ "3 183\n",
+ "\n",
+ "3 184\n",
+ "\n",
+ "3 185\n",
+ "\n",
+ "3 186\n",
+ "\n",
+ "3 187\n",
+ "\n",
+ "3 188\n",
+ "\n",
+ "3 189\n",
+ "\n",
+ "3 190\n",
+ "\n",
+ "3 191\n",
+ "\n",
+ "3 192\n",
+ "\n",
+ "3 193\n",
+ "\n",
+ "3 194\n",
+ "\n",
+ "3 195\n",
+ "\n",
+ "3 196\n",
+ "\n",
+ "3 197\n",
+ "\n",
+ "3 198\n",
+ "\n",
+ "3 199\n",
+ "\n",
+ "3 200\n",
+ "\n",
+ "3 201\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:108"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#program using continue statement\n",
+ "for i in range(1,3,1):\n",
+ " for j in range(1,3,1):\n",
+ " if i==j:\n",
+ " continue #it will again send back to loop without executing succeeding statements\n",
+ " print \"%d %d\\n\" % (i,j)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 2\n",
+ "\n",
+ "2 1\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file |