Class 8 Exam  >  Class 8 Notes  >  Computer Science for Class 8  >  Textbook: Basic

Textbook: Basic | Computer Science for Class 8 PDF Download

Download, print and study this document offline
Please wait while the PDF view is loading
 Page 1


Moz: What are Fibonacci numbers and how do you get the sequence of Fibonacci numbers?
Jy oti:	In	mathematics,	the	Fibonacci	numbers	are	the	numbers	in	the	following	integer	sequence:
0, 1, 1, 2, 3, 5, 8, 13, 21....... and so on.
T ejas:	 The	 first	 tw o	 numbers	 in	 the	 Fibonacci	
sequence are 0 and 1. Each subsequent number is 
the sum of the previous two numbers. For example, 
the next number in the sequence shown above is  
13	+	21	=	34.
Jy oti:	 Let	 us	 write	 a	 program	 to	 generate	 the	 Fibonacci	
sequence of numbers, upto 1000.
T ejas:	 Let	 us	 first	 write	 the	 algorithm	 for	 generating	 the	
sequence, by trying out an example.
T ejas:	 In	 our	 Science	 class	 w e	 discov ered	
that the number of petals on flowers is 
mostly one of the numbers in a sequence 
called the Fibonacci sequence. 
13 petals 5 petals
Aim: In this lesson, you will learn:
- Repetition of statements with condition.
- Defining	and	executing	subroutines.
- String functions.
BASIC
(We	need	three	variables) 1.	
						Let	a	=	previous	number,	b	=	current	number,	c	=	next	sequence	number.
Ask	the	user	to	input	a	number	upto	which	fibonacci	numbers	should	be	generated. 2.	
Print	the	first	two	fibonacci	numbers. 3.	
As	long	as	c	<	1000	repeat	the	following. 4.	
						 (Calculate	next	fibonacci	number	which	is	the	sum	of	previous	numbers	and	current		 
												numbers.)	
	 Next	fibonacci	sequence	number	c	=	a+b.
	 If		c	<	1000		print	the	fibonacci	numbers.
	 (The	 previous	 number	 and	 the	 current	 number	 shift,	 that	 is,	 they	 take	 the	 next	 values	 in	 	 	 	 
												the	sequence.)
	 previous	number	a	=	b.
									 Current	number	b	=	c. 
Examples
1 + 1 = 2
       1 + 2 = 3
    2 + 3 = 5
                     3 + 5 = 8
                            5 + 8 = 13
1, 1, 2, 3, 5, 8, 13, 21, ....
Sequence Generation
8
Algorithm
Memory
0, 1, 1, 2, 3, 
5, 8, 13, 21, 
34, ...
while b < num
Page 2


Moz: What are Fibonacci numbers and how do you get the sequence of Fibonacci numbers?
Jy oti:	In	mathematics,	the	Fibonacci	numbers	are	the	numbers	in	the	following	integer	sequence:
0, 1, 1, 2, 3, 5, 8, 13, 21....... and so on.
T ejas:	 The	 first	 tw o	 numbers	 in	 the	 Fibonacci	
sequence are 0 and 1. Each subsequent number is 
the sum of the previous two numbers. For example, 
the next number in the sequence shown above is  
13	+	21	=	34.
Jy oti:	 Let	 us	 write	 a	 program	 to	 generate	 the	 Fibonacci	
sequence of numbers, upto 1000.
T ejas:	 Let	 us	 first	 write	 the	 algorithm	 for	 generating	 the	
sequence, by trying out an example.
T ejas:	 In	 our	 Science	 class	 w e	 discov ered	
that the number of petals on flowers is 
mostly one of the numbers in a sequence 
called the Fibonacci sequence. 
13 petals 5 petals
Aim: In this lesson, you will learn:
- Repetition of statements with condition.
- Defining	and	executing	subroutines.
- String functions.
BASIC
(We	need	three	variables) 1.	
						Let	a	=	previous	number,	b	=	current	number,	c	=	next	sequence	number.
Ask	the	user	to	input	a	number	upto	which	fibonacci	numbers	should	be	generated. 2.	
Print	the	first	two	fibonacci	numbers. 3.	
As	long	as	c	<	1000	repeat	the	following. 4.	
						 (Calculate	next	fibonacci	number	which	is	the	sum	of	previous	numbers	and	current		 
												numbers.)	
	 Next	fibonacci	sequence	number	c	=	a+b.
	 If		c	<	1000		print	the	fibonacci	numbers.
	 (The	 previous	 number	 and	 the	 current	 number	 shift,	 that	 is,	 they	 take	 the	 next	 values	 in	 	 	 	 
												the	sequence.)
	 previous	number	a	=	b.
									 Current	number	b	=	c. 
Examples
1 + 1 = 2
       1 + 2 = 3
    2 + 3 = 5
                     3 + 5 = 8
                            5 + 8 = 13
1, 1, 2, 3, 5, 8, 13, 21, ....
Sequence Generation
8
Algorithm
Memory
0, 1, 1, 2, 3, 
5, 8, 13, 21, 
34, ...
while b < num
T ejas:	 The	 repeat	 statement	 “For ”	 	 	 	 	 	 	 	For statement will not work here as we do not know the number 
of times that we need to repeat a set of statements. Let us see if we have other repeat statements that 
we can use.
Jy oti:	W e	can	use	“While”	statement.	
Moz: Such repetitive statements such as for and while are called Loops.
Jy oti:	 W e	 can	 also	 conv ert	 text	 to	 speech.	 In	 the	 abov e	 program	 before	 the	 “input”	 statement	 w e	 can	
put the following statement and the computer reads out the text. 
T ejas:	W ow!	This	is	cool!	
1
Info
Text	to	Speech	
Syntax
say expression 
Say	“Hello	how	are	you?”
The say statement is used to 
make	BASIC-256	read	an	ex-
pression	aloud,	through	the	com-
puter's	speakers.	
																			Hello	How	are	you
PROGRAM
Jy oti:	Now	I	w ant	to	build	a	program	for	a	simple	calculator .
T ejas:	Let	us	write	the	algorithm	for	the	calculator .
For statement executes a specified block of code a specified number of times, and keeps track of the value of the variable. • 
Recall
Enter a number: 1000
Fibonacci sequence upto 1000
1, 1, 2, 3, 5, 8, 13, 21, 
34, 55, 89, 144, 233, 
377, 610, 987
PROGRAM
OUTPUT
Info
Loop:	Repeat	-	Do	something	while	a	condition	remains	true
Syntax 
while condition 
    statement(s) 
end while
Do the statements in the block •	
over	and	over	again	while the 
condition	is	true.	
The statements will be •	
executed	zero	or	more	times.	
i=8
while i<10
Print “Hello World” 
i=i+1
end while 
PROGRAM
Hello World
Hello World
OUTPUT
}
Prints twice because 
the loop is executed 
twice and then value 
of i becomes 10.
Page 3


Moz: What are Fibonacci numbers and how do you get the sequence of Fibonacci numbers?
Jy oti:	In	mathematics,	the	Fibonacci	numbers	are	the	numbers	in	the	following	integer	sequence:
0, 1, 1, 2, 3, 5, 8, 13, 21....... and so on.
T ejas:	 The	 first	 tw o	 numbers	 in	 the	 Fibonacci	
sequence are 0 and 1. Each subsequent number is 
the sum of the previous two numbers. For example, 
the next number in the sequence shown above is  
13	+	21	=	34.
Jy oti:	 Let	 us	 write	 a	 program	 to	 generate	 the	 Fibonacci	
sequence of numbers, upto 1000.
T ejas:	 Let	 us	 first	 write	 the	 algorithm	 for	 generating	 the	
sequence, by trying out an example.
T ejas:	 In	 our	 Science	 class	 w e	 discov ered	
that the number of petals on flowers is 
mostly one of the numbers in a sequence 
called the Fibonacci sequence. 
13 petals 5 petals
Aim: In this lesson, you will learn:
- Repetition of statements with condition.
- Defining	and	executing	subroutines.
- String functions.
BASIC
(We	need	three	variables) 1.	
						Let	a	=	previous	number,	b	=	current	number,	c	=	next	sequence	number.
Ask	the	user	to	input	a	number	upto	which	fibonacci	numbers	should	be	generated. 2.	
Print	the	first	two	fibonacci	numbers. 3.	
As	long	as	c	<	1000	repeat	the	following. 4.	
						 (Calculate	next	fibonacci	number	which	is	the	sum	of	previous	numbers	and	current		 
												numbers.)	
	 Next	fibonacci	sequence	number	c	=	a+b.
	 If		c	<	1000		print	the	fibonacci	numbers.
	 (The	 previous	 number	 and	 the	 current	 number	 shift,	 that	 is,	 they	 take	 the	 next	 values	 in	 	 	 	 
												the	sequence.)
	 previous	number	a	=	b.
									 Current	number	b	=	c. 
Examples
1 + 1 = 2
       1 + 2 = 3
    2 + 3 = 5
                     3 + 5 = 8
                            5 + 8 = 13
1, 1, 2, 3, 5, 8, 13, 21, ....
Sequence Generation
8
Algorithm
Memory
0, 1, 1, 2, 3, 
5, 8, 13, 21, 
34, ...
while b < num
T ejas:	 The	 repeat	 statement	 “For ”	 	 	 	 	 	 	 	For statement will not work here as we do not know the number 
of times that we need to repeat a set of statements. Let us see if we have other repeat statements that 
we can use.
Jy oti:	W e	can	use	“While”	statement.	
Moz: Such repetitive statements such as for and while are called Loops.
Jy oti:	 W e	 can	 also	 conv ert	 text	 to	 speech.	 In	 the	 abov e	 program	 before	 the	 “input”	 statement	 w e	 can	
put the following statement and the computer reads out the text. 
T ejas:	W ow!	This	is	cool!	
1
Info
Text	to	Speech	
Syntax
say expression 
Say	“Hello	how	are	you?”
The say statement is used to 
make	BASIC-256	read	an	ex-
pression	aloud,	through	the	com-
puter's	speakers.	
																			Hello	How	are	you
PROGRAM
Jy oti:	Now	I	w ant	to	build	a	program	for	a	simple	calculator .
T ejas:	Let	us	write	the	algorithm	for	the	calculator .
For statement executes a specified block of code a specified number of times, and keeps track of the value of the variable. • 
Recall
Enter a number: 1000
Fibonacci sequence upto 1000
1, 1, 2, 3, 5, 8, 13, 21, 
34, 55, 89, 144, 233, 
377, 610, 987
PROGRAM
OUTPUT
Info
Loop:	Repeat	-	Do	something	while	a	condition	remains	true
Syntax 
while condition 
    statement(s) 
end while
Do the statements in the block •	
over	and	over	again	while the 
condition	is	true.	
The statements will be •	
executed	zero	or	more	times.	
i=8
while i<10
Print “Hello World” 
i=i+1
end while 
PROGRAM
Hello World
Hello World
OUTPUT
}
Prints twice because 
the loop is executed 
twice and then value 
of i becomes 10.
Moz: You can use a subroutine for each of the operations. 
T ejas:	What	is	a	subroutine?
Moz: Sometimes we want to repeat a block of code multiple times. We use a loop (For and while 
statement) when we want to repeat the block, immediately. But, if we want to repeat the block of code 
at different times, then we use a subroutine. When we give a label to the block of code, it becomes a 
subroutine.	 Whenev er	 y ou	 w ant	 to	 execute	 this	 block	 of	 statements,	 y ou	 just	 use	 the gosub statement. 
This is called a call to the subroutine. 
Jy oti:	Why	should	w e	use	subroutines?
Moz: Subroutines help you to understand a program easily. Look at the following example. Explain 
what the program does?
Jy oti:	 This	 is	 a	 simple	 calculator .	 Depending	 on	 the	 choice	 of	 the	 operation	 the	 program	 just	 calls	 the	
subroutine to do the calculation.
Jy oti:	Can	w e	call	the	subroutine	any	number	of	times.	
Moz: Yes. You can. 
T ejas:	Understanding	someone	else’ s	program	will	be	easy	if	there	are	subroutines.
Moz: Additionally there should also be comments.      Comments
Jy oti:	 When	 w e	 w ork	 in	 teams,	 w e	 can	 divide	 a	 program	 into	 subroutines	 for	 each	 task,	 and	 subtasks.	
Each one of us can write a part of the code and also independently test it. Then we can combine the 
subroutines into the main program.
Moz: Right. 
1
Algorithm for the calculator
Ask	 	 the	 user	 	 for	 the	 opera tion	 to	 be	 performed.	 (Addition,	 subtraction,	 multiplication,	 1.	
division).
Ask	the	user	to	enter	two	numbers	num1,	num2	for	the	operation. 2.	
Use	a	condition	block	to	identify	the	operation	selected	by	the	user. 3.	
Write	statements	for	performing	each	operation.	 4.	
Print	the	result. 5.	
Comment statements (Rem and #) help in understanding the flow of the program and debugging the program. • 
Recall
Algorithm
PROGRAM
#rem A simple calculator
menu:
print “Enter your choice of mathematical operation” 
print “1- Additon” ;
print “2- Subtraction” ;
print “3- Multiplication” ;
print “4- Division” 
input “>” , choice 
input “Enter number1>” , num1
input “Enter number2>” , num2 
if choice=1 then gosub addition
if choice=2 then gosub subtract
if choice=3 then gosub mult
if choice=4 then gosub divison
end
addition:
result = num1 + num2
Print num1 +”+”+ num2+ “=”+ result
return
# rem similar subroutines are written for subtract, mult, division
Page 4


Moz: What are Fibonacci numbers and how do you get the sequence of Fibonacci numbers?
Jy oti:	In	mathematics,	the	Fibonacci	numbers	are	the	numbers	in	the	following	integer	sequence:
0, 1, 1, 2, 3, 5, 8, 13, 21....... and so on.
T ejas:	 The	 first	 tw o	 numbers	 in	 the	 Fibonacci	
sequence are 0 and 1. Each subsequent number is 
the sum of the previous two numbers. For example, 
the next number in the sequence shown above is  
13	+	21	=	34.
Jy oti:	 Let	 us	 write	 a	 program	 to	 generate	 the	 Fibonacci	
sequence of numbers, upto 1000.
T ejas:	 Let	 us	 first	 write	 the	 algorithm	 for	 generating	 the	
sequence, by trying out an example.
T ejas:	 In	 our	 Science	 class	 w e	 discov ered	
that the number of petals on flowers is 
mostly one of the numbers in a sequence 
called the Fibonacci sequence. 
13 petals 5 petals
Aim: In this lesson, you will learn:
- Repetition of statements with condition.
- Defining	and	executing	subroutines.
- String functions.
BASIC
(We	need	three	variables) 1.	
						Let	a	=	previous	number,	b	=	current	number,	c	=	next	sequence	number.
Ask	the	user	to	input	a	number	upto	which	fibonacci	numbers	should	be	generated. 2.	
Print	the	first	two	fibonacci	numbers. 3.	
As	long	as	c	<	1000	repeat	the	following. 4.	
						 (Calculate	next	fibonacci	number	which	is	the	sum	of	previous	numbers	and	current		 
												numbers.)	
	 Next	fibonacci	sequence	number	c	=	a+b.
	 If		c	<	1000		print	the	fibonacci	numbers.
	 (The	 previous	 number	 and	 the	 current	 number	 shift,	 that	 is,	 they	 take	 the	 next	 values	 in	 	 	 	 
												the	sequence.)
	 previous	number	a	=	b.
									 Current	number	b	=	c. 
Examples
1 + 1 = 2
       1 + 2 = 3
    2 + 3 = 5
                     3 + 5 = 8
                            5 + 8 = 13
1, 1, 2, 3, 5, 8, 13, 21, ....
Sequence Generation
8
Algorithm
Memory
0, 1, 1, 2, 3, 
5, 8, 13, 21, 
34, ...
while b < num
T ejas:	 The	 repeat	 statement	 “For ”	 	 	 	 	 	 	 	For statement will not work here as we do not know the number 
of times that we need to repeat a set of statements. Let us see if we have other repeat statements that 
we can use.
Jy oti:	W e	can	use	“While”	statement.	
Moz: Such repetitive statements such as for and while are called Loops.
Jy oti:	 W e	 can	 also	 conv ert	 text	 to	 speech.	 In	 the	 abov e	 program	 before	 the	 “input”	 statement	 w e	 can	
put the following statement and the computer reads out the text. 
T ejas:	W ow!	This	is	cool!	
1
Info
Text	to	Speech	
Syntax
say expression 
Say	“Hello	how	are	you?”
The say statement is used to 
make	BASIC-256	read	an	ex-
pression	aloud,	through	the	com-
puter's	speakers.	
																			Hello	How	are	you
PROGRAM
Jy oti:	Now	I	w ant	to	build	a	program	for	a	simple	calculator .
T ejas:	Let	us	write	the	algorithm	for	the	calculator .
For statement executes a specified block of code a specified number of times, and keeps track of the value of the variable. • 
Recall
Enter a number: 1000
Fibonacci sequence upto 1000
1, 1, 2, 3, 5, 8, 13, 21, 
34, 55, 89, 144, 233, 
377, 610, 987
PROGRAM
OUTPUT
Info
Loop:	Repeat	-	Do	something	while	a	condition	remains	true
Syntax 
while condition 
    statement(s) 
end while
Do the statements in the block •	
over	and	over	again	while the 
condition	is	true.	
The statements will be •	
executed	zero	or	more	times.	
i=8
while i<10
Print “Hello World” 
i=i+1
end while 
PROGRAM
Hello World
Hello World
OUTPUT
}
Prints twice because 
the loop is executed 
twice and then value 
of i becomes 10.
Moz: You can use a subroutine for each of the operations. 
T ejas:	What	is	a	subroutine?
Moz: Sometimes we want to repeat a block of code multiple times. We use a loop (For and while 
statement) when we want to repeat the block, immediately. But, if we want to repeat the block of code 
at different times, then we use a subroutine. When we give a label to the block of code, it becomes a 
subroutine.	 Whenev er	 y ou	 w ant	 to	 execute	 this	 block	 of	 statements,	 y ou	 just	 use	 the gosub statement. 
This is called a call to the subroutine. 
Jy oti:	Why	should	w e	use	subroutines?
Moz: Subroutines help you to understand a program easily. Look at the following example. Explain 
what the program does?
Jy oti:	 This	 is	 a	 simple	 calculator .	 Depending	 on	 the	 choice	 of	 the	 operation	 the	 program	 just	 calls	 the	
subroutine to do the calculation.
Jy oti:	Can	w e	call	the	subroutine	any	number	of	times.	
Moz: Yes. You can. 
T ejas:	Understanding	someone	else’ s	program	will	be	easy	if	there	are	subroutines.
Moz: Additionally there should also be comments.      Comments
Jy oti:	 When	 w e	 w ork	 in	 teams,	 w e	 can	 divide	 a	 program	 into	 subroutines	 for	 each	 task,	 and	 subtasks.	
Each one of us can write a part of the code and also independently test it. Then we can combine the 
subroutines into the main program.
Moz: Right. 
1
Algorithm for the calculator
Ask	 	 the	 user	 	 for	 the	 opera tion	 to	 be	 performed.	 (Addition,	 subtraction,	 multiplication,	 1.	
division).
Ask	the	user	to	enter	two	numbers	num1,	num2	for	the	operation. 2.	
Use	a	condition	block	to	identify	the	operation	selected	by	the	user. 3.	
Write	statements	for	performing	each	operation.	 4.	
Print	the	result. 5.	
Comment statements (Rem and #) help in understanding the flow of the program and debugging the program. • 
Recall
Algorithm
PROGRAM
#rem A simple calculator
menu:
print “Enter your choice of mathematical operation” 
print “1- Additon” ;
print “2- Subtraction” ;
print “3- Multiplication” ;
print “4- Division” 
input “>” , choice 
input “Enter number1>” , num1
input “Enter number2>” , num2 
if choice=1 then gosub addition
if choice=2 then gosub subtract
if choice=3 then gosub mult
if choice=4 then gosub divison
end
addition:
result = num1 + num2
Print num1 +”+”+ num2+ “=”+ result
return
# rem similar subroutines are written for subtract, mult, division
A	Subroutine	is	used	to	repeat	a	block	of	code	at	different	places	in	a	program. •	
When	 a	 subroutine	 is	 called,	 the	 program	 execution	 which	 is	 also	 called	 the	 control	 •	
flow	 of	 the	 program,	 moves	 into	 the	 subroutine.	 After	 the	 subroutine	 is	 executed,	 the	
control	 flow	 of	 the	 program 	 comes	 back	 and	 continu es	 from	 the	 next	 statement	 after	 the	 
subroutine	call.
Advantages	of	using	a	subroutine: •	
Multiple	calls	to	a	block	of	code	are	possible,	at	different	times	in	the	program. –
They	 reduce	 the	 chances	 of	 typing	 errors	 when	 the	 same	 code	 has	 to	 be	 used	 in	 –
multiple	places	in	the	program.
A	program	is	easy	to	read,	since	the	main	steps	can	be	easily	followed.	 –
Programs	 are	 easier	 to	 design,	 since	 the	 main	 steps	 are	 written.	 The	 subroutines	 –
contain	the	details	of	these	steps.						Stepwise thinking
When	 more	 than	 one	 programmer	 works	 on	 a	 program,	 each	 programmer	 can	 choose	 –
a	 subroutine	 to	 write	 inde pendently	 and	 also	 test	 it.	 Then	 the	 subroutines	 can	 be	
called	in	the	desired	sequence.
Subroutines
Concept Info
Subroutines
Syntax (to define a subroutine)
label:	
   statement(s)
return
Syntax (to call a subroutine)
gosub label
A subroutine has a label followed 
by a sequence of statements and 
ends with a return statement.	You	
may have multiple subroutines in a 
single	program.		
When	a		call	is	made	to	the	 •	
subroutine	using	the	gosub	
statement	the	program	
control is transferred to the 
subroutine.
The	gosub	statement	causes	 •	
the	execution	to	jump	to	the	
subroutine defined by the 
label.
return statement of the •	
subroutine sends control back 
to where the subroutine was 
called	from.	
Next,	execution	of	the	 •	
statements	 following	 the	 gosub	
statement	takes	place.
 
Apply stepwise thinking for planning a program. The main steps can be listed and the details of some of these steps can be • 
 included in the subroutines.
Recall
#subroutione for addition
addition:
result = num1 + num2
Print num1 +”+”+ num2+ “=”+ result
return PROGRAM
PROGRAM
#rem A simple calculator
menu:
print “Enter your choice of mathematical operation” 
print “1- Additon” ;
print “2- Subtraction” ;
print “3- Multiplication” ;
print “4- Division” 
input “>” , choice 
input “Enter number1>” , num1
input “Enter number2>” , num2 
if choice=1 then gosub addition
if choice=2 then gosub subtract
if choice=3 then gosub mult
if choice=4 then gosub divison
end
#subroutione for addition
addition:
result = num1 + num2
Print num1 +”+”+ num2+ “=”+ result
return
#subroutione for subtraction
subtract:
result = num1 + num2
Print num1 +”-”+ num2+ “=”+ result
return
#subroutione for multiplication
mult:
result = num1 + num2
Print num1 +”*”+ num2+ “=”+ result
return
#subroutione for division
division:
result = num1 + num2
Print num1 +”/”+ num2+ “=”+ result
return
1
Page 5


Moz: What are Fibonacci numbers and how do you get the sequence of Fibonacci numbers?
Jy oti:	In	mathematics,	the	Fibonacci	numbers	are	the	numbers	in	the	following	integer	sequence:
0, 1, 1, 2, 3, 5, 8, 13, 21....... and so on.
T ejas:	 The	 first	 tw o	 numbers	 in	 the	 Fibonacci	
sequence are 0 and 1. Each subsequent number is 
the sum of the previous two numbers. For example, 
the next number in the sequence shown above is  
13	+	21	=	34.
Jy oti:	 Let	 us	 write	 a	 program	 to	 generate	 the	 Fibonacci	
sequence of numbers, upto 1000.
T ejas:	 Let	 us	 first	 write	 the	 algorithm	 for	 generating	 the	
sequence, by trying out an example.
T ejas:	 In	 our	 Science	 class	 w e	 discov ered	
that the number of petals on flowers is 
mostly one of the numbers in a sequence 
called the Fibonacci sequence. 
13 petals 5 petals
Aim: In this lesson, you will learn:
- Repetition of statements with condition.
- Defining	and	executing	subroutines.
- String functions.
BASIC
(We	need	three	variables) 1.	
						Let	a	=	previous	number,	b	=	current	number,	c	=	next	sequence	number.
Ask	the	user	to	input	a	number	upto	which	fibonacci	numbers	should	be	generated. 2.	
Print	the	first	two	fibonacci	numbers. 3.	
As	long	as	c	<	1000	repeat	the	following. 4.	
						 (Calculate	next	fibonacci	number	which	is	the	sum	of	previous	numbers	and	current		 
												numbers.)	
	 Next	fibonacci	sequence	number	c	=	a+b.
	 If		c	<	1000		print	the	fibonacci	numbers.
	 (The	 previous	 number	 and	 the	 current	 number	 shift,	 that	 is,	 they	 take	 the	 next	 values	 in	 	 	 	 
												the	sequence.)
	 previous	number	a	=	b.
									 Current	number	b	=	c. 
Examples
1 + 1 = 2
       1 + 2 = 3
    2 + 3 = 5
                     3 + 5 = 8
                            5 + 8 = 13
1, 1, 2, 3, 5, 8, 13, 21, ....
Sequence Generation
8
Algorithm
Memory
0, 1, 1, 2, 3, 
5, 8, 13, 21, 
34, ...
while b < num
T ejas:	 The	 repeat	 statement	 “For ”	 	 	 	 	 	 	 	For statement will not work here as we do not know the number 
of times that we need to repeat a set of statements. Let us see if we have other repeat statements that 
we can use.
Jy oti:	W e	can	use	“While”	statement.	
Moz: Such repetitive statements such as for and while are called Loops.
Jy oti:	 W e	 can	 also	 conv ert	 text	 to	 speech.	 In	 the	 abov e	 program	 before	 the	 “input”	 statement	 w e	 can	
put the following statement and the computer reads out the text. 
T ejas:	W ow!	This	is	cool!	
1
Info
Text	to	Speech	
Syntax
say expression 
Say	“Hello	how	are	you?”
The say statement is used to 
make	BASIC-256	read	an	ex-
pression	aloud,	through	the	com-
puter's	speakers.	
																			Hello	How	are	you
PROGRAM
Jy oti:	Now	I	w ant	to	build	a	program	for	a	simple	calculator .
T ejas:	Let	us	write	the	algorithm	for	the	calculator .
For statement executes a specified block of code a specified number of times, and keeps track of the value of the variable. • 
Recall
Enter a number: 1000
Fibonacci sequence upto 1000
1, 1, 2, 3, 5, 8, 13, 21, 
34, 55, 89, 144, 233, 
377, 610, 987
PROGRAM
OUTPUT
Info
Loop:	Repeat	-	Do	something	while	a	condition	remains	true
Syntax 
while condition 
    statement(s) 
end while
Do the statements in the block •	
over	and	over	again	while the 
condition	is	true.	
The statements will be •	
executed	zero	or	more	times.	
i=8
while i<10
Print “Hello World” 
i=i+1
end while 
PROGRAM
Hello World
Hello World
OUTPUT
}
Prints twice because 
the loop is executed 
twice and then value 
of i becomes 10.
Moz: You can use a subroutine for each of the operations. 
T ejas:	What	is	a	subroutine?
Moz: Sometimes we want to repeat a block of code multiple times. We use a loop (For and while 
statement) when we want to repeat the block, immediately. But, if we want to repeat the block of code 
at different times, then we use a subroutine. When we give a label to the block of code, it becomes a 
subroutine.	 Whenev er	 y ou	 w ant	 to	 execute	 this	 block	 of	 statements,	 y ou	 just	 use	 the gosub statement. 
This is called a call to the subroutine. 
Jy oti:	Why	should	w e	use	subroutines?
Moz: Subroutines help you to understand a program easily. Look at the following example. Explain 
what the program does?
Jy oti:	 This	 is	 a	 simple	 calculator .	 Depending	 on	 the	 choice	 of	 the	 operation	 the	 program	 just	 calls	 the	
subroutine to do the calculation.
Jy oti:	Can	w e	call	the	subroutine	any	number	of	times.	
Moz: Yes. You can. 
T ejas:	Understanding	someone	else’ s	program	will	be	easy	if	there	are	subroutines.
Moz: Additionally there should also be comments.      Comments
Jy oti:	 When	 w e	 w ork	 in	 teams,	 w e	 can	 divide	 a	 program	 into	 subroutines	 for	 each	 task,	 and	 subtasks.	
Each one of us can write a part of the code and also independently test it. Then we can combine the 
subroutines into the main program.
Moz: Right. 
1
Algorithm for the calculator
Ask	 	 the	 user	 	 for	 the	 opera tion	 to	 be	 performed.	 (Addition,	 subtraction,	 multiplication,	 1.	
division).
Ask	the	user	to	enter	two	numbers	num1,	num2	for	the	operation. 2.	
Use	a	condition	block	to	identify	the	operation	selected	by	the	user. 3.	
Write	statements	for	performing	each	operation.	 4.	
Print	the	result. 5.	
Comment statements (Rem and #) help in understanding the flow of the program and debugging the program. • 
Recall
Algorithm
PROGRAM
#rem A simple calculator
menu:
print “Enter your choice of mathematical operation” 
print “1- Additon” ;
print “2- Subtraction” ;
print “3- Multiplication” ;
print “4- Division” 
input “>” , choice 
input “Enter number1>” , num1
input “Enter number2>” , num2 
if choice=1 then gosub addition
if choice=2 then gosub subtract
if choice=3 then gosub mult
if choice=4 then gosub divison
end
addition:
result = num1 + num2
Print num1 +”+”+ num2+ “=”+ result
return
# rem similar subroutines are written for subtract, mult, division
A	Subroutine	is	used	to	repeat	a	block	of	code	at	different	places	in	a	program. •	
When	 a	 subroutine	 is	 called,	 the	 program	 execution	 which	 is	 also	 called	 the	 control	 •	
flow	 of	 the	 program,	 moves	 into	 the	 subroutine.	 After	 the	 subroutine	 is	 executed,	 the	
control	 flow	 of	 the	 program 	 comes	 back	 and	 continu es	 from	 the	 next	 statement	 after	 the	 
subroutine	call.
Advantages	of	using	a	subroutine: •	
Multiple	calls	to	a	block	of	code	are	possible,	at	different	times	in	the	program. –
They	 reduce	 the	 chances	 of	 typing	 errors	 when	 the	 same	 code	 has	 to	 be	 used	 in	 –
multiple	places	in	the	program.
A	program	is	easy	to	read,	since	the	main	steps	can	be	easily	followed.	 –
Programs	 are	 easier	 to	 design,	 since	 the	 main	 steps	 are	 written.	 The	 subroutines	 –
contain	the	details	of	these	steps.						Stepwise thinking
When	 more	 than	 one	 programmer	 works	 on	 a	 program,	 each	 programmer	 can	 choose	 –
a	 subroutine	 to	 write	 inde pendently	 and	 also	 test	 it.	 Then	 the	 subroutines	 can	 be	
called	in	the	desired	sequence.
Subroutines
Concept Info
Subroutines
Syntax (to define a subroutine)
label:	
   statement(s)
return
Syntax (to call a subroutine)
gosub label
A subroutine has a label followed 
by a sequence of statements and 
ends with a return statement.	You	
may have multiple subroutines in a 
single	program.		
When	a		call	is	made	to	the	 •	
subroutine	using	the	gosub	
statement	the	program	
control is transferred to the 
subroutine.
The	gosub	statement	causes	 •	
the	execution	to	jump	to	the	
subroutine defined by the 
label.
return statement of the •	
subroutine sends control back 
to where the subroutine was 
called	from.	
Next,	execution	of	the	 •	
statements	 following	 the	 gosub	
statement	takes	place.
 
Apply stepwise thinking for planning a program. The main steps can be listed and the details of some of these steps can be • 
 included in the subroutines.
Recall
#subroutione for addition
addition:
result = num1 + num2
Print num1 +”+”+ num2+ “=”+ result
return PROGRAM
PROGRAM
#rem A simple calculator
menu:
print “Enter your choice of mathematical operation” 
print “1- Additon” ;
print “2- Subtraction” ;
print “3- Multiplication” ;
print “4- Division” 
input “>” , choice 
input “Enter number1>” , num1
input “Enter number2>” , num2 
if choice=1 then gosub addition
if choice=2 then gosub subtract
if choice=3 then gosub mult
if choice=4 then gosub divison
end
#subroutione for addition
addition:
result = num1 + num2
Print num1 +”+”+ num2+ “=”+ result
return
#subroutione for subtraction
subtract:
result = num1 + num2
Print num1 +”-”+ num2+ “=”+ result
return
#subroutione for multiplication
mult:
result = num1 + num2
Print num1 +”*”+ num2+ “=”+ result
return
#subroutione for division
division:
result = num1 + num2
Print num1 +”/”+ num2+ “=”+ result
return
1
T ejas:	Let	us	now	write	a	program	to	encrypt	and	decrypt	messages.
Moz: So you want to write a cryptographic package. What is your plan?
Jy oti:	 W e	 can	 use	 the	 string	 functions	 that	 w e	 explored.	 For	 example,	 giv en	 the,	 string	 ‘Hello’,	 our	
program	will	return	the	encrypted	string	as	‘olleH’.		
T ejas:	Let	us	first	try	out	a	simple	encryption	algorithm	by	rev ersing	a	string.
Moz:	Good	idea.	
# An encryption and decryption program
Cls
Say “Enter a string ”
input “Enter a string >> ” , word$
I = length(words$)
Print “Encrypted string is >> ” ;
For n = I to 1 step -1
Print Mid(words$,n,1);
Next n
End
PROGRAM
OUTPUT
Enter a string >> Hello
Encrypted string is >> olleH
Jy oti:	Let	us	use	a	subroutine	to	generate	a	graph	using	the	fibonacci	series.
PROGRAM
OUTPUT
# Draw patterns using fibonacci
clg 
cls
a=0
b=1
color yellow
rect 0, 0, 300, 300
while b < 1000
    n =a+b
    print n
    n =a+b
    a=b
    b=n
gosub fibonacci_draw
endwhile
fibonacci_draw:
for i=a to b
color red
line a,b,i,i
next i
return
Graphic output
Text output
8
13
21
34
55
89
144
233
377
610
987
1597
Read More
10 videos|9 docs

Top Courses for Class 8

10 videos|9 docs
Download as PDF
Explore Courses for Class 8 exam

Top Courses for Class 8

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Viva Questions

,

Textbook: Basic | Computer Science for Class 8

,

Semester Notes

,

Textbook: Basic | Computer Science for Class 8

,

Previous Year Questions with Solutions

,

Summary

,

Sample Paper

,

Important questions

,

ppt

,

Extra Questions

,

Textbook: Basic | Computer Science for Class 8

,

past year papers

,

mock tests for examination

,

pdf

,

study material

,

Exam

,

video lectures

,

Objective type Questions

,

MCQs

,

practice quizzes

,

shortcuts and tricks

,

Free

;