Tutorial: Learn basics of programming with BASIC

These are some of the simple programs I used on my commodore 64 or 286/386 when I was in elementary school. I think that it is a good start for learning programming and move on to something more serious later on. It has some useful programs though, mathematical to calculate all numbers from 1 to 1000, to calculate the price of assembling a PC, and some video games.

There are two preferable ways for running the code:

  1. Visit this page and enter some programming code: http://www.calormen.com/applesoft/
  2. Install Basic-256 program, its available for Windows and Linux, and it is in Ubuntu repository, so you can install it with: sudo apt-get install basic256

The code below is from gwbasic, the differences I have found so far with: 1) is that semicolons should be made into colons, 2) leave out 10, 20, etc. You don’t have to use “end” in either of them. If some command does not work, just erase that line or find a workaround.

Math programs

10 input x
20 if x<1 then 50
30 a=-x
40 print a
50 end

10 input a
20 input b
30 if b=0 then print “dividing by zero”; goto 60
40 R=a/b
50 print “The Result is”;R
60 end

10 input “the numbers are” x,y
20 if x>y then q=x/y goto 40
30 q=y/x
40 print “The result is: “;q
50 end

Textual programs

10 CLS
20 print tab(4); “I”
30 print
40 print “like”
50 print tab(4);”to”
60 print “eat”
70 print tab(4); “apples”
80 print
100 end

10 a$=”expirienced”
20 b$=”un”+a$
30 print b$
40 end

10 input a$
20 b$=”un”+a$
30 print b$
40 goto 10

10 input a$,b$
20 c$=a$+b$
30 print c$
40 goto 10

if…then

10 input x,y
20 if x>y then 50
30 print y
40 goto 60
50 print x
60 end

10 input x,y
20 if x>y then 50
30 print y
40 stop
50 print x
60 end

10 input x,y
20 if x>y then print x
30 if x<y then print y
40 end

10 input x,y
20 if x>y then print x: stop
30 print y
40 end

10 input “input numbers”; x,y,z
20 if x<y then 60
30 max=y
40 if max<z then 80
50 goto 90
60 max=y
70 goto 40
80 max=z
90 print “maximum is “;max
100 end

For, Step, Next

10 for i=0 to 10 step 2
20 a=i^2-4
30 if a=0 then print “function is not defined for x=”;i;goto 50
35 fx=(5*i+8)/a
40 print i,fx
50 next i
60 end

10 input n
20 s=0
30 for i=1 to n
40 s=s+i
50 next i
60 print s
70 end

10 s=0
20 for i=1 to 99 step 2
30 print i
40 s=s+i
50 next i
60 print s
70 end

10 cls
20 s=0
30 for i=1 to 1000
40 s=s+1
50 print “the sum of numbers from 1 to 1000 is “;s
60 end

Len and Mid$

10 input a$
20 n=len(a$)
30 print “the length of a word “;a$;” is “;n
40 goto 10

10 mid$(“Belgrade”,4,3)
20 end

10 input a$
20 n=len(a$)
30 x$=mid$(a$,1,3)
40 y$=mid$(a$,4,n-3)
50 print “rec”;a$;x$;y$
60 goto 10

Int, Rnd, Sgn, Abs

10 cls
20 input x
30 y=int(x)
40 print y
50 end

10 print int(rnd*10)
20 goto 10

10 for i=1 to 16
20 n=int(1+6*rnd)
30 print n
40 next i
50 end

10 a=0:b=0
20 cls
30 for i=1 to 20
40 a=a+100*int(1+rnd*3)
50 if a>2000 then print “car no.1 wins”:stop
55 b=b+100*int(1+rnd+3)
60 if b>2000 then print “car no.2 wins”:stop
70 print “after “;i; “time interval car no.1 has gone “;a; “meters, car no.2 “;b;”meters”
80 next i
90 end

10 a1=abs(0):a2=abs(2,35):a3=abs(-4.75)
20 b1=sgn(0):b2=sgn(-2,35):b3=int(3,75)
30 print a1,a2,a3
40 print b1,b2,b3
50 end

Database

10 for i=1 to 5
20 read d$,n
30 print d$;tab(12);n
40 next i
50 data monday,6, tuesday,7, wednesday,7, thursday,6, friday,5
60 end

10 cls
20 print “computer price”
30 print
40 v=0
60 for i=1 to 5
70 read r$,c
80 print tab(5);r$
90 input k
100 v=u+k*c
110 next i
120 print
130 print “total price”
140 data “motherboard”,80
150 data “cpu”,90
160 data “hdd”,50
170 data “optics”,40
180 data “case”,30
190 end

Complex programs

10 gosub 10000
20 baza$=chr$(162)+chr$(219)+chr$(162)
30 lopta$=chr$(209)
50 prom=0:pgo=0
100 gosub 11000
105 k2=1
110 print chr$(19);kol$;lopta$
120 print redx$;baza$
130 pali=0
100 gosub 12000
210 print redx$;
220 poke 211,k2-1
230 print ” ”
240 poke 211,k2
250 print baza$
260 if pali=1 then gosub 13000:goto 300
270 goto 200
300 print “press key to shoot”
310 wait 56465,16,16
320 print chr$(147)
330 goto 100
10000 print chr$(147)
10010 redx$=chr$(19)
10020 for i=1 to 22
10030 redx$=redx$+chr$(17)
10040 next i
10050 return
11000 k1=int(rnd(1)*33)+2
10010 kol$=” ”
10020 for n=1 to k1
10030 kol$=kol$+chr$(29)
10040 next n
10050 return
12000 pom=peek(56465) and 31
12010 pom=31-pom
12020 if pom=4 then k2=k2-1
12030 if pom=8 then k2=k2+1
12040 if k2<1 then k2=1
12050 if k2>35 tjem k2=35
12060 if pom=16 or pom=20 or pom=24 then pali=1
12070 return
13000 r1$=redx$+chr$(145)
13010 for n=1 to 21
13020 print r1$
13030 poke 211,k2
13040 print ” ”
13050 r1$=r1$+chr$(145)
13060 print r1$
13070 poke 211,k2+1
13080 print “|”
13090 next n
13100 if k1=k2+1 then pgd=pgd+1:goto 13120
13110 prom=prom+1
13120 print redx$+chr$(17)
13130 print “score “;pgd; ” missed “;prom
13140 return

10 rem **Abecedni spisak ucenika**
20 dim i$(30)
30 print “unesite imena ucenika”
40 n=0
50 input i$
60 if i$=”kraj” then 80
70 n=n+1:i$(n)=i$:goto 50
90 gosub 300
110 for k=1 to n
120 print i$(k)
130 next k
140 print
150 return
300 rem **podprogram za uredjenje spiska**
302 print “neuredjen spisak”
304 gosub 100
310 for i=1 to n-1
312 print “prolaz”;i
320 for j=i+1 to n
330 if i$(j)=>i$(i) then 365
340 p$=i$(j)
350 i$(j)=i$(i)
360 i$(j)=p$
365 gosub 100
370 next j
380 next i
382 print “uredjen spisak”
384 gosub 100
390 return
999 end

10 cls
20 screen 2
28 pset (0,0)
25 line – (639,0)
30 line – (639,199)
40 line – (0,199)
50 line – (0,0)
60 circle (320,100),100
70 do while in key$=” ”
75 loop
80 end

10 cls
20 screen 2
23 pset (0,0)
25 line – (639,0)
30 line – (639,199)
40 line – (0,199)
50 line – (0,0)
60 circle (320,100),100
62 paint (320,100)
70 do while in key$=” ”
75 loop
80 end
100 rem aida
130 sound 294,4
150 for i=1 to 26
160 read f,t
170 sound f,t
180 next i
200 sound 392,3
210 sound 440,1
220 sound 440,12
230 sound 294,4
250 restore
260 for i=1 to 26
270 read f,t
280 sound f,t
290 next i
310 sound 440,1.5
320 sound 494,1.5
330 sound 440,1.5
340 sound 392,12
360 data 392,12 440,1.5, 294,1.5, 440,1.5
370 data 494,4, 494,4, 494,4, 494,1.5, 523,1.5
380 data 392,1.5, 494,6, 440,2, 392,4, 32000,4
390 data 440,3, 494,1, 494,3, 440,1, 392,2
400 data 32000,2, 440,3, 494,1, 494,4, 440,3
410 data 494,1,494,4
430 end

10 for f=440 to 1000 step 5
20 sound f,f/1000
30 next f
40 for f=1000 to 440 step 5
50 sound f,f/1000
60 next f
100 print “{clr}”
120 a$=”g”
130 b$=”grrrrrrrrr”
140 print b$
150 a$=a$+”r”
160 print a$:print:print “{c/up}{c/up}”
170 for n=1 to 10:next n
180 get c$:if c$=” ” goto 150
190 print
200 if a$<b$ then print “prekratko”:end
210 if a$>b$ then print “predugo”:end
220 print “bravo”

10 input x
20 on x goto 40,50,60
30 goto 10
40 print “taster 1”:end
50 print “taster 2”:end
60 print “taster 3”:end

10 dim a$(2,2)
20 for n=0 to 2:for m=0 to 2
30 read a$(n,m)
40 print tab(4*m) a$(n,m)
50 next:print:next
60 data “a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”

Leave a Reply