# CHAPTER 3: THE LOOP CONTROL STRUCTURE¶

## EXAMPLE ON PAGE:89-90¶

In [1]:

count=1
while count<=3:                                    #while loop codition check
print "Enter values of p,n and r"
p=eval(raw_input())
n=eval(raw_input())
r=eval(raw_input())
si=p*n*r/100
print "Simple interest=Rs.%f" % (si)
count=count+1                                  #increment
Enter values of p,n and r
1000
5
13.5
Simple interest=Rs.675.000000
Enter values of p,n and r
2000
5
13.5
Simple interest=Rs.1350.000000
Enter values of p,n and r
3500
5
3.5
Simple interest=Rs.612.500000

## EXAMPLE ON PAGE:92¶

In [ ]:

i=1
while i<=10:
print "%d\n" % (i)                      #there is no increment/decrement thats why indefinite loop

## EXAMPLE ON PAGE:93¶

In [2]:

i=1
while i<=10:
print "\n" ,i
i=i+1
1

2

3

4

5

6

7

8

9

10

## EXAMPLE ON PAGE:93¶

In [3]:

i=5
while i>=1:
print "Make the computer literate!\n"
i=i-1
Make the computer literate!

Make the computer literate!

Make the computer literate!

Make the computer literate!

Make the computer literate!

## EXAMPLE ON PAGE:93¶

In [4]:

a=10.0
while a<=10.5:
print "Raindrops on roses..."
print "...and whiskers on kittens\n"
a=a+0.1
Raindrops on roses...
...and whiskers on kittens

Raindrops on roses...
...and whiskers on kittens

Raindrops on roses...
...and whiskers on kittens

Raindrops on roses...
...and whiskers on kittens

Raindrops on roses...
...and whiskers on kittens

Raindrops on roses...
...and whiskers on kittens

## EXAMPLE ON PAGE:94¶

In [ ]:

i=1
while i<=32767:                                  #print numbers from 1 to 32767
print "%d\n" ,i
i=i+1

In [5]:

i=1
while i<=10:
print i
i=i+1
1
2
3
4
5
6
7
8
9
10

## EXAMPLE ON PAGE:96¶

In [6]:

i=1
while i<=10:
print "\n" ,i
i+=1                                       #increment short hand operator
1

2

3

4

5

6

7

8

9

10

## EXAMPLE ON PAGE:98¶

In [7]:

for count in range(1,4,1):                          #for loop
print "Enter values of p,n and r"
p=eval(raw_input())
n=eval(raw_input())
r=eval(raw_input())
si=p*n*r/100
print "Simple Interest=Rs.%f" % (si)
Enter values of p,n and r
1000
5
13.5
Simple Interest=Rs.675.000000
Enter values of p,n and r
2000
5
13.5
Simple Interest=Rs.1350.000000
Enter values of p,n and r
3500
5
3.5
Simple Interest=Rs.612.500000

## EXAMPLE ON PAGE:101¶

In [8]:

for i in range(1,11,1):
print "%d\n" % (i)
1

2

3

4

5

6

7

8

9

10

## EXAMPLE ON PAGE:101¶

In [9]:

for i in range(1,11):
print "\n" ,i
i=i+1
1

2

3

4

5

6

7

8

9

10

## EXAMPLE ON PAGE:101¶

In [10]:

i=1
for i in range(i,11,1):
print "%d\n" % (i)
1

2

3

4

5

6

7

8

9

10

## EXAMPLE ON PAGE:102¶

In [11]:

i=1
for i in range(i,11):
print "%d\n" % (i)
i=i+1
1

2

3

4

5

6

7

8

9

10

## EXAMPLE ON PAGE:103¶

In [12]:

for r in range(1,4,1):                              #outer loop
for c in range(1,3,1):                          #inner loop
sum=r+c
print "r=%d c=%d sum=%d\n" % (r,c,sum)
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

## EXAMPLE ON PAGE:104-105¶

In [1]:

while(1):                                             #do-while loop is not present in python
print "Enter a number"
num=eval(raw_input())
print "square of %d is %d\n" % (num,num*num)
print "Want to enter another number y/n"
another=raw_input()
if(another!='y'):
break
Enter a number
5
square of 5 is 25

Want to enter another number y/n
y
Enter a number
7
square of 7 is 49

Want to enter another number y/n
n

## EXAMPLE ON PAGE:105¶

In [13]:

another='y'
for i in range(1,1000):                                #check if another is in word
if another=='y':
print "Enter a number"
num=eval(raw_input())
print "square of %d is %d\n" % (num,num*num)
print "Want to enter another number y/n"
another=raw_input()
Enter a number
5
square of 5 is 25

Want to enter another number y/n
y
Enter a number
7
square of 7 is 49

Want to enter another number y/n
n

## EXAMPLE ON PAGE:105-106¶

In [14]:

another='y'
while(another=='y'):
print "Enter a number"
num=eval(raw_input())
print "square of %d is %d\n" % (num,num*num)
print "Want to enter another number y/n"
another=raw_input()
Enter a number
5
square of 5 is 25

Want to enter another number y/n
y
Enter a number
7
square of 7 is 49

Want to enter another number y/n
n

## EXAMPLE ON PAGE:106¶

In [15]:

print "Enter a number"
num=eval(raw_input())
i=2
while i<=num-1:
if num%i==0:
print "Not a prime number\n"
break                                           #exit the loop
i+=1
if i==num:
print "Prime number\n"
Enter a number
11
Prime number

## EXAMPLE ON PAGE:107¶

In [16]:

i=1
j=1
while (i<=100):
i+=1
while (j<=200):
j+=1
if (j==150):
break                        #it will terminate the inner loop only
else:
print "%d %d\n" % (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

## EXAMPLE ON PAGE:108¶

In [17]:

for i in range(1,3,1):
for j in range(1,3,1):
if i==j:
continue                #it will again send back to loop without executing succeeding statements
print "%d %d\n" % (i,j)
1 2

2 1