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!
=== 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)