# Chapter 6, The While and do...While Loops¶

## Program 6-1 , Page number: 86¶

In :
# A first look at the while loop

i = 1

while i < 11:
print "This is line number %d"%i
i += 1 			# Python doesn't support "++" or "--" shorthand expressions

This is line number 1
This is line number 2
This is line number 3
This is line number 4
This is line number 5
This is line number 6
This is line number 7
This is line number 8
This is line number 9
This is line number 10


## Program 6-2 , Page number: 89¶

In :
# Countdown to blastoff

i = 10

print "Countdown"

while i >= 0 :
print "%d"%i
i -= 1

print "\n\nBlastoff!"

Countdown
10
9
8
7
6
5
4
3
2
1
0

Blastoff!


## Program 6-3 , Page number: 90¶

In :
# Hailstones

counter = 0;

print "Enter a positive integer:",
n = 51
print n

nsave = n

while n > 1:
if n % 2 :			# if (n % 2) is non-zero, n is not divisible by 2 and is therefore odd
n = n * 3 + 1
else :
n /= 2
counter += 1
print "Step %4d: n = %4d"%(counter,n)

print "\n\n%d went to 1 in %d steps."%(nsave,counter)

Enter a positive integer: 51
Step    1: n =  154
Step    2: n =   77
Step    3: n =  232
Step    4: n =  116
Step    5: n =   58
Step    6: n =   29
Step    7: n =   88
Step    8: n =   44
Step    9: n =   22
Step   10: n =   11
Step   11: n =   34
Step   12: n =   17
Step   13: n =   52
Step   14: n =   26
Step   15: n =   13
Step   16: n =   40
Step   17: n =   20
Step   18: n =   10
Step   19: n =    5
Step   20: n =   16
Step   21: n =    8
Step   22: n =    4
Step   23: n =    2
Step   24: n =    1

51 went to 1 in 24 steps.


## Program 6-4 , Page number: 92¶

In :
# Computing the factorial

print "Type in a positive integer:",
n = 6
print n

nsave = n
factorial = 1

while n > 1 :
factorial *= n
n -= 1

print "Factorial of %d = %d"%(nsave,factorial)

Type in a positive integer: 6
Factorial of 6 = 720


## Program 6-5 , Page number: 93¶

In :
# Computing the factorial (shorter version)

print "Type in a positive integer: ",
n = 6
print n

nsave = n
factorial = 1

while n > 1 :
factorial *= n      # Since python doesn't support '--' expression, the given program cannot be shortened
n -= 1

print "Factorial of %d = %d"%(nsave,factorial)

Type in a positive integer:  6
Factorial of 6 = 720


## Program 6-6 , Page number: 95¶

In :
# Illustration of do..while loops

# This program reads in an integer, adds up the values of each of its individual digits, and prints out the sum.

sum = 0

print "Type in an integer:",
n = 1776
print n

# There is no do...while loop in python
# An equivalent while loop can work

while n > 0 :
rightmost_digit = n % 10	# Extract rightmost digit
sum += rightmost_digit
n /= 10						# Move next digit into rightmost position

print "The sum of the digits is %d"%sum

Type in an integer: 1776
The sum of the digits is 21


## Program 6-7 , Page number: 97¶

In :
import sys

# This program reads in an integer and prints it out backwards

print "Type in an integer: ",
n = 5746
print n

print "%d backwards is "%n,

while n > 0 :
rightmost_digit = n % 10
sys.stdout.write("%d"%rightmost_digit)  # For printing in the same line without implicit whitespaces
n /= 10
print 			# For the new line

Type in an integer:  5746
5746 backwards is 6475


## Program 6-8 , Page number: 99¶

In :
# A program to read in numbers and print out their squares

print "Enter an integer (negative to quit):",
n = 5
print n

while n > 0 :
print "%d squared is %d"%(n,n*n)
print "\nEnter an integer (negative to quit):",
n = -1
print n

Enter an integer (negative to quit): 5
5 squared is 25

Enter an integer (negative to quit): -1


## Program 6-9 , Page number: 100¶

In :
# A program to read in radius values and print out the circumference and area

pi = 3.1416

print "Enter radius (negative to quit):",

while radius >= 0.0 :
print "The circumference is %f"%(2.0 * pi * radius)
print "The area is %f"%(pi * radius * radius)
print "\nEnter radius (negative to quit):",


Enter radius (negative to quit): 4.5
The circumference is 28.274400
The area is 63.617400

Enter radius (negative to quit): -1.0


## Program 6-10 , Page number: 101¶

In :
# A program to read in length and width and print out the perimeter and area

print "Enter length and width"
print " (one or both negative to quit):",
length = 4
width = 5
print length,width

while length > 0 and width > 0 :
print "The perimeter is %d"%(2*(length+width))
print "The area is %d"%(length*width)
print "\nEnter length and width"
print " (one or both negative to quit):",
length = -1
width = 0
print length,width

Enter length and width
(one or both negative to quit): 4 5
The perimeter is 18
The area is 20

Enter length and width
(one or both negative to quit): -1 0