XII COMPUTER SCIENCE -CONTROL STRUCTURES
#1. sequential statements
tamil=int(input(" ENTER YOUR TAMIL MARK : "))
print(type(tamil))
english=int(input(" ENTER YOUR ENGLISH MARK : "))
tot=tamil+english
print(" ================***********================== ")
print( " TOTAL MARKS " , tot)
print()
#2. sequential statements
name=input(" ENTER YOUR NAME : ")
age=input(" ENTER YOUR AGE ")
address=input(" ENTER YOUR ADDRESS ")
tamil=int(input(" ENTER YOUR TAMIL MARK : "))
english=int(input(" ENTER YOUR TAMIL MARK : "))
tot=tamil+english
print(" ===============***********================== ")
print( " NAME ",name,"\t AGE ",age)
print( " \n PERMANANT ADDRESS ",address)
print( " TOTAL MARKS " , tot)
#3. simple if()
x=int(input("enter the number"))
if x>18:
print("YOU ARE ELIGIBLE TO VOTE")
...................................................................................................
#4.if..else
x=int(input("enter the number"))
if x>18:
print("YOU ARE ELIGIBLE TO VOTE")
else :
print("YOU ARE NOT ELIGIBLE TO VOTE")
........................................................................................................
#5.if..elif..else
age=int(input(" enter the number "))
if age<3:
print(" child NEED NOT TO PAY ")
elif age>3 and age<60: # age is 3-59
print(" YOU NEED TO PAY full amount ")
else:
print("YOU ARE SENIOR CITIZEN PAY just 40% OF TICKET")
#6.nested if
n=int(input(" enter the number "))
if n>90:
if n<=100: #91-100
print(" A GRADE")
if n>80:
if n<=90: #81 - 90
print(" B GRADE ")
if n>60:
if n<=80: #61 - 80
print(" C GRADE ")
if n>=40:
if n<=60: #40 - 60
print(" D GRADE ")
else:
print("SORRY,YOU ARE FAIL")
print()
............................................................................................
#7.elif ladder
n=int(input(" enter the number "))
if n>90 and n<=100:
print(" A GRADE")
elif n>80 and n<=90:
print(" B GRADE ")
elif n>60 and n<=80:
print(" C GRADE ")
elif n>40 and n<=60:
print(" D GRADE ")
else:
print("SORRY,YOU ARE FAIL")
print()
................................................................................................
#8. for()ex.1
for i in range(10): #by default series start from 0 ,here 10 is exclusive so 10-1=9
print(i,end=' \t ')
print()
#O/P- 0 1 2 3 4 5 6 7 8 9
.................................................................................................
#9.for() loop ex.2
for i in range(5,50): #here series start from 5 ,50 is exclusive so 50-1=49
print(i,end=' ') #o/p : 5 6 7 8 .....49
print()
print()
................................................................................................
#10.for() loop ex.3
for i in range(0,99,4): #here series start from 0 and 99 is exclusive so end value is not 99-1=98 or 99-4=95
print(i,end=' ') #o/p : 0 4 8 12 16 .....92 96
print()
print()
................................................................................................
#11.for() loop ex.4
for i in range(10,50,3): #here series start from 10 ,50 is exclusive so end value is less than 50
print(i,end=' ') #o/p : 10 13 16 19 ......49
print() # give this values and test it (10,50,5) (10,50,3+2) &(100,0,-5)
………………………………………………………………………………………………
#12.while() loop
i=5 # i is start value
while(i<10): # end value based on condition
print(i,end=' ')
i=i+3 # updation : here i++ will not work because unary operator are not accepted
...............................................................................................
#13.nested while loop
n=int(input(" ENTER END VALUE "))
i=0
while i<n:
j=0
while j<n:
print(i,end=' ')
j=j+1
print("\n")
i=i+1
14.break statements
list=[1,3,6,-5,7,8]
print("Total no.of item in list",len(list))
for i in list:
if i<0:
break
else:
print(i)
print()
print()
print()
15.continue statements
list=[1,3,6,-5,7,8]
print("Total no.of item in list",len(list))
for i in range(len(list)):
print(list[i],end=' ')
print(" \n..............................***************............... " )
for i in list:
if i<0:
continue
else:
print(i)
16.pass statements
for i in range(5):
pass
print(" pass statement treated as a place holder " )
17.while ..else
i=9
while(i<5):
print(i,end=' ')
i+=1
else:
print("else statement")
18.for …else
for i in range (30 , 4 , -3):
print(i,end=' ')
else:
print("else statement")
19.while nest while
n=int(input("enter num"))
i=0
while(i<n):
j=0
while(j<n):
print(i,end=' ')
j+=1
print()
i+=1
print( )
20.for nest for
for i in range(5)
for j in range(3)
print( i,end=” “ )
21.for nest while
for i in range(5):
j=0
while(j<5):
print(i,end=' ')
j+=1
print( )
i+=1
…..
22.while nest for
i=0
while(i<5):
for j in range(1,7):
print( i, end=' ')
print( )
i+=1
23.pattern1
for i in range(5):
for j in range (5-i-1):
print(' * ',end =' ')
print()
output: if range (5-i-1)
* * * *
* * *
* *
*
output:if range (i+1)
*
* *
* * *
* * * *
* * * * *
24.pattern2
for i in range(5):
for j in range (i+1):
print(' * ' * 4,end =' ')
output:
* * * *
* * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
25.pattern-3
for i in range(5):
print( "$",end=' ' )
for j in range (i+1):
print(i * 2,end =' ')
print( )
OUTPUT :
$0
$2 2
$4 4 4
$6 6 6 6
$8 8 8 8 8
26.pattern-4
for i in range(5):
print( "$",end=' ' )
for j in range (i+1):
print(" * " , end=' ' )
for k in range(j+1):
print(i * 2,end =' ')
print()
print()
OUTPUT :
$ * 0
$ * 2 * 2 2
$ * 4 * 4 4 * 4 4 4
$ * 6 * 6 6 * 6 6 6 * 6 6 6 6
$ * 8 * 8 8 * 8 8 8 * 8 8 8 8 * 8 8 8 8
Prepared By
C VADIVELMURUGAN M.Sc.,B.Ed.,M.Phil.,DTP.,
9600519206