Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
BASIC
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Syntax == === Typical BASIC keywords{{Anchor|keywords}} === ==== Data manipulation ==== ; <code>LET</code> : assigns a value (which may be the result of an [[expression (programming)|expression]]) to a variable. In most dialects of BASIC, <code>LET</code> is optional, and a line with no other identifiable keyword will assume the keyword to be <code>LET</code>. ; <code>DATA</code> : holds a list of values which are assigned sequentially using the READ command. ; <code>READ</code> : reads a value from a <code>DATA</code> statement and assigns it to a variable. An internal pointer keeps track of the last <code>DATA</code> element that was read and moves it one position forward with each <code>READ</code>. Most dialects allow multiple variables as parameters, reading several values in a single operation. ; <code>RESTORE</code> : resets the internal pointer to the first <code>DATA</code> statement, allowing the program to begin <code>READ</code>ing from the first value. Many dialects allow an optional line number or ordinal value to allow the pointer to be reset to a selected location. ; <code>DIM</code> : Sets up an array. ==== Program flow control ==== ; <code>IF ... THEN ... {ELSE}</code> : used to perform comparisons or make decisions. Early dialects only allowed a line number after the <code>THEN</code>, but later versions allowed any valid statement to follow. <code>ELSE</code> was not widely supported, especially in earlier versions. ; <code>FOR ... TO ... {STEP} ... NEXT</code> : repeat a section of code a given number of times. A variable that acts as a counter, the "index", is available within the [[Control flow#Loops|loop]]. ; <code>WHILE ... WEND</code> and <code>REPEAT ... UNTIL</code> : repeat a section of code while the specified condition is true. The condition may be evaluated before each iteration of the loop, or after. Both of these commands are found mostly in later dialects. ; <code>DO ... LOOP {WHILE}</code> or <code>{UNTIL}</code> : repeat a section of code indefinitely or while/until the specified condition is true. The condition may be evaluated before each iteration of the loop, or after. Similar to <code>WHILE</code>, these keywords are mostly found in later dialects. ; <code>[[Goto|GOTO]]</code> : jumps to a numbered or labelled line in the program. Most dialects also allowed the form {{code|GO TO}}. ; <code>GOSUB ... RETURN</code> : jumps to a numbered or labelled line, executes the code it finds there until it reaches a <code>RETURN</code> command, on which it jumps back to the statement following the <code>GOSUB</code>, either after a colon, or on the next line. This is used to implement [[subroutine]]s. ; <code>ON ... GOTO/GOSUB</code> : chooses where to jump based on the specified conditions. See [[Switch statement]] for other forms. ; <code>DEF FN</code> : a pair of keywords introduced in the early 1960s to define functions. The original BASIC functions were modelled on FORTRAN single-line functions. BASIC functions were one expression with variable arguments, rather than [[subroutine]]s, with a syntax on the model of <code>DEF FND(x) = x*x</code> at the beginning of a program. Function names were originally restricted to FN, plus one letter, ''i.e.'', FNA, FNB ... ==== Input and output ==== ; <code>LIST</code> : displays the full source code of the current program. ; {{anchor|PRINT}}<code>PRINT</code> : displays a message on the screen or other output device. ; <code>INPUT</code> : asks the user to enter the value of a variable. The statement may include a prompt message. ; <code>TAB</code> : used with <code>PRINT</code> to set the position where the next character will be shown on the screen or printed on paper. <code>AT</code> is an alternative form. ; <code>SPC</code> : prints out a number of space characters. Similar in concept to <code>TAB</code> but moves by a number of additional spaces from the current column rather than moving to a specified column. ==== Mathematical functions ==== ; <code>ABS</code> : Absolute value ; <code>ATN</code> : Arctangent (result in [[radian]]s) ; <code>COS</code> : Cosine (argument in [[radian]]s) ; <code>EXP</code> : Exponential function ; <code>INT</code> : Integer part (typically [[floor function]]) ; <code>LOG</code> : Natural logarithm ; <code>RND</code> : [[Random number generation]] ; <code>SIN</code> : Sine (argument in [[radian]]s) ; <code>SQR</code> : Square root ; <code>TAN</code> : Tangent (argument in [[radian]]s) ==== Miscellaneous ==== ; <code>REM</code> : holds a programmer's comment or REMark; often used to give a title to the program and to help identify the purpose of a given section of code. ;{{anchor|USR}} <code>USR</code> ("User Serviceable Routine"): transfers program control to a [[Machine code|machine language]] subroutine, usually entered as an alphanumeric [[String (computer science)|string]] or in a list of DATA statements. ; <code>CALL</code> : alternative form of <code>USR</code> found in some dialects. Does not require an artificial parameter to complete the function-like syntax of <code>USR</code>, and has a clearly defined method of calling different routines in memory. ; <code>TRON</code> / <code>TROFF</code>: turns on display of each line number as it is run ("TRace ON"). This was useful for [[debugging]] or correcting of problems in a program. TROFF turns it back off again. ; <code>ASM</code> : some compilers such as Freebasic,<ref>{{Cite web|url=http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgAsm|title=KeyPgAsm|website=FreeBasic Wiki|access-date=August 2, 2017}}</ref> Purebasic,<ref>{{Cite web|url=https://www.purebasic.com/documentation/reference/inlinedasm.html|title=Inline x86 ASM|website=Pure Basic|access-date=August 2, 2017}}</ref> and Powerbasic<ref>{{Cite web|url=https://www.powerbasic.com/help/pbcc/using_assembly-language_in_your_code.htm|title=Using assembly-language in your code|website=Power Basic|access-date=August 2, 2017|archive-url=https://web.archive.org/web/20170802164032/https://www.powerbasic.com/help/pbcc/using_assembly-language_in_your_code.htm|archive-date=August 2, 2017|url-status=dead}}</ref> also support [[Inline assembler|inline assembly]] language, allowing the programmer to intermix high-level and low-level code, typically prefixed with "ASM" or "!" statements. === Data types and variables === Minimal versions of BASIC had only integer variables and one- or two-letter variable names, which minimized requirements of limited and expensive memory (RAM). More powerful versions had floating-point arithmetic, and variables could be labelled with names six or more characters long. There were some problems and restrictions in early implementations; for example, Applesoft BASIC allowed variable names to be several characters long, but only the first two were significant, thus it was possible to inadvertently write a program with variables "LOSS" and "LOAN", which would be treated as being the same; assigning a value to "LOAN" would silently overwrite the value intended as "LOSS". Keywords could not be used in variables in many early BASICs; "SCORE" would be interpreted as "SC" OR "E", where OR was a keyword. [[String (computer science)|String]] variables are usually distinguished in many microcomputer dialects by having $ suffixed to their name as a [[Sigil (computer programming)|sigil]], and values are often identified as strings by being delimited by "double quotation marks". Arrays in BASIC could contain integers, floating point or string variables. Some dialects of BASIC supported [[matrix (mathematics)|matrices and matrix operations]], which can be used to solve sets of simultaneous linear algebraic equations. These dialects would directly support matrix operations such as assignment, addition, multiplication (of compatible matrix types), and evaluation of a determinant. Many microcomputer BASICs did not support this data type; matrix operations were still possible, but had to be programmed explicitly on array elements. === Examples === [[File:ARROW (Listing) (3300726999).png|thumb|A simple game implemented in BASIC]] ==== Unstructured BASIC ==== New BASIC programmers on a home computer might start with a simple program, perhaps using the language's PRINT statement to display a message on the screen; a well-known and often-replicated example is [[The C Programming Language|Kernighan and Ritchie]]'s [["Hello, World!" program]]: <syntaxhighlight lang="basic"> 10 PRINT "Hello, World!" 20 END </syntaxhighlight> An [[infinite loop]] could be used to fill the display with the message: <syntaxhighlight lang="basic"> 10 PRINT "Hello, World!" 20 GOTO 10 </syntaxhighlight> Note that the <code>END</code> statement is optional and has no action in most dialects of BASIC. It was not always included, as is the case in this example. This same program can be modified to print a fixed number of messages using the common <code>FOR...NEXT</code> statement: <syntaxhighlight lang="basic"> 10 LET N=10 20 FOR I=1 TO N 30 PRINT "Hello, World!" 40 NEXT I </syntaxhighlight> Most home computers BASIC versions, such as [[MSX BASIC]] and [[GW-BASIC]], supported simple data types, loop cycles, and arrays. The following example is written for GW-BASIC, but will work in most versions of BASIC with minimal changes: <syntaxhighlight lang="basic"> 10 INPUT "What is your name: "; U$ 20 PRINT "Hello "; U$ 30 INPUT "How many stars do you want: "; N 40 S$ = "" 50 FOR I = 1 TO N 60 S$ = S$ + "*" 70 NEXT I 80 PRINT S$ 90 INPUT "Do you want more stars? "; A$ 100 IF LEN(A$) = 0 THEN GOTO 90 110 A$ = LEFT$(A$, 1) 120 IF A$ = "Y" OR A$ = "y" THEN GOTO 30 130 PRINT "Goodbye "; U$ 140 END </syntaxhighlight> The resulting dialog might resemble: What is your name: Mike Hello Mike How many stars do you want: 7 ******* Do you want more stars? yes How many stars do you want: 3 *** Do you want more stars? no Goodbye Mike The original Dartmouth Basic was unusual in having a matrix keyword, MAT.{{efn|From version 3 onwards.}} Although not implemented by most later microprocessor derivatives, it is used in this example from the 1968 manual<ref>{{Cite book|url=http://bitsavers.trailing-edge.com/pdf/dartmouth/BASIC_4th_Edition_Jan68.pdf |archive-url=https://web.archive.org/web/20140103140704/http://bitsavers.trailing-edge.com/pdf/dartmouth/BASIC_4th_Edition_Jan68.pdf |archive-date=2014-01-03 |url-status=live|title=Basic: a manual for BASIC, the elementary algebraic language designed for use with the Dartmouth Time Sharing System|last1=Kemeny|first1=John G.|last2=Kurtz|first2=Thomas E.|date=January 1968|publisher=Dartmouth College Computation Center|location=Hanover, N.H.|language=en|edition=4th|page=53}}</ref> which averages the numbers that are input: <syntaxhighlight lang="basic"> 5 LET S = 0 10 MAT INPUT V 20 LET N = NUM 30 IF N = 0 THEN 99 40 FOR I = 1 TO N 45 LET S = S + V(I) 50 NEXT I 60 PRINT S/N 70 GO TO 5 99 END </syntaxhighlight> ==== Structured BASIC ==== Second-generation BASICs (for example, [[VSI BASIC for OpenVMS|VAX Basic]], [[SuperBASIC]], [[True BASIC]], [[QuickBASIC]], [[BBC BASIC]], [[Pick operating system|Pick BASIC]], [[PowerBASIC]], [[Liberty BASIC]], [[QB64]] and (arguably) [[COMAL]]) introduced a number of features into the language, primarily related to structured and procedure-oriented programming. Usually, [[line number]]ing is omitted from the language and replaced with [[label (computer science)|labels]] (for [[GOTO]]) and [[subroutine|procedures]] to encourage easier and more flexible design.<ref name="GBvsQB">{{cite web|url=http://support.microsoft.com/kb/73084|title=Differences Between GW-BASIC and QBasic|date=2003-05-12|access-date=2008-06-28|archive-url=https://web.archive.org/web/20131019181140/http://support.microsoft.com/kb/73084|archive-date=2013-10-19}}</ref> In addition keywords and structures to support repetition, selection and procedures with local variables were introduced. The following example is in Microsoft QuickBASIC: <syntaxhighlight lang="QBasic"> REM QuickBASIC example REM Forward declaration - allows the main code to call a REM subroutine that is defined later in the source code DECLARE SUB PrintSomeStars (StarCount!) REM Main program follows INPUT "What is your name: ", UserName$ PRINT "Hello "; UserName$ DO INPUT "How many stars do you want: ", NumStars CALL PrintSomeStars(NumStars) DO INPUT "Do you want more stars? ", Answer$ LOOP UNTIL Answer$ <> "" Answer$ = LEFT$(Answer$, 1) LOOP WHILE UCASE$(Answer$) = "Y" PRINT "Goodbye "; UserName$ END REM subroutine definition SUB PrintSomeStars (StarCount) REM This procedure uses a local variable called Stars$ Stars$ = STRING$(StarCount, "*") PRINT Stars$ END SUB </syntaxhighlight> ==== Object-oriented BASIC ==== Third-generation BASIC dialects such as [[Visual Basic (classic)|Visual Basic]], [[Xojo]], [[Gambas]], [[StarOffice Basic]], [[BlitzMax]] and [[PureBasic]] introduced features to support object-oriented and [[event-driven programming]] paradigm. Most built-in procedures and functions are now represented as ''methods'' of standard objects rather than ''operators''. Also, the [[operating system]] became increasingly accessible to the BASIC language. The following example is in [[Visual Basic .NET]]: <!-- needs a better (event-driven?) example --> <syntaxhighlight lang="vbnet"> Public Module StarsProgram Private Function Ask(prompt As String) As String Console.Write(prompt) Return Console.ReadLine() End Function Public Sub Main() Dim userName = Ask("What is your name: ") Console.WriteLine("Hello {0}", userName) Dim answer As String Do Dim numStars = CInt(Ask("How many stars do you want: ")) Dim stars As New String("*"c, numStars) Console.WriteLine(stars) Do answer = Ask("Do you want more stars? ") Loop Until answer <> "" Loop While answer.StartsWith("Y", StringComparison.OrdinalIgnoreCase) Console.WriteLine("Goodbye {0}", userName) End Sub End Module </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)