# Chapter 7: Decision Making & Looping¶

## example 7.1, page no. 108¶

In [3]:
"""
the method shown in textbook is not possible in python.
We will implement in a different way
"""

print "Enter a string. Enter quit to stop"

my_str = ""
while True:
c = raw_input()
if c == "quit":
break
my_str = my_str + c + " "
print "String you entered is: ", my_str

Enter a string. Enter quit to stop
Java
is
Good
quit
String you entered is:  Java is Good


## example 7.2, page no. 109¶

In [6]:
#Do.. While loop

row = 1
column = 1

while row <= 3:
column = 1
while column <= 3:
y = row * column
print " ", y,
column += 1
print ""
row += 1

  1   2   3
2   4   6
3   6   9


## example 7.3, page no. 112¶

In [20]:
print "2 to power-n   n          2 to power n"
p = 1
for n in range(0,10):
if n == 0:
p = 1
else:
p = p * 2
q = float(1.0/p)
print q,"          ",n,"          ",p

2 to power-n   n          2 to power n
1.0            0            1
0.5            1            2
0.25            2            4
0.125            3            8
0.0625            4            16
0.03125            5            32
0.015625            6            64
0.0078125            7            128
0.00390625            8            256
0.001953125            9            512


## example 7.4, page no. 116¶

In [1]:
states = ["TamilNadu", "AndhraPradesh", "UttarPradesh", "Rajasthan"]
for i in range(0, len(states)):
print "Standard for-loop: state name: ", states[i]
print ""
for state in states:
print "Enhanced for-loop: state name: ", state

print ""
cities = []
cities.append("Delhi")
cities.append("Mumbai")
cities.append("Calcutta")
cities.append("Chennai")

for i in range(0, len(cities)):
print "Standard for-loop: state name: ", cities[i]
print ""
for city in cities:
print "Enhanced for-loop: enhanced name: ", city

print "\nIn Collection"
for i in cities:
print i

Standard for-loop: state name:  TamilNadu
Standard for-loop: state name:  AndhraPradesh
Standard for-loop: state name:  UttarPradesh
Standard for-loop: state name:  Rajasthan

Enhanced for-loop: state name:  TamilNadu
Enhanced for-loop: state name:  AndhraPradesh
Enhanced for-loop: state name:  UttarPradesh
Enhanced for-loop: state name:  Rajasthan

Standard for-loop: state name:  Delhi
Standard for-loop: state name:  Mumbai
Standard for-loop: state name:  Calcutta
Standard for-loop: state name:  Chennai

Enhanced for-loop: enhanced name:  Delhi
Enhanced for-loop: enhanced name:  Mumbai
Enhanced for-loop: enhanced name:  Calcutta
Enhanced for-loop: enhanced name:  Chennai

In Collection
Delhi
Mumbai
Calcutta
Chennai


## example 7.5, page no. 120¶

In [28]:
for i in range(1,100):
print " "
if i >= 10:
break
for j in range(1, 100):
print " * ",
if j == i:
break


*
*   *
*   *   *
*   *   *   *
*   *   *   *   *
*   *   *   *   *   *
*   *   *   *   *   *   *
*   *   *   *   *   *   *   *
*   *   *   *   *   *   *   *   *