Chapter 3: The Loop Control Structure

Simple Interest using While Loop, Page number: 99

In [1]:
#Variable declaration
count = 1
pr = [1000,2000,3500]
yr = [5,5,5]
intr = [13.5,13.5,3.5]

# while loop
while count <= 3:
    #Input from the user
    #p,n,r = raw_input("Enter values of p, n and r : ").split()
    p = pr[count-1] # principle
    n = yr[count-1] # number of years
    r = intr[count-1]# rate of interest

    #Calculation
    si = p * n * r / 100 ; #formula for simple interest

    #Result
    print "Simple interest = Rs.",si 

    #Increment count
    count = count + 1
    
Simple interest = Rs. 675.0
Simple interest = Rs. 1350.0
Simple interest = Rs. 612.5

Simple Interest using For Loop , Page number: 109

In [2]:
pr = [1000,2000,3500]
yr = [5,5,5]
intr = [13.5,13.5,3.5]

#for loop
for count in range(1, 4):
    #Input from the user
    #p,n,r = raw_input("Enter values of p, n and r : ").split()
    p = pr[count-1] # principle
    n = yr[count-1] # number of years
    r = intr[count-1]# rate of interest
    
    #Calculation
    si = p * n * r / 100 ; #formula for simple interest

    #Result
    print "Simple interest = Rs.",si 
Simple interest = Rs. 675.0
Simple interest = Rs. 1350.0
Simple interest = Rs. 612.5

Nested For Loops , Page number: 114

In [3]:
#nested for loops
for r in range(1,4): #outer loop
    for c in range(1,3): #inner loop
        s = r + c #find the sum
        print "r = %d c = %d sum = %d" % (r, c, s) #Display result
    
r = 1 c = 1 sum = 2
r = 1 c = 2 sum = 3
r = 2 c = 1 sum = 3
r = 2 c = 2 sum = 4
r = 3 c = 1 sum = 4
r = 3 c = 2 sum = 5

Do While Loop , Page number: 116

In [5]:
#do while loop
while True:
    #num = raw_input("Enter a number: ")
    num = 11
    print "square of %d is %d"%(num, num * num )
    print "Want to enter another number y/n: " 
    another = 'n'
    print another
    if another == 'y':
        continue
    else:
        break
    
square of 11 is 121
Want to enter another number y/n: 
n

Do While using For Loop, Page number: 117

In [7]:
#Variable declaration
another = 'y'

#do while loop
import sys
for i in range(1,10000): #infinte loop
    #num = raw_input("Enter a number: ")
    num = 11
    print "square of %d is %d"%(num, num * num )
    print "Want to enter another number y/n: " 
    another = 'n'
    print another
    if another == 'y':
        continue
    else:
        break
    
square of 11 is 121
Want to enter another number y/n: 
n

Do While using While Loop, Page number: 117

In [8]:
#Variable declaration
another = 'y'

#do while loop
while another == 'y':
    #num = raw_input("Enter a number: ")
    num = 11
    print "square of %d is %d"%(num, num * num )
    print "Want to enter another number y/n: " 
    another = 'n'
    print another
   
    
square of 11 is 121
Want to enter another number y/n: 
n

Prime Number, Page number: 118

In [9]:
#Input from user
#num = raw_input("Enter a number: ")
num = 11

#Variable declaration
i = 2

#while loop
while i <=(num - 1):
    if num % i == 0:
        print "Not a prime number"  #Display if not prime number
        break
    i += 1

#Display if prime number
if i == num:
    print  "Prime number" 
Prime number

Break Statement , Page number: 119

In [11]:
#Variable declaration
i = 1
j = 1

#while loops
while i <= 100 : #outer loop
    i = i+1
    while j <= 200 : #inner loop
        j = j+1
        if j == 150:
            break #break statement in inner loop
        else:
            print i, j 
            
2 2
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 10
2 11
2 12
2 13
2 14
2 15
2 16
2 17
2 18
2 19
2 20
2 21
2 22
2 23
2 24
2 25
2 26
2 27
2 28
2 29
2 30
2 31
2 32
2 33
2 34
2 35
2 36
2 37
2 38
2 39
2 40
2 41
2 42
2 43
2 44
2 45
2 46
2 47
2 48
2 49
2 50
2 51
2 52
2 53
2 54
2 55
2 56
2 57
2 58
2 59
2 60
2 61
2 62
2 63
2 64
2 65
2 66
2 67
2 68
2 69
2 70
2 71
2 72
2 73
2 74
2 75
2 76
2 77
2 78
2 79
2 80
2 81
2 82
2 83
2 84
2 85
2 86
2 87
2 88
2 89
2 90
2 91
2 92
2 93
2 94
2 95
2 96
2 97
2 98
2 99
2 100
2 101
2 102
2 103
2 104
2 105
2 106
2 107
2 108
2 109
2 110
2 111
2 112
2 113
2 114
2 115
2 116
2 117
2 118
2 119
2 120
2 121
2 122
2 123
2 124
2 125
2 126
2 127
2 128
2 129
2 130
2 131
2 132
2 133
2 134
2 135
2 136
2 137
2 138
2 139
2 140
2 141
2 142
2 143
2 144
2 145
2 146
2 147
2 148
2 149
3 151
3 152
3 153
3 154
3 155
3 156
3 157
3 158
3 159
3 160
3 161
3 162
3 163
3 164
3 165
3 166
3 167
3 168
3 169
3 170
3 171
3 172
3 173
3 174
3 175
3 176
3 177
3 178
3 179
3 180
3 181
3 182
3 183
3 184
3 185
3 186
3 187
3 188
3 189
3 190
3 191
3 192
3 193
3 194
3 195
3 196
3 197
3 198
3 199
3 200
3 201

Continue Statement , Page number: 120

In [12]:
#for loops
for i in range(1,3):
    for j in range(1,3):
        if i==j :
            continue # continue statement
        print  i , j
        
1 2
2 1
In [ ]: