QBasic/Appendix
From Wikibooks, the open-content textbooks collection
[edit] Commands
[edit] ABS()
PRINT ABS(54.345) 'This will print the value exactly as it is (54.345)
PRINT ABS(-43) 'This will print the value as (43)
This returns an absolute value of an expression within the brackets, turning a negative to a positive (e.g. -4 to 4)
[edit] ACCESS
OPEN "file.txt" FOR APPEND ACCESS WRITE
This sets the access of a file that has been declared into the program. There are three settings that the programmer can set. These are:
READ - Sets up the file to be read only, no writing. WRITE - Writes only to the file. Cannot be read. READ WRITE - Sets the file to both of the settings above.
[edit] ASC()
PRINT ASC("t") 'Will print 116
This returns the ASCII number of the character within the brackets. If the programmer put in a string into brackets, only the first character of the string will be shown.
[edit] ATN()
ATN( 23 / 34 )
Part of the inbuilt trigonometry functions. This the Arc-Tangent, it converts the values to an angle.
[edit] BEEP
BEEP
Allows a "Beep" sound emit from the PC speaker.
[edit] CASE
SELECT CASE [variable] CASE [value]: [command] CASE ELSE: [command] END SELECT
Use this when using multiple values in your program and assigning them separate paths. This is an example of a program with no CASE commands that assigns different paths to values:
10 PRINT "1. Print 'path'" PRINT "2. Print 'hello" PRINT "3. Quit" INPUT "Enter a choice: "; a$ IF a$ = "1" THEN PRINT "path" GOTO 10 IF a$ = "2" THEN PRINT "hello" GOTO 10 IF a$ = "3" THEN END PRINT "That is not a valid choice" GOTO 10
This is what a program looks like with the CASE command:
PRINT "1. Print 'path'" PRINT "2. Print 'Hello'" PRINT "3. Quit" INPUT "Enter a choice: "; a$ SELECT CASE a$ CASE "1": PRINT "path" CASE "2": PRINT "Hello" CASE "3": END CASE ELSE: PRINT "That is not a valid choice" END SELECT
[edit] CHAIN
CHAIN [filename].[format]
This lets the program transfer variable values from one program to another. You can either open a QBasic file (.bas) or a compiled file (.exe). The COMMON statement has to be declared before any CHAIN command can be run in the program.
[edit] CHDIR
CHDIR [directory name]
This is used for changing a directory. Used in conjunction with MKDIR and RMDIR. The directory name is declared exactly like in DOS PROMPT. For example:
CHDIR "c:/Program Files/QBasic_FIles/Testing_Files"
To use this command, the programmer will have to know how MS DOS and COMMAND PROMPT work.
[edit] CHR$()
This returns the string value of an ASCII character.
CHR$([Character number])
This is used a lot for inputting keyboard keys. Some keyboard keys cannot be type (e.g. the Esc key or the F Buttons). So the programmer has to make up for this with the CHR$ command. Here is a list of some character numbers:
08: Backspace 09: Tab 27: Esc 72: Up Arrow 75: Left Arrow 77: Right Arrow 80: Down Arrow
The numbers are based on the ANSI codes. There are 225 different characters to choose from.
[edit] CINT()
This rounds the integer within the brackets to the nearest integer.
PRINT CINT(4573.73994596)
[edit] CIRCLE
CIRCLE ([X Coordinate], [Y Coordinate]), [Radius], [Colour]
Lets the programmer display a circle. Like all graphics commands, it must be used with the SCREEN command.
[edit] CLEAR
CLEAR
Resets all variables, strings, arrays and closes all files. The reset command on QBasic.
[edit] CLOSE
CLOSE #[number of file]
Closes a file that has been opened in a QBasic program.
[edit] CLS
CLS
Clears the active screen. Erases all text, graphics, resets the cursor to the upper left (1,1), and also applies the current background color (this has to be set using the COLOR command) to the whole screen.
[edit] COLOR
COLOR [Number of Colour]
This lets you change the colour of the text in the program. It can be done like this:
COLOR 14 PRINT "Yellow"
You have a choice of sixteen colours:
00: Black 08: Dark Grey 01: Dark Blue 09: Light Blue 02: Dark Green 10: Light Green 03: Dark Cyan 11: Light Cyan 04: Dark Red 12: Light Red 05: Dark Purple 13: Magenta 06: Orange Brown 14: Yellow 07: Grey 15: White
These values are the numbers that you put in the COLOR command.
[edit] COMMON
Declares variables as global.
COMMON SHARED [variablename]
The COMMON statement must be declared before any statements that are executable in the program. This is an example of a non-executable statement.
[edit] CONST
This declares a value as a constant. Which means that the value cannot be changed within the program.
CONST Name AS INTEGER CONST Name AS STRING
These are put at the beginning of a program.
[edit] DATA
DATA [constant]
Use in conjunction with the READ and RESTORE command. Mostly used in programs dealing with graphics, this command lets QBasic read a large number of constants. The READ command accesses the data while the RESTORE command "refreshes" the data, allowing it to be used again.
[edit] DATE$
This declares the date of the system. It is in the format of mm-dd-yyyy. Use it like this:
a$ = DATE$
[edit] DIM
This this is used to declare variables, either singular or an array of variables.
DIM [Array Name]$ ([Number Of values])
This is used for building arrays. Input the values using the LET command, like this:
DIM Name$ (3) LET Name$ (1) = "John" LET Name$ (2) = "Mary" LET Name$ (3) = "Peter"
For inputting single variables, the programmer inputs the data like this:
DIM Name AS STRING 'Declaring a string variable DIM Name AS INTEGER 'Declaring an integer variable DIM Name AS DOUBLE 'Declaring a variable as both a string and an integer
These values can be changed within the program.
[edit] DO
DO [program] LOOP UNTIL [condition]
Used to create a loop in the program. This command checks the condition after the loop has started. This is used with the LOOP command. This is a very simple example:
num$ = 1 sum$ = 0 DO sum$ = 2 * num$ PRINT sum$ num$ = num$ + 1 LOOP UNTIL num$ = 13
This program outputs the Two Times Tables up to 12. The Reason why we put 13 is that the program stops at that point and does not continue the loop. If we put LOOP UNTIL num$ = 12 the last output on the screen would be "22".
[edit] DRAW
DRAW [string expression]
This is used to draw lines. This will draw a line at the last pixel coordinate and colour stated in the program. It draws according to the directions, angles, and lengths stated in the string expression. Here is how to use the command:
SCREEN 7 PSET (50, 50), 4 DRAW "u50 r50 d50 l50"
This program will draw a red square at the coordinates of the pixel started. It will also colour it to the colour number. It is really a free movement shape command, unlike the LINE or CIRCLE command were you can only draw one style of object. You notice the letters at the front of each number, that is the direction the line should go. Here is the chart:
U = Up E = Upper-right D = Down F = Lower-right L = Left G = Lower-left R = Right H = Upper-left
The number that follows the letter is the length of the line in pixels.
[edit] END
END
Signifies the end of the program. When QBasic sees this command it usually comes up with a statement saying: "Press Any Key to Continue".
[edit] END IF
END IF
Ends the program if a condition is reached.
[edit] ENVIRON
ENVIRON [string expression]
NOTE: If you are running QBasic on a Windows system, you wont be able to use this command.
This command lets you set an environment variable.
[edit] EOF()
This checks the file that is being read if there is still data to be read.
OPEN File.txt FOR INPUT AS #1 DO INPUT #1, text$ PRINT text$ LOOP UNTIL EOF(1) END
The integer in the EOF brackets is a boolean / binary value, a one or zero. 0 if the line of the file still contains data and a 1 if the line of data stops.
[edit] ERASE
ERASE [arrayname] [,]
Used to erase all dimensioned arrays.
[edit] ERROR
ON ERROR GOTO [stated line label] ERROR [error number]
ON ERROR GOTO is used to go around error events when the occur. ERROR manually causes a fake error, used mainly for troubleshooting the error trapping code. The error number chart is as follows:
1 NEXT without FOR 39 CASE ELSE expected 2 Syntax Error 40 Variable required 3 RETURN without GOSUB 50 FIELD overflow 4 Out of DATA 51 Internal error 5 Illegal function call 52 Bad file name or number 6 Overflow 53 File not found 7 Out of memory 54 Bad file mode 8 Label not defined 55 File already open 9 Subscript out of range 56 FIELD statement active 10 Duplicate definition 57 Device I/O error 11 Division by zero 58 File already exists 12 Illegal in direct mode 59 Bad record length 13 Type mismatch 61 Disk full 14 Out of string space 62 Input past end of file 16 String formula too complex 63 Bad record number 17 Cannot continue 64 Bad file name 18 Function not defined 67 Too many files 19 No RESUME 68 Device unavailable 20 RESUME without error 69 Communication-buffer overflow 24 Device timeout 70 Permission denied 25 Device Fault 71 Disk not ready 26 FOR without NEXT 72 Disk-media error 27 Out of paper 73 Advanced feature unavailable 29 WHILE without WEND 74 Rename across disks 30 WEND without WHILE 75 Path/File access error 33 Duplicate label 76 Path not found 35 Subprogram not defined 37 Argument-count mismatch 38 Array not defined
[edit] FOR
FOR [variable] = [Value]
This is used when mostly dealing with the LOOP and NEXT command. Usually used to input a large amount of numbers. For example:
FOR a = 200 TO 300 PRINT a NEXT a
This program would print out all numbers in between 200 and 300.
[edit] GOSUB
GOSUB [subroutine]
This lets you go into a subroutine in a program. It is used with the RETURN command when writing subroutines.
[edit] IF
IF [variable or string] [operator] [variable or string] THEN [command]
Compares variables or strings. For example, if you wanted to examine whether or not a user-entered password was the correct password, you might enter:
IF a$ = "password" THEN PRINT "Password Correct"
Where a$ is the user entered password. Some operators include:
"="- equal to
"<"- less than (only used when variable or string is a number value)
">"- greater than (only used when variable or string is a number value)
"<>"- does not equal
"<="- less than or equal to (only used when variable or string is a number value)
">="- greater than or equal to (only used when variable or string is a number value)
One can also preform actions to number values then compare them to other strings or variables using the if command, such as in the below examples:
IF a+5 = 15 THEN PRINT "Correct"
IF a*6 = b*8 THEN PRINT "Correct"
[edit] INKEY$
[variable] = INKEY$
This is used when you want a program to function with key input from the keyboard. Look at this example on how this works:
a$ = INKEY$ PRINT "Press Esc to Exit" END IF a$ = CHR$(27)
You can use this in conjunction with the CHR$ command or type the letter (e.g. A).
[edit] INPUT
INPUT [String Literal] [,or;] [Variable]
Displays the String Literal, if a semi colon follows the string literal, a question mark is displayed, and the users input until they hit return is entered into the variable. The variable can be a string or numeric. If a user attempts to enter a string for a numeric variable, the program will ask for the input again. The String Literal is option. If the string literal is used, a comma (,) or semicolon (;) is necessary.
[edit] LET
LET [variable] = [value]
Lets the program know what a variable is valued at.
[edit] LINE
LINE ([X], [Y]) - ([X], [Y]), [Colour Number]
Used for drawing lines in QBasic. The first X and Y are used as coordinates for the beginning of the line and the second set are used for coordinating were the end of the line is. You must put a SCREEN command at the beginning of the program to make it work.
[edit] LOOP
DO [Program] LOOP UNTIL [condition]
Used to create a loop in the program. This command checks the condition after the loop has started. This is used in conjunction with the DO command.
[edit] LPRINT
LPRINT [statement or variable]
Prints out text to a printer.
[edit] OPEN
OPEN "[file name and format]"
This opens a file. You have to write the full name, for example:
OPEN "file.txt"
See that the file name and format of file is written.
[edit] READ
READ [Variable]
Used in conjunction with the DATA command, this command lets QBasic read data. This is mostly used when dealing with large quantities of data like bitmaps.
[edit] REM
REM [Comment]
This is a command that lets anything within the line Written after the REM command not be read by the computer. It is used only to write comments and any notes for aiding the programmer or programmers using the program.
[edit] RETURN
RETURN
Signifies that it is the end of a subroutines
[edit] PLAY
PLAY "[string expression]"
Used to play notes and a score in QBasic. The tones are indicated by letters A through G. Accidentals are indicated with a "+" or "#" (for sharp) or "-" (for flat) after the note letter. See this example:
PLAY "c c# c c#"
There are also codes that sert the duration, octave and tempo. PLAY executes the commands or notes the order in which they appear in the string. Any indicators that change the properties are effective for the notes following that indicator.
Ln Sets the duration (length) of the notes. The variable n does not indicate an actual
duration amount but rather a note type; L1 - whole note, L2 - half note, L4 - quarter note, etc. (L8, L16, L32, L64). By default n = 4. On Sets the current octave. Valid values for n are 0 through 6. An octave begins with C and
ends with B. Remember that C- is equivalent to B. MN ML MS Stand for Music Normal, Music Legato, and Music Staccato. MN - Note duration is 7/8ths of the length indicated by Ln. ML - Note duration is full length of that indicated by Ln. MS - Note duration is 3/4ths of the length indicated by Ln. Pn Causes a silence (pause) for the length of note indicated (same as Ln). Tn Sets the number of "L4"s in a minute. Valid values are from 32 to 255. The default value is T120. . (period) When placed after a note, it causes the duration of the note to be 3/2 of the set duration. This is how to get "dotted" notes. "L4 C#." would play C sharp as a dotted quarter note. MB MF Stand for Music Background and Music Foreground. MB places a maximum of 32 notes in the music buffer and plays them while executing other statements. Works very well for games. MF switches the PLAY mode back to normal. Default is MF.
[edit] PRINT
PRINT [Argument] [,or;] [Argument]...
Displays text to the screen. The Argument can be a string literal, a string variable, a numeric literal or a numeric variable. All arguments are optional.
[edit] PSET
PSET ([X coordinate],[Y coordinate]), [Pixel Colour]
This command displays pixels, either one at a time or a group of them at once. For the command to work, the program must have a SCREEN command in it.
[edit] SCREEN
SCREEN [Screen Mode Number]
This command is used for displaying graphics on the screen. There are ten main types of screen modes that can be used in QBasic depending on the resolution that you want. Here is a list of what screen modes you can choose from:
SCREEN 0: Textmode, cannot be used for graphics. This the screen mode that text based programs run on.
SCREEN 1: 320 x 200 Resolution. Four Colours
SCREEN 2: 640 x 200 Resolution. Two Colours (Black and White)
SCREEN 7: 320 x 200 Resolution. Sixteen Colours
SCREEN 8: 640 x 200 Resolution. Sixteen Colours
SCREEN 9: 640 x 350 Resolution. Sixteen Colours
SCREEN 10: 640 x 350 Resolution. Two Colours (Black and White)
SCREEN 11: 640 x 480 Resolution. Two Colours
SCREEN 12: 640 x 480 Resolution. Sixteen Colours
SCREEN 13: 320 x 200 Resolution. 256 Colours. (Recommended)
[edit] SOUND
SOUND [frequency], [duration]
Unlike the BEEP command, this produces a sound from the PC speakers that is of a varible frequency and duration. The frequency is measured in Hertz and has a range from 37 to 32767. Put in one of these numbers in the frequency section. The duration is clock ticks that is defaulted at 18.2 ticks per second.
[edit] THEN
[Command] [variable] = [value] THEN GOTO [line command value]
Used in conjunction with the GOTO or IF condition commands. It tells the computer what to do if a certain condition has been met.
[edit] TO
[Command] [Variable] = [Value] TO [Value]
Usually used to input a number of variables.
FOR a = 400 TO 500 PRINT a NEXT a
This example will print all numbers from 400 to 500. Instead of declaring all values separately, we can get them all declared in one go.
[edit] Val()
Val([variable])
This turns a string into its numerical value.