This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
basic [2020/04/16 16:46] smj [BASIC Programming Examples] |
basic [2020/04/18 01:38] smj [BASIC Programming Examples] |
||
---|---|---|---|
Line 18: | Line 18: | ||
====== BASIC Programming Examples ====== | ====== BASIC Programming Examples ====== | ||
- | There are many statements available to you in BASIC, but here are a few that can get you started and will work across many BASIC interpreters. | + | There are many statements available to you in BASIC, but here are a few that can get you started and will work across many BASIC interpreters. |
- | UNIX System V R.3 (WINS) (lcm3b2) | + | |
+ | |||
+ | | ||
+ | | ||
+ | UNIX System V Release 3.2.2 AT&T 3B2 | ||
+ | | ||
+ | | ||
+ | All Rights Reserved | ||
+ | |||
+ | $ basic | ||
+ | * REM *** BASIC will evaluate 2+2 to give us 4 | ||
+ | | ||
+ | 4 | ||
+ | |||
+ | * REM *** The variable ' | ||
+ | | ||
+ | 0 | ||
+ | |||
+ | * REM *** LET assigns the value of ' | ||
+ | *let a=1 | ||
+ | | ||
+ | 1 | ||
+ | |||
+ | * REM *** STRINGS are quoted in BASIC and to use a variable as a string, just append ' | ||
+ | | ||
+ | Hello World! | ||
+ | * let a$ = "Hello World!" | ||
+ | * print a$ | ||
+ | Hello World! | ||
+ | |||
+ | * REM *** Now let's write it as a program with line numbers | ||
+ | *10 print "Hello World!" | ||
+ | *20 end | ||
+ | |||
+ | * REM *** The LIST command will show us what we typed in | ||
+ | | ||
+ | 10 print "Hello World!" | ||
+ | 20 end | ||
+ | |||
+ | * REM *** The RUN command will run our program | ||
+ | | ||
+ | Hello World! | ||
+ | |||
+ | * REM *** The NEW command will clear the memory buffer and LIST will show us nothing is there | ||
+ | | ||
+ | | ||
+ | * | ||
+ | |||
+ | * REM *** Lets write a programming using INPUT to assign a VARIABLE N | ||
+ | *10 input "What is your name?"; | ||
+ | *20 print "Hello "; n$ | ||
+ | *30 end | ||
+ | | ||
+ | 10 input "What is your name?"; | ||
+ | 20 print "Hello "; n$ | ||
+ | 30 end | ||
+ | | ||
+ | What is your name?? COMPUTER | ||
+ | Hello COMPUTER | ||
+ | |||
+ | * REM *** Here is an example of a FOR loop. The loop is closed with the NEXT statement. | ||
+ | * REM *** NEXT I tells BASIC that the variable I is free to be used again. | ||
+ | | ||
+ | *10 for i = 0 to 20 | ||
+ | *20 print i | ||
+ | *30 next i | ||
+ | *40 end | ||
+ | | ||
+ | 0 | ||
+ | 1 | ||
+ | 2 | ||
+ | 3 | ||
+ | 4 | ||
+ | 5 | ||
+ | 6 | ||
+ | 7 | ||
+ | 8 | ||
+ | 9 | ||
+ | 10 | ||
+ | 11 | ||
+ | 12 | ||
+ | 13 | ||
+ | 14 | ||
+ | 15 | ||
+ | 16 | ||
+ | 17 | ||
+ | 18 | ||
+ | 19 | ||
+ | 20 | ||
+ | |||
+ | * REM *** The STEP statement is implied in the FOR loop as "STEP 1" by default | ||
+ | * REM *** But you can specify a STEP number - here we STEP by 2 | ||
+ | * REM *** To replace line 10, just type it in as if it were a new line | ||
+ | *10 for i = 0 to 20 step 2 | ||
+ | | ||
+ | 10 for i = 0 to 20 step 2 | ||
+ | 20 print i | ||
+ | 30 next i | ||
+ | 40 end | ||
+ | | ||
+ | 0 | ||
+ | 2 | ||
+ | 4 | ||
+ | 6 | ||
+ | 8 | ||
+ | 10 | ||
+ | 12 | ||
+ | 14 | ||
+ | 16 | ||
+ | 18 | ||
+ | 20 | ||
+ | |||
+ | * REM *** Likewise we can run the FOR loop backwards by using a "STEP -1" | ||
+ | *10 for i = 20 to 0 step -1 | ||
+ | | ||
+ | 10 for i = 20 to 0 step -1 | ||
+ | 20 print i | ||
+ | 30 next i | ||
+ | 40 end | ||
+ | | ||
+ | 20 | ||
+ | 19 | ||
+ | 18 | ||
+ | 17 | ||
+ | 16 | ||
+ | 15 | ||
+ | 14 | ||
+ | 13 | ||
+ | 12 | ||
+ | 11 | ||
+ | 10 | ||
+ | 9 | ||
+ | 8 | ||
+ | 7 | ||
+ | 6 | ||
+ | 5 | ||
+ | 4 | ||
+ | 3 | ||
+ | 2 | ||
+ | 1 | ||
+ | 0 | ||
+ | |||
+ | * REM *** Here is an advanced example which will introduce nested FOR loops | ||
+ | * REM *** and the function INT (integer) and SIN (sine) to calculate a sine wave | ||
- | login: lcm | + | *list |
- | Password: | + | 10 let w = 2 |
- | UNIX System V Release 3.2.2 AT&T 3B2 | + | 15 let h = 20 |
- | lcm3b2 | + | 20 let t = 6.28318/ |
- | Copyright | + | 25 let s = 35 |
- | All Rights Reserved | + | 30 for i = 1 to w |
+ | 35 for j = 0 to 6.38318 - t step t | ||
+ | 40 let a = int(sin(j) * s + 0.5) | ||
+ | 45 for k = 1 to s + a | ||
+ | 50 print " "; | ||
+ | 55 next k | ||
+ | 60 print " | ||
+ | 65 next j | ||
+ | 70 next i | ||
+ | | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | + | ||
+ | |||
+ | * REM *** To return back to UNIX, type the command " | ||
+ | | ||
+ | $ | ||
- | $ basic | + | ====== |
- | *print 2+2 | + | |
- | 4 | + | |
- | *print a | + | |
- | 0 | + | |
- | *let a=1 | + | |
- | *print a | + | |
- | 1 | + | |
- | *10 print "Hello World!" | + | |
- | *20 end | + | |
- | *list | + | |
- | 10 print "Hello World!" | + | |
- | 20 end | + | |
- | *run | + | |
- | Hello World! | + | |
- | *new | + | |
- | *10 input "What is your name?"; | + | |
- | *20 print "Hello "; n$ | + | |
- | *30 end | + | |
- | *list | + | |
- | 10 input "What is your name?"; | + | |
- | 20 print "Hello "; n$ | + | |
- | 30 end | + | |
- | *run | + | |
- | What is your name?? COMPUTER | + | |
- | Hello COMPUTER | + | |
- | *new | + | |
- | *10 for i = 0 to 20 | + | |
- | *20 print i | + | |
- | *30 next i | + | |
- | *40 end | + | |
- | *run | + | |
- | 0 | + | |
- | 1 | + | |
- | 2 | + | |
- | 3 | + | |
- | 4 | + | |
- | 5 | + | |
- | 6 | + | |
- | 7 | + | |
- | 8 | + | |
- | 9 | + | |
- | 10 | + | |
- | 11 | + | |
- | 12 | + | |
- | 13 | + | |
- | 14 | + | |
- | 15 | + | |
- | 16 | + | |
- | 17 | + | |
- | 18 | + | |
- | 19 | + | |
- | 20 | + | |
- | *list | + | |
- | 10 for i = 0 to 20 | + | |
- | 20 print i | + | |
- | 30 next i | + | |
- | 40 end | + | |
- | *10 for i = 0 to 20 step 2 | + | |
- | *list | + | |
- | 10 for i = 0 to 20 step 2 | + | |
- | 20 print i | + | |
- | 30 next i | + | |
- | 40 end | + | |
- | *run | + | |
- | 0 | + | |
- | 2 | + | |
- | 4 | + | |
- | 6 | + | |
- | 8 | + | |
- | 10 | + | |
- | 12 | + | |
- | 14 | + | |
- | 16 | + | |
- | 18 | + | |
- | 20 | + | |
- | *10 for i = 20 to 0 step -1 | + | |
- | *list | + | |
- | 10 for i = 20 to 0 step -1 | + | |
- | 20 print i | + | |
- | 30 next i | + | |
- | 40 end | + | |
- | *run | + | |
- | 20 | + | |
- | 19 | + | |
- | 18 | + | |
- | 17 | + | |
- | 16 | + | |
- | 15 | + | |
- | 14 | + | |
- | 13 | + | |
- | 12 | + | |
- | 11 | + | |
- | 10 | + | |
- | 9 | + | |
- | 8 | + | |
- | 7 | + | |
- | 6 | + | |
- | 5 | + | |
- | 4 | + | |
- | 3 | + | |
- | 2 | + | |
- | 1 | + | |
- | 0 | + | |
- | *system | + | |
- | $ | + | |
+ | Visit our repository for [[https:// | ||
- | ====== BASIC Reference Manuals ====== | ||
- | |||
- | * Control Data Corporation BASIC(([[http:// | ||