Chapter 5- Control Flow

Example-age1.cpp, Page no-153

In [1]:
age=int(raw_input("Enter your age: "))
if(age>12 and age<20):
    print "you are a teen-aged person. good!"
Enter your age: 15
you are a teen-aged person. good!

Example-age2.cpp, Page no-154

In [2]:
age=int(raw_input("Enter your age: "))
if(age<0):
    print "I am sorry!"
    print "age can never be negative"
if(age>12 and age<20):
    print "you are a teen-aged person. good!"
Enter your age: -10
I am sorry!
age can never be negative

Example-large.cpp, Page no-155

In [1]:
big=float
a, b, c=[float(x) for x in raw_input("Enter three floating-point numbers: ").split()] #taking input in single line separated by white space
big=a
if(b>big):
    big=b
if(c>big):
    big=c
print "Largest of the three numbers =", big
Enter three floating-point numbers: 10.2 15.6 12.8
Largest of the three numbers = 15.6

Example-age3.cpp, Page no-156

In [3]:
age=int(raw_input("Enter your age: "))
if(age>12 and age<20):
    print "you are a teen-aged person. good!"
else:
    print "you are not a teen-aged person."
Enter your age: 15
you are a teen-aged person. good!

Example-lived.cpp, Page no-157

In [2]:
years=float(raw_input("Enter your age in years: "))
if(years<0):
    print "I am sorry! age can never be negative"
else:
    secs=years*365*24*60*60
    print "You have lived for %.4g seconds" %(secs)
Enter your age in years: 25
You have lived for 7.884e+08 seconds

Example-age4.cpp, Page no-158

In [3]:
age=int(raw_input("Enter your age: "))
if(age>12 and age<20):
    print "you are a teen-aged person. good!"
else:
    if(age<13):
        print "you will surely reach teen-age."
    else:
        print "you have crossed teen-age!"
Enter your age: 25
you have crossed teen-age!

Example-count1.cpp, Page no-159

In [1]:
n=int(raw_input("How many integers to be displayed: "))
for i in range(n):
    print i
How many integers to be displayed: 5
0
1
2
3
4

Example-sumsq1.cpp, Page no-160

In [1]:
Sum=0
sum_of_squares=0
for i in range(2, 31, 2):
    Sum+=i
    sum_of_squares+=i*i
print "Sum of first 15 positive even numbers =", Sum
print "Sum of their squares =", sum_of_squares
Sum of first 15 positive even numbers = 240
Sum of their squares = 4960

Example-sumsq2.cpp, Page no-161

In [2]:
Sum=0
sum_of_squares=0
for i in range(30, 0, -2):
    Sum+=i
    sum_of_squares+=i*i
print "Sum of first 15 positive even numbers =", Sum
print "Sum of their squares =", sum_of_squares
Sum of first 15 positive even numbers = 240
Sum of their squares = 4960

Example-noinit.cpp, Page no-162

In [3]:
i=1
for i in range(i, 11):
    print i*5,
5 10 15 20 25 30 35 40 45 50

Example-pyramid.cpp, Page no-163

In [4]:
n=int(raw_input("Enter the number of lines: "))
for p in range(1, n+1):
    for q in range(1, n-p+1):
        print "\t",
    m=p
    for q in range (1, p+1):
        print "\t", m,
        m+=1
    m=m-2
    for q in range(1, p):
        print "\t", m,
        m-=1
    print ''
Enter the number of lines: 5
					1 
				2 	3 	2 
			3 	4 	5 	4 	3 
		4 	5 	6 	7 	6 	5 	4 
	5 	6 	7 	8 	9 	8 	7 	6 	5 

Example-count2.cpp, Page no-164

In [5]:
n=int(raw_input("How many integers to be displayed: "))
i=0
while i<n:
    print i
    i+=1
How many integers to be displayed: 5
0
1
2
3
4

Example-average1.cpp, Page no-164

In [6]:
Sum=0
count=0
print "Enter the marks, -1 at the end..."
marks=int(raw_input())
while marks!=-1:
    Sum+=marks
    count+=1
    marks=int(raw_input())
average=Sum/count
print "The average is", average    
Enter the marks, -1 at the end...
80
75
82
74
-1
The average is 77

Example-bin2deci.cpp, Page no-165

In [7]:
decimal=0
position=0
binary=int(raw_input("Enter the binary number: "))
while binary:
    digit=binary%10
    decimal+=digit<<position
    binary/=10
    position+=1
print "Its decimal equivalent =", decimal
Enter the binary number: 111
Its decimal equivalent = 7

Example-count3.cpp, Page no-166

In [2]:
n=int(raw_input("How many integers to be displayed: "))
i=0
while 1: #do while loop
    print i
    i+=1
    if i>=n:
        break
How many integers to be displayed: 5
0
1
2
3
4

Example-dowhile.cpp, Page no-167

In [3]:
while 1: #do-while loop
    inchar=raw_input("Enter your sex (m/f): ")
    if(inchar=='m' or inchar=='f'):
        break
if inchar=='m':
    print "so you are male. good!"
else:
    print "so you are female. good!"
Enter your sex (m/f): d
Enter your sex (m/f): b
Enter your sex (m/f): m
so you are male. good!

Example-pa1.cpp, Page no-167

In [5]:
rev=0
num=int(raw_input("Enter the number: "))
n=num
while 1:
    digit=num%10
    rev=rev*10 + digit
    num/=10
    if num==0:
        break
print "Reverse of the number =", rev
if n==rev:
    print "The number is a palindrome"
else:
    print "The number is not a palindrome"
Enter the number: 121
Reverse of the number = 121
The number is a palindrome

Example-average2.cpp, Page no-169

In [4]:
Sum=0
count=0
print "Enter the marks, -1 at the end..."
while 1:
    marks=int(raw_input())
    if marks==-1:
        break
    Sum+=marks
    count+=1
average=Sum/count
print "The average is", average    
Enter the marks, -1 at the end...
80
75
82
74
-1
The average is 77

Example-sex2.cpp, Page no-171

In [5]:
ch=raw_input("Enter your sex (m/f): ")
if ch=='m':
    print "So you are male. good!"
elif ch=='f':
    print "So you are female. good!"
else:
    print "Error: Invalid sex code!"
Enter your sex (m/f): m
So you are male. good!

Example-calc.cpp, Page no-172

In [1]:
print "------------------Basic Calculator------------------"
print "Choose an option:"
print "Add"
print "Subtract"
print "Multiply"
print "Divide"
ch=raw_input()
num1, num2=[int(x) for x in raw_input("Enter the value of the operands: ").split()]
if ch=='1':
    print num1+num2
elif ch=='2':
    print num1-num2
elif ch=='3':
    print num1*num2
elif ch=='4':
    print num1/num2
else:
    print "Incorrect choice: "
------------------Basic Calculator------------------
Choose an option:
Add
Subtract
Multiply
Divide
1
Enter the value of the operands: 22 33
55

Example-sumpos.cpp, Page no-174

In [6]:
total=0
while 1:
    num=int(raw_input("Enter a number (0 to quit): "))
    if num==0:
        print "end of data entry."
        break
    if num<0:
        print "skipping this number."
        continue
    total+=num
print "Total of all +ve numbers is ", total
Enter a number (0 to quit): 10
Enter a number (0 to quit): 20
Enter a number (0 to quit): -5
skipping this number.
Enter a number (0 to quit): 10
Enter a number (0 to quit): 0
end of data entry.
Total of all +ve numbers is  40

Example-jump.cpp, Page no-175

In [2]:
total=0
while 1:
    num=int(raw_input("Enter a number (0 to quit): "))
    if num==0:
        print "end of data entry."
        print "Total of all +ve numbers is", total #no goto in python
        break
    if num<0:
        print "skipping this number."
        continue
    total+=num
Enter a number (0 to quit): 10
Enter a number (0 to quit): 20
Enter a number (0 to quit): -5
skipping this number.
Enter a number (0 to quit): 10
Enter a number (0 to quit): 0
end of data entry.
Total of all +ve numbers is 40

Example-age5.cpp, Page no-177

In [3]:
age=int(raw_input("Enter your age: "))
if(age>12 and age<20):
    pass
print "you are a teen-aged person. good!"
Enter your age: 50
you are a teen-aged person. good!

Example-agecmp.cpp, Page no-177

In [1]:
myage=25
print "Hi! my age is ", myage
yourage=int(raw_input("What is your age? "))
if myage==yourage:
    print "We are born on the same day. Are we twins!"
Hi! my age is  25
What is your age? 25
We are born on the same day. Are we twins!

Example Page no-178

In [4]:
l1=int(raw_input("Enter the lower limit: "))
l2=int(raw_input("Enter the higher limit: "))
print "The prime numbers between", l1, "and", l2, "are: ",
for i in range(l1, l2+1):
    if i<=3:
        print i,"\t",
    else:
        for j in range(2, i/2+1):
            if(i%j==0):
                break
        if(i%j==0):
            continue #no goto
        print i, "\t",
Enter the lower limit: 1
Enter the higher limit: 100
The prime numbers between 1 and 100 are:  1 	2 	3 	5 	7 	11 	13 	17 	19 	23 	29 	31 	37 	41 	43 	47 	53 	59 	61 	67 	71 	73 	79 	83 	89 	97