The Basics of Programming

For a program to be really useful, it has to be able to take input from a user and output the results. Some input can be from a keyboard and output sent to the screen. In simple programs, you would use the shell or command prompt to take input and the output. These are known as the standard input and outputs.

A) Input

When in putting data, you need to scan the keyboard for characters and store them in a buffer or a variable. In BASIC the command is INPUT ["prompt string";] variable. The Input command will display the optional prompt string and will prompt for some input and put the contents into the variable

Example input code:

INPUT "Enter your name? "; name$

INPUT "Enter a number (1 to 100)? "; number

If using a graphical front end, then the input will be via Text Boxes, Buttons and Dialog boxes where you can make enter text, numbers and make selections, or answer questions by the program. On Amiga BASIC, this is provided using commands such as WINDOW, GADGET etc.

For example, a string input gadget can be created on a previously opened window:
GADGET 1,1,"",(50,20)-(150,30),2 ' Create a string gadget (id is 1)

GADGET OUTPUT 1 ' Set current gadget to id 1
value$ = GADGET(2) ' Return the value from that gadget

Modern computers also have a mouse so your program can make use of the mouse's position (its X, Y) position and buttons (Left or Right) to perform input.

b) Output

When outputting data or information, you would normally use the shell or command prompt window to display it. In BASIC, the command to output is PRINT expression-list. The expression list can be any combination of strings, numbers and variables. You can seperate the data with either a semicolon (;) which will print data next to last one, or a comma (,) which will tabulate to the next comma stop (set by the WIDTH statement).

e.g.

PRINT "Please enter your name:" ' Display the text 'Please enter your name:'
PRINT "Result is "; result ' Display the text 'Result is ' and follow it with a decimal number from the variable called result
PRINT "Your name is ";name;" and your age is "; age ' Display the text 'Your name is ' and the contents of variable name followed by more text ' and your age is ', and then the contents of decimal variable age */



PRINT A, B, C, TOTAL

You can also specify where in a window you want to print to using the LOCATE statement. The format is LOCATE row, column.

LOCATE 20,1
PRINT "Score "; sc

If you want your information formated somehow, then you can use the SPC and TAB functions. SPC(n) will print 'n' spaces, while TAB(n) will tabulate your data into neat columns, 'n' specifies the next tab column.
E.g.

PRINT A; TAB(10); B; TAB(20); C; TAB(30); TOTAL

A more advanced printing can be achieved with PRINT USING format-string; string-expression. The format-string will determine the exact width, type of characters and placement using hashes (#) for digits, dots (.) for decimal places, plus and minus (+/-) for number sign, dollar ($) for currency, astericks (*) for leading spaces, dash (-) for literal characters and hat (^) for exponential positions. E.g.

PRINT USING "+###.##"; 200.45, 17.6, 197.04, -26.0

If using a graphical front end, then the output will be via Labels or Text Boxes, where you can display text or numbers. On Amiga BASIC, this is provided using commands such as WINDOW, GADGET etc.

For example to display some string text in a gadget:
GADGET 4,1,"Please enter your name here",(50,20)-(150,30),2,1 ' Create a string gadget (id is 4)

Also, instead of just text or numbers or list of numbers you can also output results on forms, and use charts and graphs using the drawing tools provided by BASIC using commands such as LINE, CIRCLE, AREA and AREAFILL.

Next Page