DO [{WHILE | UNTIL} condition]
[statementblock]
LOOP
DO
[statementblock]
LOOP [{WHILE | UNTIL} condition]
Example:
i% = 0 PRINT "Value of i% at beginning of loop is "; i% DO WHILE i% < 10 i% = i% + 1 LOOP PRINT "Value of i% at end of loop is "; i%
See also EXIT FOR...NEXT WHILE...WEND
END [{DEF | FUNCTION | IF | SELECT | SUB | TYPE}]
If no argument is supplied, END ends the program and closes all files.
Example:
PRINT "Game over." END
See also DEF FN FUNCTION IF...THEN...ELSE SELECT CASE STOP SUB SYSTEM TYPE
EXIT {DEF | DO | FOR | FUNCTION | SUB}
Example:
i% = 0 DO i% = i% + 1 IF i% = 500 THEN EXIT DO LOOP PRINT "EXIT at"; i%
See also DEF FN DO...LOOP FOR...NEXT FUNCTION SUB
FOR counter = start TO end [STEP increment]
[statementblock]
NEXT [counter [,counter]...]
Example:
FOR i% = 1 TO 15 PRINT i% NEXT i% FOR i% = 7 TO -6 STEP -3 PRINT i% NEXT i%
See also DO...LOOP EXIT WHILE...WEND
IF condition1 THEN
[statementblock-1]
[ELSEIF condition2 THEN
[statementblock-2]]...
[ELSE
[statementblock-n]]
END IF
IF condition THEN statements [ELSE statements]
Example:
INPUT "1 or 2? ", i% IF i% = 1 OR i% = 2 THEN PRINT "OK" ELSE PRINT "Out of range" END IF
See also ON...GOSUB ON...GOTO SELECT CASE
GOSUB line1
.
.
.
RETURN [line2]
If you don't supply a label or line number for RETURN, the program continues execution at the statement following the GOSUB (for subroutine calls) or where an event occurred (for event handling). See the ON keyword for information about event-handling statements.
SUB and CALL statements provide a better alternative to GOSUB subroutines.
Example:
FOR i% = 1 TO 20 GOSUB Square NEXT i% END Square: PRINT i%, i% * i% RETURN
See also CALL ON Keyword ON...GOSUB SUB
GOTO line
DO...LOOP, SELECT CASE, IF...THEN...ELSE, SUB, and FUNCTION provide better ways to control the flow of your program.
GOTO is also used as a keyword in the ON ERROR statement.
See also DO...LOOP FUNCTION IF...THEN...ELSE ON ERROR ON...GOTO SELECT CASE SUB
See also COM, ON COM KEY, ON KEY ON ERROR ON...GOSUB, ON...GOTO PEN, ON PEN PLAY, ON PLAY STRIG, ON STRIG TIMER, ON TIMER
ON expression% GOSUB line-list
ON expression% GOTO line-list
SELECT CASE provides a better way to perform multiple branching.
Example:
FOR i% = 1 TO 2 ON i% GOSUB one, Two NEXT i% END one: PRINT "One" RETURN Two: PRINT "Two" RETURN
See also ON Keyword SELECT CASE
SELECT CASE testexpression
CASE expressionlist1
[statementblock-1]
[CASE expressionlist2
[statementblock-2]]...
[CASE ELSE
[statementblock-n]]
END SELECT
The expressionlist arguments can have any of these forms or a combination of them, separated by commas:
expression[,expression]...
expression TO expression
IS relational-operator expression
Example:
INPUT "Enter acceptable level of risk (1-5): ", Total SELECT CASE Total CASE IS >= 5 PRINT "Maximum risk and potential return." PRINT "Choose stock investment plan." CASE 2 TO 4 PRINT "Moderate to high risk and potential return." PRINT "Choose mutual fund or corporate bonds." CASE 1 PRINT "No risk, low return." PRINT "Choose IRA." END SELECT
See also IF...THEN...ELSE
STOP
The STOP keyword also suspends trapping of events in these statements:
COM, ON COM KEY, ON KEY PEN, ON PEN
PLAY, ON PLAY STRIG, ON STRIG
TIMER, ON TIMER
Example:
FOR i% = 1 TO 10 PRINT i% IF i% = 5 THEN STOP 'STOP pauses; F5 Continues. NEXT i%
SYSTEM
CONST constantname = expression [,constantname = expression]...
Example:
CONST PI = 3.141593 INPUT "Radius of Circle: "; R PRINT "Area = "; PI * R ^ 2
DATA constant[,constant]...
READ variablelist
RESTORE [line]
DATA statements can be entered only at the module level. They cannot be used in procedures.
Example:
FOR i% = 1 TO 3 READ a%, b$ PRINT a%, b$ RESTORE NEXT i% DATA 1, "Repeat"
DIM [SHARED] variable[(subscripts)] [AS type] [,variable[(subscripts)] [AS type]]...
REDIM [SHARED] variable(subscripts) [AS type] [,variable(subscripts) [AS type]]...
DIM declares either static or dynamic arrays. Unless array storage has been determined by $STATIC, $DYNAMIC, or COMMON, arrays dimensioned with numbers are static and arrays dimensioned with variables are dynamic. REDIM always declares dynamic arrays.
Static array storage is allocated when you start a program and remains fixed. Dynamic array storage is allocated while a program runs.
Example:
' $DYNAMIC DIM a(49, 49) REDIM a(19, 14)
See also COMMON ERASE OPTION BASE SHARED, STATIC $STATIC, $DYNAMIC
ERASE arrayname [,arrayname]...
For static arrays, ERASE sets each element of a numeric array to zero and each element of a string array to null.
For dynamic arrays, ERASE frees the memory used by the array. You must redeclare the array's dimensions with REDIM or DIM before using it.
Example:
DIM a%(0) a%(0) = 6 PRINT "Before: "; a%(0) ERASE a% PRINT "After: "; a%(0)
See also CLEAR DIM, REDIM
OPTION BASE {0 | 1}
The DIM statement TO clause provides a better way to set the lower bound of an array subscript.
See also DIM, REDIM LBOUND, UBOUND
REM remark
' remark
Remarks are ignored when the program runs unless they contain metacommands.
A remark can be inserted on a line after an executable statement if it is preceded by the single-quote (') form of REM or if REM is preceded by a colon (:).
Example:
REM This is a comment. ' This is also a comment. PRINT "Test1" 'This is a comment after a PRINT statement. PRINT "Test2": REM This is also a comment after a PRINT statement.
See also $STATIC, $DYNAMIC
SWAP variable1, variable2
Example:
a% = 1: b% = 2 PRINT "Before: "; a%, b% SWAP a%, b% PRINT "After: "; a%, b%
TYPE usertype
elementname AS typename
[elementname AS typename]
.
.
.
END TYPE
Use DIM, REDIM, COMMON, STATIC, or SHARED to create a variable of a user-defined data type.
Example:
TYPE Card Suit AS STRING * 9 Value AS INTEGER END TYPE DIM Deck(1 TO 52) AS Card Deck(1).Suit = "Club" Deck(1).Value = 2 PRINT Deck(1).Suit, Deck(1).Value
See also COMMON DIM, REDIM SHARED, STATIC
[CALL] name [([argumentlist])]
If you omit the CALL keyword, also omit the parentheses around argumentlist. Either declare the procedure in a DECLARE statement before calling it, or save the program and QBasic automatically generates a DECLARE statement.
To specify an argument whose value will not be changed by the procedure, enclose the argument in parentheses.
Example:
The program REMLINE.BAS illustrates calling SUB procedures. To view or run this program, load REMLINE.BAS using the Open
command from the File menu.
See also CALL ABSOLUTE DECLARE SUB
DECLARE {FUNCTION | SUB} name [([parameterlist])]
Example:
The program REMLINE.BAS illustrates declaring FUNCTION and SUB procedures. To view or run this program, load REMLINE.BAS using
the Open command from the File menu.
FUNCTION name [(parameterlist)] [STATIC]
[statementblock]
name = expression
[statementblock]
END FUNCTION
When you call the function, you can specify that an argument's value will not be changed by the function by enclosing the argument in parentheses.
Example:
The program REMLINE.BAS illustrates calling FUNCTION procedures. To view or run this program, load REMLINE.BAS using the Open
command from the File menu.
See also DECLARE DEF FN EXIT SHARED, STATIC SUB
RUN [{linenumber | file$}]
RUN closes all files and clears program memory before loading a program. Use the CHAIN statement to run a program without closing open files.
Example:
'Assumes the program TEST.BAS is in a \DOS directory. RUN "C:\DOS\TEST.BAS"
See also CHAIN
SHELL (commandstring$)
Your program resumes when the DOS command or batch file completes.
If you omit the command string, SHELL invokes a DOS shell and displays the DOS prompt. Use the EXIT command to resume your program.
Example:
SHELL "DIR"
SHARED variable[()] [AS type] [,variable[()] [AS type]]...
STATIC variable[()] [AS type] [,variable[()] [AS type]]...
Example:
The program REMLINE.BAS illustrates using the SHARED and STATIC statements. To view or run this program, load REMLINE.BAS using
the Open command from the File menu.
See also COMMON DIM, REDIM
SUB name[(parameterlist)] [STATIC]
[statementblock]
END SUB
When you call the SUB procedure, you can specify that an argument's value will not be changed by the procedure by enclosing the argument in parentheses.
Example:
The program REMLINE.BAS illustrates calling SUB procedures. To view or run this program, load REMLINE.BAS using the Open
command from the File menu.
See also CALL DECLARE EXIT FUNCTION SHARED, STATIC
CLS [{0 | 1 | 2}]
See also VIEW VIEW PRINT WINDOW
LOCATE [row%] [,[column%] [,[cursor%] [,start% [,stop%]]]]
CSRLIN
POS(expression)
Example:
CLS LOCATE 5, 5 MyRow% = CSRLIN MyCol% = POS(0) PRINT "Position 1 (Press any key)" DO LOOP WHILE INKEY$ = "" LOCATE (MyRow% + 2), (MyCol% + 2) PRINT "Position 2"
INKEY$
INKEY$ returns a null string if there is no character to return.
For standard keys, INKEY$ returns a 1-byte string containing the character read.
For extended keys, INKEY$ returns a 2-byte string made up of the null character (ASCII 0) and the keyboard scan code.
Example:
PRINT "Press Esc to exit..." DO LOOP UNTIL INKEY$ = CHR$(27) '27 is the ASCII code for Esc.
See also Keyboard Scan Codes
INP(port%)
OUT port%, data%
Example:
x% = INP(&H3FC) 'Read COM1 Modem Control Register. OUT &H3FC, (x% XOR 1) 'Change Data Terminal Ready bit.
See also WAIT
INPUT [;] ["prompt"{; | ,}] variablelist
LINE INPUT [;] ["prompt";] variable$
INPUT #filenumber%, variablelist
LINE INPUT #filenumber%, variable$
INPUT uses a comma as a separator between entries.
LINE INPUT reads all characters up to a carriage return.
For keyboard input, a semicolon immediately after INPUT keeps the cursor on the same line after the user presses the Enter key.
Example:
CLS OPEN "LIST" FOR OUTPUT AS #1 DO INPUT " NAME: ", Name$ 'Read entries from the keyboard. INPUT " AGE: ", Age$ WRITE #1, Name$, Age$ INPUT "Add another entry"; R$ LOOP WHILE UCASE$(R$) = "Y" CLOSE #1 'Echo the file back. OPEN "LIST" FOR INPUT AS #1 CLS PRINT "Entries in file:": PRINT DO WHILE NOT EOF(1) LINE INPUT #1, REC$ 'Read entries from the file. PRINT REC$ 'Print the entries on the screen. LOOP CLOSE #1 KILL "LIST"
See also INKEY$ INPUT$ OPEN Statement File Modes
KEY key%, stringexpression$
KEY LIST
KEY ON
KEY OFF
Example:
KEY 4, "MENU" + CHR$(13) KEY LIST KEY 4, "" KEY LIST
See also KEY, ON KEY (Event Trapping)
LPOS(n%)
Example:
'This example requires a printer. LPRINT FOR i% = 1 TO 20 LPRINT i%; IF LPOS(1) >= 10 THEN LPRINT 'Begin a new line. NEXT i%
PRINT [#filenumber%,] [expressionlist] [{; | ,}]
LPRINT [expressionlist] [{; | ,}]
Example:
OPEN "TEST.DAT" FOR OUTPUT AS #1 PRINT #1, USING "##.### "; 12.12345 CLOSE OPEN "TEST.DAT" FOR INPUT AS #1 INPUT #1, a$ PRINT a$ LPRINT "This is a line"; 1 LPRINT "This is a line", LPRINT 2
See also PRINT USING, LPRINT USING WIDTH WRITE
PRINT [#filenumber%,] USING formatstring$; expressionlist [{; | ,}]
LPRINT USING formatstring$; expressionlist [{; | ,}]
Example:
a = 123.4567 PRINT USING "###.##"; a LPRINT USING "+###.####"; a a$ = "ABCDEFG" PRINT USING "!"; a$ LPRINT USING "\ \"; a$
See also PRINT, LPRINT WIDTH
OPEN "COMn: optlist1 optlist2" [FOR mode] AS [#]filenum% [LEN=reclen%]
Defaults: 300 baud, even parity, 7 data bits, 1 stop bit.
Option | Description |
---|---|
ASC | Opens the device in ASCII mode. |
BIN | Opens the device in binary mode. |
CD[m] | Sets the timeout period (in milliseconds) on the DATA Carrier Detect (DCD) line. |
CS[m] | Sets the timeout period (in milliseconds) on the Clear to Send (CTS) line. |
DS[m] | Sets the timeout period (in milliseconds) on the DATA Set Ready (DS) line. |
LF | Sends a line-feed character after a carriage return. |
OP[m] | Specifies how long (in milliseconds) OPEN COM waits for all communications lines to become open. |
RB[n] | Sets the size (in bytes) of the receive buffer. |
RS | Suppresses detection of Request to Send (RTS). |
TB[n] | Sets the size (in bytes) of the transmit buffer. |
Example:
'Use this example for trouble shooting serial communications problems. 'Slow baud, hardware handshaking is ignored and buffers are enlarged. OPEN "COM1:300,N,8,1,CD0,CS0,DS0,OP0,RS,TB2048,RB2048" FOR RANDOM AS #1
See also OPEN
SPC(n%)
Example:
PRINT "Text1"; SPC(10); "Text2"
See also PRINT, LPRINT PRINT USING, LPRINT USING SPACE$ TAB
SCREEN (row%,column% [,colorflag%])
Value | Returns |
---|---|
0 (or omitted) | The character's ASCII code. |
1 | The character's color attribute. |
Example:
CLS PRINT "Hello" PRINT "The ASCII value of character at 1,1 is"; SCREEN(1, 1)
See also POINT SCREEN Statement ASCII Character Codes Color Attributes and Values
TAB(column%)
Example:
PRINT TAB(25); "Text"
See also PRINT, LPRINT PRINT USING, LPRINT USING SPC SPACE$
VIEW PRINT [toprow% TO bottomrow%]
If you omit the toprow% and bottomrow% arguments, VIEW PRINT sets the entire screen as the text viewport.
Ranges for toprow% and bottomrow% depend on the screen mode.
Example:
VIEW PRINT 10 TO 15 FOR i% = 1 TO 100 'Output will scroll. PRINT i% NEXT i%
See also CLS LOCATE PRINT, LPRINT SCREEN WIDTH Screen Modes
WAIT portnumber%, AND-expression% [,XOR-expression%]
Example:
'Reads the interrupt controller port address &H20. 'Press any key to continue. WAIT &H20, 1
See also INP, OUT Boolean Operators
WIDTH [columns%] [,rows%]
WIDTH {#filenumber% | device$}, columns%
WIDTH LPRINT columns%
Example:
OPEN "LPT1:" FOR OUTPUT AS #1 WIDTH #1, 132
See also PRINT, LPRINT SCREEN VIEW PRINT
CIRCLE [STEP] (x!,y!),radius![,[color%] [,[start!] [,[end!] [,aspect!]]]]
To convert from degrees to radians, multiply degrees by (PI / 180).
Example:
'This example requires a color graphics adapter. SCREEN 2 CIRCLE (320, 100), 200 CIRCLE STEP(0, 0), 100
See also COLOR DRAW LINE SCREEN VIEW WINDOW Color Attributes and Values Screen Modes
COLOR [foreground%] [,[background%] [,border%]] (Screen mode 0 (text only))
COLOR [background%] [,palette%] (Screen mode 1)
COLOR [foreground%] (Screen modes 4, 12, 13)
COLOR [foreground%] [,background&] (Screen modes 7-10)
palette% | Attribute 1 | Attribute 2 | Attribute 3 |
---|---|---|---|
0 | Green | Red | Brown |
1 | Cyan | Magenta | Bright white |
The available color attributes and values depend on your graphics adapter and the screen mode set by the most recent SCREEN statement.
If your system is equipped with an EGA, VGA, or MCGA adapter, use the PALETTE statement to change the color assignments of color attributes.
Example:
'This example requires a color graphics adapter. SCREEN 7 FOR i% = 0 TO 15 COLOR i% PRINT i% NEXT i%
See also DRAW PAINT PALETTE, PALETTE USING SCREEN Color Attributes and Values Screen Modes
GET [STEP](x1!,y1!)-[STEP](x2!,y2!), arrayname[(index%)]
PUT [STEP] (x1!,y1!), arrayname[(index%)] [,actionverb]
Keyword | Action |
---|---|
AND | Merges stored image with an existing image. |
OR | Superimposes stored image on existing image. |
PSET | Draws stored image, erasing existing image. |
PRESET | Draws stored image in reverse colors, erasing existing image. |
XOR | Draws a stored image or erases a previously drawn image while preserving the background, producing animation effects. |
A PUT statement should always be executed in the same screen mode as the GET statement used to capture the image, or a compatible mode. See Screen Image Arrays and Compatibility.
Example:
'This example requires a color graphics adapter. SCREEN 1 DIM box%(1 TO 200) x1% = 0: x2% = 10: y1% = 0: y2% = 10 LINE (x1%, y1%)-(x2%, y2%), 2, BF GET (x1%, y1%)-(x2%, y2%), box% DO PUT (x1%, y1%), box%, XOR x1% = RND * 300 y1% = RND * 180 PUT (x1%, y1%), box% LOOP WHILE INKEY$ = ""
See also SCREEN Screen Modes
LINE [[STEP](x1!,y1!)]-[STEP](x2!,y2!) [,[color%] [,[B | BF] [,style%]]]
Example:
'This example requires a color graphics adapter. SCREEN 1 LINE (110, 70)-(190, 120), , B LINE (0, 0)-(320, 200), 3, , &HFF00
See also CIRCLE INPUT, LINE INPUT PRESET, PSET SCREEN Color Attributes and Values Screen Modes
PAINT [STEP] (x!,y!)[,[{color% | tile$}] [,[bordercolor%] [,background$]]]
The available color attributes depend on your graphics adapter and the screen mode set by the most recent SCREEN statement.
Example:
'This example requires a color graphics adapter. SCREEN 1 CIRCLE (106, 100), 75, 1 LINE (138, 35)-(288, 165), 1, B PAINT (160, 100), 2, 1
See also ASC, CHR$ CIRCLE DRAW LINE SCREEN Color Attributes and Values Screen Modes
PALETTE [attribute%,color&]
PALETTE USING arrayname#((index%))
The available color attributes and values depend on your graphics adapter and the screen mode set by the most recent SCREEN statement.
Example:
'This example requires a color graphics adapter. PALETTE 0, 1 SCREEN 1 FOR i% = 0 TO 3: A%(i%) = i%: NEXT i% LINE (138, 35)-(288, 165), 3, BF LINE (20, 10)-(160, 100), 2, BF DO FOR i% = 0 TO 3 A%(i%) = (A%(i%) + 1) MOD 16 NEXT i% PALETTE USING A%(0) LOOP WHILE INKEY$ = ""
See also COLOR SCREEN Color Attributes and Values Screen Modes
PCOPY sourcepage%, destinationpage%
The value that identifies the video page is determined by the size of video memory and the current screen mode.
Example:
PCOPY 1, 3
See also SCREEN Screen Modes
PMAP (startcoordinate#, n%)
startcoordinate# | n% | Returns |
---|---|---|
Window x coordinate | 0 | Viewport x coordinate |
Window y coordinate | 1 | Viewport y coordinate |
Viewport x coordinate | 2 | Window x coordinate |
Viewport y coordinate | 3 | Window y coordinate |
Example:
'This example requires a graphics adapter that supports screen mode 1. SCREEN 1 WINDOW SCREEN (0, 0)-(100, 100) PRINT "Logical x=50, physical x="; PMAP(50, 0) PRINT "Logical y=50, physical y="; PMAP(50, 1)
POINT {(n%) | (x%,y%)}
n% | Returns |
---|---|
0 | The current viewport x coordinate |
1 | The current viewport y coordinate |
2 | The current window x coordinate |
3 | The current window y coordinate |
Example:
'This example requires a color graphics adapter. SCREEN 1 LINE (0, 0)-(100, 100), 2 LOCATE 14, 1 FOR y% = 1 TO 10 FOR x% = 1 TO 10 PRINT POINT(x%, y%); NEXT x% PRINT NEXT y%
See also COLOR PMAP SCREEN VIEW WINDOW Color Attributes and Values
PRESET [STEP] (x!,y!) [,color%]
PSET [STEP] (x!,y!) [,color%]
Available color attributes depend on your graphics adapter and screen mode. Coordinate values depend on the graphics adapter, screen mode, and most recent VIEW and WINDOW statements.
Example:
'This example requires a color graphics adapter. SCREEN 1 FOR i% = 0 TO 320 PSET (i%, 100) FOR delay% = 1 TO 100: NEXT delay% PRESET (i%, 100) NEXT i%
See also SCREEN VIEW WINDOW Color Attributes and Values Screen Modes
SCREEN mode% [,[colorswitch%] [,[activepage%] [,visualpage%]]]
Mode | Value | Action |
---|---|---|
0 | 0 | Disables color |
0 | Nonzero | Enables color |
1 | 0 | Enables color |
1 | Nonzero | Disables color |
Example:
'This example requires a color graphics adapter. SCREEN 1 '320 x 200 graphics LINE (110, 70)-(190, 120), , B LINE (0, 0)-(320, 200), 3, , &HFF00
See also CIRCLE COLOR DRAW LINE PAINT SCREEN Function VIEW WINDOW Screen Modes
VIEW [[SCREEN] (x1!,y1!)-(x2!,y2!) [,[color%] [,border%]]]
If all arguments are omitted, the entire screen is the viewport.
The available color attributes depend on your graphics adapter and the screen mode set by the most recent SCREEN statement.
Example:
'This example requires a color graphics adapter. SCREEN 1 VIEW (10, 10)-(300, 180), , 1 LOCATE 1, 11: PRINT "A big graphics viewport"; VIEW SCREEN (80, 80)-(200, 125), , 1 LOCATE 11, 11: PRINT "A small graphics viewport";
See also CLS SCREEN VIEW PRINT WINDOW Color Attributes and Values Screen Modes
WINDOW [[SCREEN] (x1!,y1!)-(x2!,y2!)]
WINDOW with no arguments disables the logical coordinate system.
Use the VIEW statement to change the size of the viewport.
Example:
'This example requires a color graphics adapter. SCREEN 1 FOR i% = 1 TO 10 STEP 2 WINDOW (-160 / i%, -100 / i%)-(160 / i%, 100 / i%) CIRCLE (0, 0), 10 NEXT i%
See also CLS PMAP POINT SCREEN VIEW WIDTH
CHDIR pathname$
MKDIR pathname$
RMDIR pathname$
FILES [filespec$]
Example:
MKDIR "C:\TEMP\TEST" CHDIR "C:\TEMP" FILES RMDIR "TEST"
KILL filespec$
Example:
INPUT "File to delete: "; f$ KILL f$
See also FILES
NAME oldspec$ AS newspec$
Example:
INPUT "Old Name: "; OldFN$ INPUT "New Name: "; NewFN$ NAME OldFN$ AS NewFN$
See also FILES
CLOSE [[#]filenumber%[,[#]filenumber%]...]
CLOSE with no arguments closes all open files and devices.
Example:
CLS INPUT "Enter filename: ", n$ OPEN n$ FOR OUTPUT AS #1 PRINT #1, "This is saved to the file." CLOSE OPEN n$ FOR INPUT AS #1 INPUT #1, a$ PRINT "Read from file: "; a$ CLOSE
EOF(filenumber%)
Example:
CLS OPEN "TEST.DAT" FOR OUTPUT AS #1 FOR I% = 1 TO 10 WRITE #1, I%, 2 * I%, 5 * I% NEXT I% CLOSE #1 OPEN "TEST.DAT" FOR INPUT AS #1 DO LINE INPUT #1, a$ PRINT a$ LOOP UNTIL (EOF(1))
FILEATTR(filenumber%,attribute%)
Value | mode |
---|---|
1 | Input |
2 | Output |
4 | Random |
8 | Append |
32 | Binary |
Example:
OPEN "TEST.DAT" FOR BINARY AS #1 PRINT FILEATTR(1, 1) CLOSE
See also OPEN
FREEFILE
Example:
OPEN "TEST.DAT" FOR OUTPUT AS #1 PRINT "Next file number: "; FREEFILE CLOSE
See also OPEN
GET [#]filenumber%[,[recordnumber&][,variable]]
PUT [#]filenumber%[,[recordnumber&][,variable]]
Example:
TYPE TestRecord Student AS STRING * 20 Score AS SINGLE END TYPE DIM MyClass AS TestRecord OPEN "FINAL.DAT" FOR RANDOM AS #1 LEN = LEN(MyClass) MyClass.Student = "MarySa" MyClass.Score = 99 PUT #1, 1, MyClass CLOSE #1 OPEN "FINAL.DAT" FOR RANDOM AS #1 LEN = LEN(MyClass) GET #1, 1, MyClass PRINT "STUDENT:", MyClass.Student PRINT "SCORE:", MyClass.Score CLOSE #1 KILL "FINAL.DAT"
See also FIELD GET, PUT (Graphics) LSET, RSET MKn$, CVn Functions TYPE
INPUT$(n[,[#]filenumber%])
Example:
OPEN "TEST.DAT" FOR OUTPUT AS #1 PRINT #1, "The text" CLOSE OPEN "TEST.DAT" FOR INPUT AS #1 PRINT INPUT$(3, 1) 'Print first 3 characters. CLOSE
See also INPUT, LINE INPUT
LOC(filenumber%)
For binary files, LOC returns the position of the last byte read or written.
For random-access files, LOC returns the number of the last record read from or written to the file.
For sequential files, LOC returns the current byte position in the file, divided by 128.
Example:
OPEN "TEST.DAT" FOR RANDOM AS #1 FOR i% = 1 TO 10 PUT #1, , i% NEXT i% SEEK #1, 2 GET #1, , i% PRINT "Data: "; i%; " Current record: "; LOC(1); " Next: "; SEEK(1)
LOCK [#]filenumber% [,{record& | [start&] TO end&}]
UNLOCK [#]filenumber% [,{record& | [start&] TO end&}]
For sequential files, LOCK and UNLOCK affect the entire file.
Example:
'This example runs only in a network environment. OPEN "TEST.DAT" FOR RANDOM AS #1 FOR i% = 1 TO 10 PUT #1, , i% NEXT i% LOCK #1, 2 'Lock record 2. GET #1, 2, i% UNLOCK #1, 2 'Unlock record 2.
LOF(filenumber%)
Example:
INPUT "Enter filename: "; f$ OPEN f$ FOR BINARY AS #1 PRINT "File length = "; LOF(1) CLOSE
OPEN file$ [FOR mode] [ACCESS access] [lock] AS [#]filenumber% [LEN=reclen%]
Example:
INPUT "Enter Filename: "; n$ OPEN n$ FOR OUTPUT AS #1 PRINT #1, "This is saved to the file." CLOSE OPEN n$ FOR INPUT AS #1 INPUT #1, a$ PRINT "Read from file: "; a$ CLOSE
See also CLOSE FREEFILE OPEN COM TYPE OPEN Statement Alternate Syntax
SEEK(filenumber%)
SEEK [#]filenumber%, position&
Example:
OPEN "TEST.DAT" FOR RANDOM AS #1 FOR i% = 1 TO 10 PUT #1, , i% NEXT i% SEEK #1, 2 GET #1, , i% PRINT "Data: "; i%; " Current record: "; LOC(1); " Next: "; SEEK(1)
WRITE [[#]filenumber%,] expressionlist
WRITE inserts commas between items and quotation marks around strings as they are written. WRITE writes values to a file in a form that can be read by the INPUT statement.
Example:
CLS OPEN "LIST" FOR OUTPUT AS #1 DO INPUT " NAME: ", Name$ INPUT " AGE: ", Age$ WRITE #1, Name$, Age$ INPUT "Add another entry"; R$ LOOP WHILE UCASE$(R$) = "Y" CLOSE #1 'Print the file to the screen. OPEN "LIST" FOR INPUT AS #1 CLS PRINT "Entries in file:": PRINT DO WHILE NOT EOF(1) INPUT #1, Rec1$, Rec2$ 'Read entries from file. PRINT Rec1$, Rec2$ 'Print the entries on the screen. LOOP CLOSE #1 KILL "LIST"
See also INPUT, LINE INPUT OPEN PRINT, LPRINT
CLEAR [,,stack&]
Example:
CLEAR ,,2000
See also ERASE
FRE(numeric-expression)
FRE(stringexpression$)
Value | FRE returns |
---|---|
-1 | The size of the largest array (nonstring) you can create |
-2 | The unused stack space |
Any other number | The available string space |
Example:
PRINT "String Space", FRE("") PRINT "Unused Stack Space", FRE(-2) PRINT "Array Space", FRE(-1)
PEEK(address)
POKE address,byte%
Example:
DEF SEG = 0 Status% = PEEK(&H417) 'Read keyboard status. POKE &H417, (Status% XOR &H40) 'Change Caps Lock state, bit 6.
See also DEF SEG
HEX$(numeric-expression&)
OCT$(numeric-expression&)
Example:
INPUT x a$ = HEX$ (x) b$ = OCT$ (x) PRINT x; "decimal is "; a$; " hexadecimal and "; b$; " in octal."
INSTR([start%,]stringexpression1$,stringexpression2$)
Example:
a$ = "Microsoft QBasic" PRINT "String position ="; INSTR(1, a$, "QBasic")
See also LEFT$, RIGHT$ LEN MID$
LCASE$(stringexpression$)
UCASE$(stringexpression$)
Example:
Test$ = "THE string" PRINT Test$ PRINT LCASE$(Test$); " in lowercase" PRINT UCASE$(Test$); " IN UPPERCASE"
LEFT$(stringexpression$,n%)
RIGHT$(stringexpression$,n%)
Example:
a$ = "Microsoft QBasic" PRINT LEFT$(a$, 5) 'Output is: Micro PRINT RIGHT$(a$, 5) 'Output is: Basic
See also MID$
LEN(stringexpression$)
LEN(variable)
Example:
a$ = "Microsoft QBasic" PRINT LEN(a$)
See also OPEN
LSET stringvariable$=stringexpression$
RSET stringvariable$=stringexpression$
LSET recordvariable1=recordvariable2
Example:
OPEN "FILEDAT.DAT" FOR RANDOM AS #1 LEN = 10 FIELD #1, 5 AS Ls1$, 5 AS Rs1$ LSET Ls1$ = "LSET" RSET Rs1$ = "RSET" PUT #1, 1 CLOSE #1 OPEN "FILEDAT.DAT" FOR RANDOM AS #1 LEN = 10 FIELD #1, 5 AS Ls2$, 5 AS Rs2$ GET #1, 1 PRINT "*" + Ls2$ + "*", "*" + Rs2$ + "*" CLOSE #1
LTRIM$(stringexpression$)
RTRIM$(stringexpression$)
Example:
a$ = " Basic " PRINT "*" + a$ + "*" 'Output is: * Basic * PRINT "*" + LTRIM$(a$) + "*" 'Output is: *Basic * PRINT "*" + RTRIM$(a$) + "*" 'Output is: * Basic*
MID$(stringexpression$,start%[,length%])
MID$(stringvariable$,start%[,length%])=stringexpression$
Example:
a$ = "Where is Paris?" PRINT MID$(a$, 10, 5) 'Output is: Paris Text$ = "Paris, France" PRINT Text$ 'Output is: Paris, France MID$(Text$, 8) = "Texas " PRINT Text$ 'Output is: Paris, Texas
See also LEFT$, RIGHT$ LEN
SPACE$(n%)
Example:
FOR i% = 1 TO 5 x$ = SPACE$ (i%) PRINT x$; i% NEXT i%
STR$(numeric-expression)
VAL(stringexpression$)
Example:
PRINT "Decimal 65 is represented in hexadecimal as "; PRINT "&H" + LTRIM$(STR$(41)) PRINT VAL(RIGHT$("Microsoft 1990", 4))
STRING$(length%,{ascii-code% | stringexpression$})
Example:
PRINT STRING$(5, "-"); PRINT "Hello"; PRINT STRING$(5, "-")
See also ASCII Character Codes
ABS(numeric-expression)
SGN(numeric-expression)
Example:
PRINT ABS(45.5 - 100!) 'Output is: 54.5 PRINT SGN(1), SGN(-1), SGN(0) 'Output is: 1 -1 0
ASC(stringexpression$)
CHR$(ascii-code%)
Example:
PRINT ASC("Q") 'Output is: 81 PRINT CHR$(65) 'Output is: A
See also ASCII Character Codes
ATN(numeric-expression)
COS(angle)
SIN(angle)
TAN(angle)
The ATN function returns an angle in radians.
To convert from degrees to radians, multiply degrees by (PI / 180).
Example:
CONST PI=3.141592654 PRINT ATN(TAN(PI/4.0)), PI/4.0 'Output is: .7853981635 .7853981635 PRINT (COS(180 * (PI / 180))) 'Output is: -1 PRINT (SIN(90 * (PI / 180))) 'Output is: 1 PRINT (TAN(45 * (PI / 180))) 'Output is: 1.000000000205103
CDBL(numeric-expression)
CSNG(numeric-expression)
Example:
PRINT 1 / 3, CDBL(1 / 3) 'Output is: .3333333 .3333333333333333 PRINT CSNG(975.3421515#) 'Output is: 975.3422
See also CINT, CLNG
CINT(numeric-expression)
CLNG(numeric-expression)
Example:
PRINT CINT(12.49), CINT(12.51) 'Output is: 12 13 PRINT CLNG(338457.8) 'Output is: 338458
See also CDBL, CSNG FIX, INT
MKSMBF$(single-precision-expression!)
MKDMBF$(double-precision-expression#)
CVSMBF (4-byte-numeric-string)
CVDMBF (8-byte-numeric-string)
Function | Returns |
---|---|
MKSMBF$ | A 4-byte string containing a Microsoft-Binary-format number |
MKDMBF$ | An 8-byte string containing a Microsoft-Binary-format number |
CVSMBF | A single-precision number in IEEE format |
CVDMBF | A double-precision number in IEEE format |
These functions are useful for maintaining data files created with older versions of Basic.
Example:
TYPE Buffer SngNum AS STRING * 4 DblNum AS STRING * 8 END TYPE DIM RecBuffer AS Buffer OPEN "TESTDAT.DAT" FOR RANDOM AS #1 LEN = 12 SNum = 98.9 DNum = 645.3235622# RecBuffer.SngNum = MKSMBF$(SNum) RecBuffer.DblNum = MKDMBF$(DNum) PUT #1, 1, RecBuffer GET #1, 1, RecBuffer CLOSE #1 PRINT CVSMBF(RecBuffer.SngNum), CVDMBF(RecBuffer.DblNum)
EXP(numeric-expression)
LOG(numeric-expression)
Example:
PRINT EXP(0), EXP(1) 'Output is: 1 2.718282 PRINT LOG(1), LOG(EXP(1)) 'Output is: 0 1
FIX(numeric-expression)
INT(numeric-expression)
Example:
PRINT FIX(12.49), FIX(12.54) 'Output is: 12 12 PRINT INT(12.54), INT(-99.4) 'Output is: 12 -100
See also CINT, CLNG
RANDOMIZE [seed%]
RND[(n#)]
n# | RND returns |
---|---|
Less than 0 | The same number for any n# |
Greater than 0 (or omitted) | The next random number |
0 | The last number generated |
Example:
RANDOMIZE TIMER x% = INT(RND * 6) + 1 y% = INT(RND * 6) + 1 PRINT "Roll of two dice: die 1 ="; x%; "and die 2 ="; y%
SQR(numeric-expression)
Example:
PRINT SQR(25), SQR(2) 'Output is: 5 1.414214
TIME$
TIME$ = stringexpression$
The TIME$ function returns a string in the form hh:mm:ss.
Example:
PRINT TIME$ TIME$ = "08:00:58" 'Note: The new system time remains in effect until ' you change it again. PRINT "Time set to "; TIME$
See also DATE$
COM(n%) ON
COM(n%) OFF
COM(n%) STOP
ON COM(n%) GOSUB line
Example:
COM(1) ON 'Enable event trapping on port 1. ON COM(1) GOSUB ComHandler DO : LOOP WHILE INKEY$ = "" COM(1) OFF END ComHandler: PRINT "Something was typed at the terminal attached to COM1." RETURN
See also OPEN COM
ERDEV
ERDEV$
The low byte of the value returned by ERDEV contains the DOS error code. The high byte contains device-attribute information.
ERR
ERL
ERL does not return line labels. If there are no line numbers in the program, ERL returns 0.
See also ERDEV, ERDEV$ ERROR ON ERROR RESUME Run-Time Error Codes
ERROR expression%
See also ERDEV, ERDEV$ ERR, ERL ON ERROR RESUME
KEY(n%) ON
KEY(n%) OFF
KEY(n%) STOP
ON KEY(n%) GOSUB line
n% | Key |
---|---|
0 | All keys listed here (KEY(0) ON, KEY(0) OFF, and KEY(0) STOP only). |
1-10 | Function keys F1-F10. |
11 | Up Arrow key. |
12 | Left Arrow key. |
13 | Right Arrow key. |
14 | Down Arrow key. |
15-25 | User-defined keys. For more information, see Declaring User-Defined Keys. |
30, 31 | Function keys F11 and F12. |
Example:
'This example requires Caps Lock and Num Lock to be off. CONST ESC = 27 KEY 15, CHR$(&H4) + CHR$(&H1F) 'Set up Ctrl+S as KEY 15. ON KEY(15) GOSUB PauseHandler KEY(15) ON WHILE INKEY$ <> CHR$(ESC) PRINT "Press Esc to stop, Ctrl+S to pause." PRINT WEND END PauseHandler: SLEEP 1 RETURN
See also KEY (Assignment) Declaring User-Defined Keys
ON ERROR {GOTO line | RESUME NEXT}
If ON ERROR isn't used, any run-time error ends your program.
See also ERDEV, ERDEV$ ERR, ERL ERROR GOTO RESUME
PEN ON
PEN OFF
PEN STOP
ON PEN GOSUB line
Example:
'This example requires a light pen. ON PEN GOSUB Handler PEN ON PRINT "Press Esc to exit." DO UNTIL INKEY$ = CHR$(27): LOOP END Handler: PRINT "Pen is at row"; PEN(6); ", column"; PEN(7) RETURN
See also PEN Function
PLAY ON
PLAY OFF
PLAY STOP
ON PLAY(queuelimit%) GOSUB line
Example:
ON PLAY(3) GOSUB Background PLAY ON Music$ = "MBo3L8ED+ED+Eo2Bo3DCL2o2A" PLAY Music$ LOCATE 2, 1: PRINT "Press any key to stop."; DO WHILE INKEY$ = "": LOOP END Background: i% = i% + 1 LOCATE 1, 1: PRINT "Background called "; i%; "time(s)"; PLAY Music$ RETURN
See also PLAY (Music) PLAY Function
STRIG(n%) ON
STRIG(n%) OFF
STRIG(n%) STOP
ON STRIG(n%) GOSUB line
n% | Trigger |
---|---|
0 | Lower trigger, joystick A |
2 | Lower trigger, joystick B |
4 | Upper trigger, joystick A |
6 | Upper trigger, joystick B |
Example:
'This example requires a joystick. ON STRIG(0) GOSUB Handler STRIG(0) ON PRINT "Press Esc to exit." DO UNTIL INKEY$ = CHR$(27): LOOP END Handler: PRINT "Joystick trigger is depressed." RETURN
See also STICK STRIG Function
TIMER ON
TIMER OFF
TIMER STOP
ON TIMER(n%) GOSUB line
Example:
ON TIMER(1) GOSUB TimeUpdate TIMER ON CLS PRINT "Time: "; TIME$ StartTime = TIMER WHILE TimePast < 10 TimePast = TIMER - StartTime WEND END TimeUpdate: LOCATE 1, 7: PRINT TIME$ RETURN
See also TIMER Function
PLAY commandstring$
Octave and tone commands: | |
---|---|
Ooctave | Sets the current octave (0 - 6). |
< or > | Moves up or down one octave. |
A - G | Plays the specified note in the current octave. |
Nnote | Plays a specified note (0 - 84) in the seven-octave range (0 is a rest). |
Duration and tempo commands: | |
Llength | Sets the length of each note (1 - 64). L1 is whole note, L2 is a half note, etc. |
ML | Sets music legato. |
MN | Sets music normal. |
MS | Sets music staccato. |
Ppause | Specifies a pause (1 - 64). P1 is a whole-note pause, P2 is a half-note pause, etc. |
Ttempo | Sets the tempo in quarter notes per minute (32 - 255). |
Mode commands: | |
MF | Plays music in foreground. |
MB | Plays music in background. |
Suffix commands: | |
# or + | Turns preceding note into a sharp. |
- | Turns preceding note into a flat. |
. | Plays the preceding note 3/2 as long as specified. |
To execute a PLAY command substring from a PLAY command string, use the "X" command:
PLAY "X"+ VARPTR$(commandstring$)
Example:
'Play scale in 7 different octaves scale$ = "CDEFGAB" PLAY "L16" FOR i% = 0 TO 6 PLAY "O" + STR$(i%) PLAY "X" + VARPTR$(scale$) NEXT i%
See also BEEP PLAY Function PLAY, ON PLAY (Event Trapping) SOUND VARPTR$
RESUME [{line | NEXT}]
TIMER
Use TIMER to time programs or parts of programs, or with the RANDOMIZE statement to seed the random-number generator.
Example:
RANDOMIZE TIMER
See also RANDOMIZE, RND TIMER, ON TIMER Statements
BEEP
result = expression1 boolean-operator expression2
NOT | Bit-wise complement |
AND | Conjunction |
OR | Disjunction (inclusive "or") |
XOR | Exclusive "or" |
EQV | Equivalence |
IMP | Implication |
Each operator returns results as indicated in the following truth table. T is true (nonzero); F is false (zero):
NOT | AND | OR | XOR | EQV | IMP | |
---|---|---|---|---|---|---|
Expression1 | T F | T T F F | T T F F | T T F F | T T F F | T T F F |
Expression2 | T F T F | T F T F | T F T F | T F T F | T F T F | |
Result | F T | T F F F | T T T F | F T T F | T F F T | T F T T |
Boolean operations are performed after arithmetic and relational operations in order of precedence.
Expressions are converted to integers or long integers before a Boolean operation is performed.
If the expressions evaluate to 0 or -1, a Boolean operation returns 0 or -1 as the result. Because Boolean operators do bit-wise calculations, using values other than 0 for false and -1 for true may produce unexpected results.
CALL ABSOLUTE ([argumentlist,] offset%)
Example:
'Calls routine for printing the screen to a local printer. DIM a%(2) DEF SEG = VARSEG(a%(0)) FOR I% = 0 TO 2 READ D% POKE VARPTR(a%(0)) + I%, D% NEXT I% DATA 205, 5, 203 : ' int 5 retf 'Machine-language code 'for printing screen. CALL ABSOLUTE(VARPTR(a%(0))) DEF SEG
See also CALL VARPTR, VARSEG
CHAIN filespec$
Example:
'Assumes the program TEST.BAS is in a \DOS directory. CHAIN "C:\DOS\TEST.BAS"
Color monitor | Monochrome monitor | |||
Color attribute | Default color value(a) | Displayed color | Default color value | Displayed color |
SCREEN Modes 0, 7, 8, 9(b), 12, and 13 | ||||
0 | 0 | Black | 0(c) | Off |
1 | 1 | Blue | Underlined(d) | |
2 | 2 | Green | 1(c) | On(d) |
3 | 3 | Cyan | 1(c) | On(d) |
4 | 4 | Red | 1(c) | On(d) |
5 | 5 | Magenta | 1(c) | On(d) |
6 | 6 | Brown | 1(c) | On(d) |
7 | 7 | White | 1(c) | On(d) |
8 | 8 | Gray | 0(c) | Off |
9 | 9 | Light blue | High-intensity underlined | |
10 | 10 | Light green | 2(c) | High-intensity |
11 | 11 | Light cyan | 2(c) | High-intensity |
12 | 12 | Light red | 2(c) | High-intensity |
13 | 13 | Light magenta | 2(c) | High-intensity |
14 | 14 | Yellow | 2(c) | High-intensity |
15 | 15 | High-intensity white | 0(c) | Off |
SCREEN Modes 1 and 9(e) | ||||
0 | 0 | Black | 0 | Off |
1 | 11 | Light cyan | 2 | High-intensity |
2 | 13 | Light magenta | 2 | High-intensity |
3 | 15 | High-intensity white | 0 | Off white |
SCREEN Modes 2 and 11 | ||||
0 | 0 | Black | 0 | Off |
1 | 15 | High-intensity white | 0 | Off white |
(a) EGA color numbers. VGA and MCGA use display-color values that produce visually equivalent colors.
(b) For VGA or EGA with video memory > 64K.
(c) Only for mode 0.
(d) Off when used for background.
(e) EGA with video memory <= 64K.
See also COLOR PALETTE, PALETTE USING SCREEN Screen Modes
COMMON [SHARED] variablelist
Unless it has been declared as a static array in a preceding DIM statement, an array variable in a COMMON statement is a
dynamic array.
Its dimensions must be set in a later DIM or REDIM statement.
See also CHAIN DIM, REDIM FUNCTION SHARED, STATIC SUB
DATE$
DATE$ = stringexpression$
The DATE$ function returns a string in the form mm-dd-yyyy.
Example:
PRINT DATE$ DATE$ = "01-01-90" 'Note: The new system date remains in effect until ' you change it again. PRINT "Date set to "; DATE$
See also TIME$
KEY n%, CHR$(keyboardflag%) + CHR$(scancode%)
Value | Key |
---|---|
0 | No keyboard flag |
1 through 3 | Either Shift key |
4 | Ctrl key |
8 | Alt key |
32 | NumLock key |
64 | Caps Lock key |
128 | Extended keys on a 101-key keyboard |
See also KEY (Assignment) KEY, ON KEY (Event Trapping)
DEF FNname[(parameterlist)] = expression
DEF FNname[(parameterlist)]
[statementblock]
FNname = expression
[statementblock]
EXIT DEF]
[statementblock]
END DEF
The FUNCTION statement provides a better way to define a function.
See also EXIT FUNCTION SHARED, STATIC
DEF SEG [=address]
Example:
DEF SEG = 0 status% = PEEK(&H417) 'Read keyboard status. POKE &H417, (status% XOR &H40) 'Change Caps Lock state, bit 6.
See also BSAVE, BLOAD CALL ABSOLUTE PEEK, POKE
DRAW commandstring$
Line-drawing and cursor-movement commands | |
---|---|
D[n%] | Moves cursor down n% units. |
E[n%] | Moves cursor up and right n% units. |
F[n%] | Moves cursor down and right n% units. |
G[n%] | Moves cursor down and left n% units. |
H[n%] | Moves cursor up and left n% units. |
L[n%] | Moves cursor left n% units. |
M[{+|-}]x%,y% | Moves cursor to point x%,y%. If x% is preceded by + or -, moves relative to the current point. |
R[n%] | Moves cursor right n% units. |
U[n%] | Moves cursor up n% units. |
[B] | Optional prefix that moves cursor without drawing. |
[N] | Optional prefix that draws and returns cursor to its original position. |
Color, rotation, and scale commands | |
An% | Rotates an object n% * 90 degrees (n% can be 0, 1, 2, or 3). |
Cn% | Sets the drawing color (n% is a color attribute). |
Pn1%,n2% | Sets the paint fill and border colors of an object (n1% is the fill-color attribute, n2% is the border-color attribute). |
Sn% | Determines the drawing scale by setting the length of a unit of cursor movement. The default n% is 4, which is equivalent to 1 pixel. |
TAn% | Turns an angle n% degrees (-360 through 360). |
If you omit n% from line-drawing and cursor-movement commands, the cursor moves 1 unit.
To execute a DRAW command substring from a DRAW command string, use the "X" command:
DRAW "X" + VARPTR$(commandstring$)
Example:
'This example requires a color graphics adapter. SCREEN 1 Triangle$ = "F60 L120 E60" DRAW "C2 X" + VARPTR$(Triangle$) DRAW "BD30 P1,2 C3 M-30,-30"
See also PALETTE, PALETTE USING SCREEN VARPTR$ Color Attributes and Values
FIELD [#]filenumber%, fieldwidth% AS stringvariable$ [,fieldwidth% AS stringvariable$] ...
Record variables usually provide a better way to handle record data.
Example:
OPEN "FILEDAT.DAT" FOR RANDOM AS #1 LEN = 80 FIELD #1, 30 AS name$, 50 AS address$
See also GET, PUT LSET, RSET TYPE
Characters that format a numeric expression | |
---|---|
# | Digit position. |
. | Decimal point position. |
, | Placed left of the decimal point, prints a comma every third digit. |
+ | Position of number sign. |
^^^^ | Prints in exponential format. |
- | Placed after digit, prints trailing sign for negative numbers. |
$$ | Prints leading $. |
** | Fills leading spaces with *. |
**$ | Combines ** and $$. |
Characters used to format a string expression | |
& | Prints entire string. |
! | Prints only the first character of the string. |
\ \ | Prints first n characters, where n is the number of blanks between slashes + 2. |
Characters used to print literal characters | |
_ | literal. |
Key | Scan Code | ASCII | Shift | Ctrl | Alt | Num | Caps | Shift Caps | Shift Num |
---|---|---|---|---|---|---|---|---|---|
Esc | 01 | 1B | 1B | 1B | - | 1B | 1B | 1B | 1B |
1 ! | 02 | 31 | 21 | 7800 | 31 | 31 | 31 | 31 | |
2 @ | 03 | 32 | 40 | 0300 | 7900 | 32 | 32 | 32 | 32 |
3 # | 04 | 33 | 23 | - | 7A00 | 33 | 33 | 33 | 33 |
4 $ | 05 | 34 | 24 | - | 7B00 | 34 | 34 | 34 | 34 |
5 % | 06 | 35 | 25 | - | 7C00 | 35 | 35 | 35 | 35 |
6 ^ | 07 | 36 | 5E | 1E | 7D00 | 36 | 36 | 36 | 36 |
7 & | 08 | 37 | 26 | - | 7E00 | 37 | 37 | 37 | 37 |
8 * | 09 | 38 | 2A | - | 7F00 | 38 | 38 | 38 | 38 |
9 ( | 0A | 39 | 28 | - | 8000 | 39 | 39 | 39 | 39 |
0 ) | 0B | 30 | 29 | - | 8100 | 30 | 30 | 30 | 30 |
- _ | 0C | 2D | 5F | 1F | 8200 | 2D | 2D | 5F | 5F |
= + | 0D | 3D | 2B | - | 8300 | 3D | 3D | 2B | 2B |
Bksp | 0E | 08 | 08 | 7F | - | 08 | 08 | 08 | 08 |
Tab | 0F | 09 | 0F00 | - | 09 | 09 | 0F00 | 0F00 | |
Q | 10 | 71 | 51 | 11 | 1000 | 71 | 51 | 71 | 51 |
W | 11 | 77 | 57 | 17 | 1100 | 77 | 57 | 77 | 57 |
E | 12 | 65 | 45 | 05 | 1200 | 65 | 45 | 65 | 45 |
R | 13 | 72 | 52 | 12 | 1300 | 72 | 52 | 72 | 52 |
T | 14 | 74 | 54 | 14 | 1400 | 74 | 54 | 74 | 54 |
Y | 15 | 79 | 59 | 19 | 1500 | 79 | 59 | 79 | 59 |
U | 16 | 75 | 55 | 15 | 1600 | 75 | 55 | 75 | 55 |
I | 17 | 69 | 49 | 09 | 1700 | 69 | 49 | 69 | 49 |
O | 18 | 6F | 4F | 0F | 1800 | 6F | 4F | 6F | 4F |
P | 19 | 70 | 50 | 10 | 1900 | 70 | 50 | 70 | 50 |
[ { | 1A | 5B | 7B | 1B | - | 5B | 5B | 7B | 7B |
] } | 1B | 5D | 7D | 1D | - | 5D | 5D | 7D | 7D |
enter | 1C | 0D | 0D | 0A | - | 0D | 0D | 0A | 0A |
ctrl | 1D | - | - | - | - | - | - | - | - |
A | 1E | 61 | 41 | 01 | 1E00 | 61 | 41 | 61 | 41 |
S | 1F | 73 | 53 | 13 | 1F00 | 73 | 53 | 73 | 53 |
D | 20 | 64 | 44 | 04 | 2000 | 64 | 44 | 64 | 44 |
F | 21 | 66 | 46 | 06 | 2100 | 66 | 46 | 66 | 46 |
G | 22 | 67 | 47 | 07 | 2200 | 67 | 47 | 67 | 47 |
H | 23 | 68 | 48 | 08 | 2300 | 68 | 48 | 68 | 48 |
J | 24 | 6A | 4A | 0A | 2400 | 6A | 4A | 6A | 4A |
K | 25 | 6B | 4B | 0B | 2500 | 6B | 4B | 6B | 4B |
L | 26 | 6C | 4C | 0C | 2600 | 6C | 4C | 6C | 4C |
; : | 27 | 3B | 3A | - | - | 3B | 3B | 3A | 3A |
' " | 28 | 27 | 22 | - | - | 27 | 27 | 22 | 22 |
` ~ | 29 | 60 | 7E | - | - | 60 | 60 | 7E | 7E |
Lshift | 2A | - | - | - | - | - | - | - | - |
\ | | 2B | 5C | 7C | 1C | - | 5C | 5C | 7C | 7C |
Z | 2C | 7A | 5A | 1A | 2C00 | 7A | 5A | 7A | 5A |
X | 2D | 78 | 58 | 18 | 2D00 | 78 | 58 | 78 | 58 |
C | 2E | 63 | 43 | 03 | 2E00 | 63 | 43 | 63 | 43 |
V | 2F | 76 | 56 | 16 | 2F00 | 76 | 56 | 76 | 56 |
B | 30 | 62 | 42 | 02 | 3000 | 62 | 42 | 62 | 42 |
N | 31 | 6E | 4E | 0E | 3100 | 6E | 4E | 6E | 4E |
M | 32 | 6D | 4D | 0D | 3200 | 6D | 4D | 6D | 4D |
, < | 33 | 2C | 3C | - | - | 2C | 2C | 3C | 3C |
. > | 34 | 2E | 3E | - | - | 2E | 2E | 3E | 3E |
/ ? | 35 | 2F | 3F | - | - | 2F | 2F | 3F | 3F |
Rshift | 36 | - | - | - | - | - | - | - | - |
* PrtSc | 37 | 2A | INT 5 | 10 | - | 2A | 2A | INT 5 | INT 5 |
alt | 38 | - | - | - | - | - | - | - | - |
space | 39 | 20 | 20 | 20 | - | 20 | 20 | 20 | 20 |
caps | 3A | - | - | - | - | - | - | - | - |
F1 | 3B | 3B00 | 5400 | 5E00 | 6800 | 3B00 | 3B00 | 5400 | 5400 |
F2 | 3C | 3C00 | 5500 | 5F00 | 6900 | 3C00 | 3C00 | 5500 | 5500 |
F3 | 3D | 3D00 | 5600 | 6000 | 6A00 | 3D00 | 3D00 | 5600 | 5600 |
F4 | 3E | 3E00 | 5700 | 6100 | 6B00 | 3E00 | 3E00 | 5700 | 5700 |
F5 | 3F | 3F00 | 5800 | 6200 | 6C00 | 3F00 | 3F00 | 5800 | 5800 |
F6 | 40 | 4000 | 5900 | 6300 | 6D00 | 4000 | 4000 | 5900 | 5900 |
F7 | 41 | 4100 | 5A00 | 6400 | 6E00 | 4100 | 4100 | 5A00 | 5A00 |
F8 | 42 | 4200 | 5B00 | 6500 | 6F00 | 4200 | 4200 | 5B00 | 5B00 |
F9 | 43 | 4300 | 5C00 | 6600 | 7000 | 4300 | 4300 | 5C00 | 5C00 |
F10 | 44 | 4400 | 5D00 | 6700 | 7100 | 4400 | 4400 | 5D00 | 5D00 |
num | 45 | - | - | - | - | - | - | - | - |
scrl | 46 | - | - | - | - | - | - | - | - |
home | 47 | 4700 | 37 | 7700 | - | 37 | 4700 | 37 | 4700 |
up | 48 | 4800 | 38 | - | - | 38 | 4800 | 38 | 4800 |
pgup | 49 | 4900 | 39 | 8400 | - | 39 | 4900 | 39 | 4900 |
- (kpd) | 4A | 2D | 2D | - | - | 2D | 2D | 2D | 2D |
left | 4B | 4B00 | 34 | 7300 | - | 34 | 4B00 | 34 | 4B00 |
center | 4C | 4C00 | 35 | - | - | 35 | 4C00 | 35 | 4C00 |
right | 4D | 4D00 | 36 | 7400 | - | 36 | 4D00 | 36 | 4D00 |
+ (kpd) | 4E | 2B | 2B | - | - | 2B | 2B | 2B | 2B |
end | 4F | 4F00 | 31 | 7500 | - | 31 | 4F00 | 31 | 4F00 |
down | 50 | 5000 | 32 | - | - | 32 | 5000 | 32 | 5000 |
pgdn | 51 | 5100 | 33 | 7600 | - | 33 | 5100 | 33 | 5100 |
ins | 52 | 5200 | 30 | - | - | 30 | 5200 | 30 | 5200 |
del | 53 | 5300 | 2E | - | - | 2E | 5300 | 2E | 5300 |
Key | Scan Code | ASCII | Shift | Ctrl | Alt | Num | Caps | Shift Caps | Shift Num |
LBOUND(array[,dimension%])
UBOUND(array[,dimension%])
Example:
DIM a%(1 TO 3, 2 TO 7) PRINT LBOUND(a%, 1), UBOUND(a%, 2)
See also DIM, REDIM
ACCESS {READ | WRITE | READ WRITE}
See also OPEN
OPEN mode2$,[#]filenum%,file$[,reclen%]
QBasic supports this syntax for compatibility with programs written in earlier versions of BASIC.
See also OPEN
See also OPEN OPEN COM PRINT, LPRINT WRITE
PEN(n%)
n% | Returns |
---|---|
0 | Whether pen was used since last call (-1 if yes, 0 if no) |
1 | The x screen coordinate of the last pen press |
2 | The y screen coordinate of the last pen press |
3 | The current pen switch status (-1 if down, 0 if up) |
4 | The x screen coordinate where the pen last left the screen |
5 | The y screen coordinate where the pen last left the screen |
6 | The character row of the last pen press |
7 | The character column of the last pen press |
8 | The character row where the pen last left the screen |
9 | The character column where the pen last left the screen |
Example:
DO P = PEN(3) LOCATE 1, 1: PRINT "Pen is "; IF P THEN PRINT "down" ELSE PRINT "up " PRINT "X ="; PEN(4), " Y ="; PEN(5); " " LOOP
See also PEN, ON PEN Statements SCREEN Screen Modes
PLAY (n)
Example:
Music$ = "MBT180o2P2P8L8GGGL2E-P24P8L8FFFL2D" PLAY Music$ WHILE PLAY(0) > 5: WEND PRINT "Just about done!"
See also PLAY (Music) PLAY, ON PLAY (Event Trapping)
RESET
Code | Message | Code | Message |
---|---|---|---|
1 | NEXT without FOR | 37 | Argument-count mismatch |
2 | Syntax error | 38 | Array not defined |
3 | RETURN without GOSUB | 40 | Variable required |
4 | Out of DATA | 50 | FIELD overflow |
5 | Illegal function call | 51 | Internal error |
6 | Overflow | 52 | Bad file name or number |
7 | Out of memory | 53 | File not found |
8 | Label not defined | 54 | Bad file mode |
9 | Subscript out of range | 55 | File already open |
10 | Duplicate definition | 56 | FIELD statement active |
11 | Division by zero | 57 | Device I/O error |
12 | Illegal in direct mode | 58 | File already exists |
13 | Type mismatch | 59 | Bad record length |
14 | Out of string space | 61 | Disk full |
16 | String formula too complex | 62 | Input past end of file |
17 | Cannot continue | 63 | Bad record number |
18 | Function not defined | 64 | Bad file name |
19 | No RESUME | 67 | Too many files |
20 | RESUME without error | 68 | Device unavailable |
24 | Device timeout | 69 | Communication-buffer overflow |
25 | Device fault | 70 | Permission denied |
26 | FOR without NEXT | 71 | Disk not ready |
27 | Out of paper | 72 | Disk-media error |
29 | WHILE without WEND | 73 | Feature unavailable |
30 | WEND without WHILE | 74 | Rename across disks |
33 | Duplicate label | 75 | Path/File access error |
35 | Subprogram not defined | 76 | Path not found |
Use bits-per-pixel-per-plane and planes values to determine the required size of the array that holds a graphics screen image. Bits-per-pixel-per-plane and planes values, along with the horizontal resolution, also determine which screen modes are compatibile:
Screen mode | Bits-per-pixel -per-plane | Planes | Horizontal resolution (in pixels) |
---|---|---|---|
1 | 2 | 1 | 320 |
2, 4, 11 | 1 | 1 | 640 |
3 | 1 | 1 | 720 |
7 | 1 | 4 | 320 |
8, 9(> 64K video memory), 12 | 1 | 4 | 640 |
9(64K video memory), 10 | 1 | 2 | 640 |
13 | 8 | 1 | 320 |
The following formula gives the required size, in bytes, of an array used to hold a captured image:
size% = 4 + INT(((PMAP (x2!, 0) - PMAP (x1!, 0) + 1) * (bits-per-pixel-per-plane%) + 7) / 8) * planes% * (PMAP (y2!, 1) - PMAP (y1!, 1) + 1)
GET and PUT operations are compatible in screen modes with the same horizontal resolution and bits-per-pixel-per-plane and planes values. For example, screen modes 2, 4, and 11 are compatible, and screen modes 8 and 12 are compatible.
See also SCREEN Screen Modes
SCREEN 0: Text mode only |
---|
|
SCREEN 1: 320 x 200 graphics |
|
SCREEN 2: 640 x 200 graphics |
|
SCREEN 3: Hercules adapter required, monochrome monitor only |
|
SCREEN 4: |
|
SCREEN 7: 320 x 200 graphics |
|
SCREEN 8: 640 x 200 graphics |
|
SCREEN 9: 640 x 350 graphics |
|
SCREEN 10: 640 x 350 graphics, monochrome monitor only |
|
SCREEN 11 (VGA or MCGA) |
|
SCREEN 12 (VGA) |
|
SCREEN 13 (VGA or MCGA) |
|
See also SCREEN Statement
SOUND frequency, duration
Example:
FOR i% = 440 TO 1000 STEP 5 SOUND i%, i% / 1000 NEXT i%
See also PLAY
{REM | '} $STATIC
{REM | '} $DYNAMIC
DIM and REDIM usually provide a better way to specify whether arrays are dynamic or static.
See also DIM, REDIM REM SHARED, STATIC
STICK(n%)
n% | Returns |
---|---|
0 | x coordinate of joystick A |
1 | y coordinate of joystick A |
2 | x coordinate of joystick B |
3 | y coordinate of joystick B |
You must call STICK(0) before STICK(1), STICK (2), or STICK(3).
STICK(0) records the current coordinates.
Example:
Temp% = STICK(0) PRINT STICK(2), STICK(3)
See also STRIG Function STRIG, ON STRIG Statements
STOP
The STOP keyword also suspends trapping of events in these statements: COM, ON COM KEY, ON KEY PEN, ON PEN PLAY, ON PLAY STRIG, ON STRIG TIMER, ON TIMER
Example:
FOR i% = 1 TO 10 PRINT i% IF i% = 5 THEN STOP 'STOP pauses; F5 Continues. NEXT i%
STRIG(n%)
n% | Condition |
---|---|
0 | Lower joystick A trigger was pressed since last STRIG(0) |
1 | Lower joystick A trigger is currently pressed |
2 | Lower joystick B trigger was pressed since last STRIG(2) |
3 | Lower joystick B trigger is currently pressed |
4 | Upper joystick A trigger was pressed since last STRIG(4) |
5 | Upper joystick A trigger is currently pressed |
6 | Upper joystick B trigger was pressed since last STRIG(6) |
7 | Upper joystick B trigger is currently pressed |
STRIG returns -1 if the condition is true, 0 otherwise.
Example:
PRINT "Press Esc to exit." DO IF STRIG(0) OR INKEY$ = CHR$(27) THEN EXIT DO LOOP DO BEEP 'BEEP while trigger A is pressed. LOOP WHILE STRIG(1)
See also STICK STRIG, ON STRIG Statements
TIMER
Use TIMER to time programs or parts of programs, or with the RANDOMIZE statement to seed the random-number generator.
Example:
RANDOMIZE TIMER
See also RANDOMIZE, RND TIMER, ON TIMER Statements
VARPTR(variablename)
VARSEG(variablename)
See also CALL ABSOLUTE DEF SEG PEEK, POKE VARPTR$
WHILE condition
.
.
.
WEND
DO...LOOP provides a better way to execute statements in a program loop.
See also DO...LOOP FOR...NEXT
'Illustrates ERDEV, ERDEV$, ERL, ERR, ERROR, ON ERROR, and RESUME. ON ERROR GOTO Handler 10 CHDIR "a:\" 'Causes ERR 71 "Disk not ready" 'if no disk in Drive A. 20 y% = 0 30 x% = 5 / y% 'ERR 11 "Division by zero." 40 PRINT "x% ="; x% 50 ERROR 57 'ERR 57 "Device I/O error." Handler: PRINT PRINT "Error "; ERR; " on line "; ERL SELECT CASE ERR CASE 71 PRINT "Using device "; ERDEV$; " device error code = "; ERDEV RESUME NEXT CASE 11 INPUT "What value do you want to divide by"; y% RESUME 'Retry line 30 with new value of y%. CASE ELSE PRINT "Unexpected error, ending program." END END SELECT
See also COMMON DECLARE DEF FN DIM, REDIM FIELD FUNCTION NAME OPEN SHARED, STATIC SUB TYPE
Data-Type Suffixes |
---|
! Single-precision # Double-precision $ String % Integer & Long-integer |
Mathematical Operators |
* Multiplication symbol - Minus sign / Division symbol (slash) = Relational operator or assignment symbol > Greater than + Plus sign . Decimal point < Less than \ Integer division symbol (backslash) ^ Exponentiation symbol (up arrow or caret) |
Special |
' Comment line (single quote) ; Controls PRINT and INPUT statement output , Controls PRINT and INPUT statement output : Separates multiple statements on a single line ? INPUT statement prompt _ Line continuation underscore (reserved for compatibility with other versions of Basic but not supported by QBasic) |
BSAVE filespec$, offset%, length&
BLOAD filespec$[,offset%]
The starting address of the memory area saved or loaded is determined by the offset and the most recent DEF SEG statement.
See also DEF SEG VARPTR, VARSEG
See also AS Basic Character Set COMMON DECLARE DEF FN DIM, REDIM FUNCTION SHARED, STATIC SUB TYPE
DEFINT letterrange [,letterrange]...
DEFLNG letterrange [,letterrange]...
DEFSNG letterrange [,letterrange]...
DEFDBL letterrange [,letterrange]...
DEFSTR letterrange [,letterrange]...
Statement | Default Data Type |
---|---|
DEFINT | Integer |
DEFLNG | Long integer |
DEFSNG | Single precision |
DEFDBL | Double precision |
DEFSTR | String |
A data-type suffix (%, &, !, #, or $) always takes precedence over a DEFtype statement.
Single-precision is the default data type if you do not specify a DEFtype statement.
After you specify a DEFtype statement in your program, QBasic automatically inserts a corresponding DEFtype statement in each procedure you create.
Example:
DEFDBL A-Z a = SQR(3) PRINT "Square root of 3 = "; a
ENVIRON$ (env-variable$)
ENVIRON$ (n%)
ENVIRON stringexpression$
Changes made by the ENVIRON statement are erased when the program ends.
Example:
ENVIRON "PATH=TEST" PRINT ENVIRON$("PATH")
VARPTR$(commandstring$)
Example:
Scale$ = "CDEFGAB" PLAY "L16" FOR i% = 0 TO 6 PLAY "O" + STR$(i%) PLAY "X" + VARPTR$(Scale$) NEXT i%
See also DRAW PLAY (Music) VARPTR, VARSEG
See also PALETTE, PALETTE USING PRINT USING, LPRINT USING
TRON
TROFF
QBasic's debugging features make these statements unnecessary.
See also Run and Debug Keys
Start program execution from beginning | Shift+F5 |
Continue program execution from current statement | F5 |
Execute program to current cursor position | F7 |
Execute next program statement as a single step | F8 |
Single step, tracing around procedure calls | F10 |
Set or clear a breakpoint | F9 |
See also DIM, REDIM FOR...NEXT LOCK, UNLOCK SELECT CASE
See also CIRCLE FOR...NEXT GET, PUT LINE PAINT PRESET, PSET
SLEEP [seconds&]
If seconds& is 0 or is omitted, the program is suspended until a key is pressed or a trapped event occurs.
Example:
PRINT "Taking a 10-second nap..." SLEEP 10 PRINT "Wake up!"
See also WAIT
See also COM, ON COM KEY, ON KEY KEY (Assignment) PEN, ON PEN PLAY, ON PLAY STRIG, ON STRIG TIMER, ON TIMER
numeric-expression1 MOD numeric-expression2
Example:
PRINT 19 MOD 6.7 'QBasic rounds 6.7 to 7, then divides. 'Output is: 5
MKI$(integer-expression%)
MKL$(long-integer-expression&)
MKS$(single-precision-expression!)
MKD$(double-precision-expression#)
CVI(2-byte-numeric-string)
CVL(4-byte-numeric-string)
CVS(4-byte-numeric-string)
CVD(8-byte-numeric-string)
Function | Returns | Function | Returns |
---|---|---|---|
MKI$ | A 2-byte string | CVI | An integer |
MKL$ | A 4-byte string | CVL | A long integer |
MKS$ | A 4-byte string | CVS | A single-precision number |
MKD$ | An 8-byte string | CVD | A double-precision number |
See also FIELD MKSMBF$, MKDMBF$, CVSMBF, CVDMBF
[LET] variable=expression
Use of the optional LET keyword is not recommended. The variable=expression assignment statement performs the same action with or without LET.
See also LSET, RSET
IOCTL [#]filenumber%, string$
IOCTL$([#]filenumber%)
IOCTL control strings and the information returned by IOCTL$ depend on the device driver. See your device-driver documentation for information about IOCTL control strings and what is returned by IOCTL$.