User Manual | ![]() |
Although this manual is not intended to teach BASIC programming, it helps to see a few examples of actual programs. The following programs are included with the standard picoBASIC Integer distribution, and each serves to illustrate one or more features of the language. For the sample runs, program output is in black, user input is in red, and comments are in green.
Simply prints the current time at the top left corner, then repeats until a Break. Illustrates the use of system functions.
10 CLS 20 PRINT TAB(0);TIME$; 30 GOTO 20Sample Run:
16:48:03 repeatedly updated
Inputs an integer from the user, then calculates and displays all the prime number factors for that integer, including powers of primes. Illustrates handling user input, conditional branching, looping, and handling fractional parts of numbers.
10 CLS: PRINT TAB(8); "Prime Factors" 20 PRINT 30 PRINT "Enter 0 to exit program" 40 INPUT "Number";A 50 IF A=0 THEN 230 60 REM The sign of a number is the first factor: 70 PRINT SGN(A) 80 A=ABS(A) 90 REM Test all integers for primes 100 REM integers A/2 thru A will have no new factors 110 FOR I=2 TO A/2 112 FOUND=0 115 PRINT TAB(0);I; 120 S=0 130 IF A MOD I > 0 THEN 170 140 A = A/I 150 S = S+1 160 GOTO 130 170 IF S=0 THEN 200 180 FOUND=1 190 IF S>1 THEN PRINT "^";S ELSE PRINT 200 NEXT 210 IF NOT FOUND THEN PRINT TAB(0);SPACE$(10); 215 PRINT 220 GOTO 40 230 ENDSample Run:
Prime Factors Enter 0 to exit program Number: ? 45 1 3^2 5
This is an instructional program intended to teach & test knowledge of the order of planets from the sun. The user is asked which planet is in a particular orbit, and the answer is then given. Illustrates string arrays, random numbers, string manipulation, and DATA statements.
10 'Planets Quiz 20 CLS: RAND 'seed random numbers 25 REM BUILD LIST OF PLANET NAMES 30 READ NP 35 DIM P$(NP+1) 40 FOR I=1 TO 9:READ P$(I): NEXT I 99 ' Ask a question 100 PLANET=RND(NP)+1 105 PRINT 110 PRINT "What is planet ";PLANET; 120 PRINT "from the sun" 125 ' Get an answer 130 INPUT A$ 140 A$=UCASE$(TRIM$(A$)) 150 IF A$=P$(PLANET) THEN PRINT " * Correct *": GOTO 200 160 PRINT "Sorry. The answer is ";P$(PLANET) 199 'ask if user wants another go 200 INPUT"Again";YN$ 210 IF LEFT$(UCASE$(TRIM$(YN$)),1)="Y" THEN 100 220 PRINT 230 PRINT "Did you know that sometimes Pluto's" 240 PRINT "orbit is closer than Neptune's ?" 250 END 300 ' Planet Names 310 DATA 9 '# planets 320 DATA MERCURY, VENUS, EARTH 330 DATA MARS,JUPITER,SATURN 340 DATA URANUS,NEPTUNE,PLUTOSample Run:
What planet is 9 from the sun ? mars Sorry. The answer is PLUTO Again? y What planet is 2 from the sun ? venus * Correct * Again?
This program generates a list of 10 integers to serve as a data set to sort. It then does a CHAIN to a bubble sort program to sort and print the set. Illustrates numeric arrays and the CHAINing programs together.
10 ' Build a set to sort 20 DIM D(11) 30 FOR I=1 TO 10 40 D(I)=RND(100) 50 PRINT D(I); 60 NEXT 200 CHAIN "bubblesort"Sample Run:
68 76 99 93 80 47 32 71 58 77 The raw data sorting... 32 47 58 68 71 76 77 80 93 99 The sorted data
This is the bubble sort program used by example 4. This is a very slow, but very simple algorithm.
If you load the SORT DATA program, then MERGE it with the BUBBLESORT program, you will have a single, combined program. Any lines existing in the current program before the merge will be replaced by lines with the same number in the second program. In this case, SORT DATA's line 200 gets replaced by BUBBLESORT's line 200, eliminating the CHAIN command, which is no longer necessary when both programs are merged together. Merging programs allows a library of utility programs to be 'plugged into' other programs, providing a measure of modularity.
200 'Bubble Sort 210 PRINT "sorting..." 220 N=10 'number of elements 230 FOR I=1 TO N-1 240 FOR J=I+1 TO N 250 IF D(I)<=D(J) THEN 290 260 T=D(I) 270 D(I)=D(J) 280 D(J)=T 290 NEXT J,I 299 'print results 300 FOR I=1 TO N: PRINT D(I);: NEXT
This program calculates the circumference of a circle of given radius, using the formula: C = 2πr. It illustrates fixed point arithmetic, more string functions, and subroutines.
To provide greater accuracy than simple integers allow, intermediate values are scaled up by factors of 10. The result is then scaled down again with a decimal point inserted. This technique allows a simulation of fixed point decimal arithmetic. The dynamic range of such calculations is extremely limited in Integer BASIC (in this case, a radius much bigger than 5 will give an overflow error). However, it can be effectively doubled by using the entire numeric range, -32767 to +32767.
10 'Circumference of a Circle 20 'scale PI up by 100 30 'to provide 2 decimal places 40 PI=314 50 INPUT"radius";R$ 60 GOSUB 210 'convert to int*10 70 C=2*PI*R 80 PRINT "circumference: "; 90 PRINT TRIM$(STR$(C/1000)); 100 PRINT ".";(C MOD 1000)/10 110 END 200 'convert real num to integer*10 210 R$=R$+"0" 220 DP=INSTR(1,R$,".") 230 IF DP>0 THEN R$=LEFT$(R$,DP-1)+MID$(R$,DP+1,1) 240 R=ABS(VAL(R$)) 250 RETURNSample Run:
radius? 1.3 circumference: 8.16
©2002 Picodoc, Inc. All rights reserved.