Martes, Enero 22, 2013

Syntax in BASIC Programming Language


The Basic Programming Language

Typical BASIC keywords

Data manipulation
  • LET: assigns a value (which may be the result of an expression) to a variable.
  • DATA: holds a list of values which are assigned sequentially using the READ command.
Program flow control
  • IF ... THEN ... ELSE: used to perform comparisons or make decisions.
  • FOR ... TO ... {STEP} ... NEXT: repeat a section of code a given number of times. A variable that acts as a counter is available within the loop.
  • WHILE ... WEND and REPEAT ... UNTIL: repeat a section of code while the specified condition is true. The condition may be evaluated before each iteration of the loop, or after.
  • DO ... LOOP {WHILE} or {UNTIL}: repeat a section of code Forever or While/Until the specified condition is true . The condition may be evaluated before each iteration of the loop, or after.
  • GOTO: jumps to a numbered or labelled line in the program.
  • GOSUB: temporarily jumps to a numbered or labelled line, returning to the following line after encountering the RETURN Command. This is used to implement subroutines.
  • ON ... GOTO/GOSUB: chooses where to jump based on the specified conditions. See Switch statement for other forms.
Input and output
  • PRINT: displays a message on the screen or other output device.
  • INPUT: asks the user to enter the value of a variable. The statement may include a prompt message.
  • TAB or AT: sets the position where the next character will be shown on the screen or printed on paper.
Miscellaneous
  • REM: 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.
  • USR: transfers program control to a machine language subroutine, usually entered as an alphanumeric string or in a list of DATA statements.

Data types and variables

Minimal versions of BASIC had only integer variables and one-letter variable names. More powerful versions had floating-point arithmetic, and variables could be labelled with names six or more characters long.
String variables are usually distinguished in many microcomputer dialects by having $ suffixed to their name, and string values are typically enclosed in quotation marks.
Arrays in BASIC could contain integers, floating point or string variables.
Some dialects of BASIC supported matrices and matrix operations, useful for the solution of sets of simultaneous linear algebraic equations. These dialects would support matrix operations such as assignment, addition, multiplication (of compatible matrix types), and evaluation of a determinant. Microcomputer dialects often lacked this data type and required a programmer to provide subroutines to carry out equvalent operations.

Examples

The original Dartmouth Basic was unusual in having a matrix keyword, MAT. Although dropped by most later microprocessor derivatives it is used in this example from the 1968 manual which averages the numbers that are input:
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
New BASIC programmers on a home computer might start with a simple program similar to the Hello world program made famous by Kernighan and Ritchie.
10 PRINT "Hello, World!"
140 END
This generally involves simple use of the language's PRINT statement to display the message (such as the programmer's name) to the screen. Often an infinite loop was used to fill the display with the message.
Most first generation BASIC languages 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:
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
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
Second generation BASICs (for example QuickBASIC and PowerBASIC) introduced a number of features into the language, primarily related to structured and procedure-oriented programming. Usually, line numbering is omitted from the language and replaced with labels (for GOTO) and procedures to encourage easier and more flexible design.
INPUT "What is your name: ", UserName$
PRINT "Hello "; UserName$
DO
  INPUT "How many stars do you want: ", NumStars
  Stars$ = STRING$(NumStars, "*")
  PRINT Stars$
  DO
    INPUT "Do you want more stars? ", Answer$
  LOOP UNTIL Answer$ <> ""
  Answer$ = LEFT$(Answer$, 1)
LOOP WHILE UCASE$(Answer$) = "Y"
PRINT "Goodbye "; UserName$
Third generation BASIC dialects such as Visual Basic, REALbasic, StarOffice Basic and BlitzMax 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.
The following example is in Visual Basic .NET:
Public Class StarsProgram
    Public Shared Sub Main()
        Dim UserName, Answer, stars As String, NumStars As Integer
        Console.Write("What is your name: ")
        UserName = Console.ReadLine()
        Console.WriteLine("Hello {0}", UserName)
        Do
            Console.Write("How many stars do you want: ")
            NumStars = CInt(Console.ReadLine())
            stars = New String("*", NumStars)
            Console.WriteLine(stars)
            Do
                Console.Write("Do you want more stars? ")
                Answer = Console.ReadLine()
            Loop Until Answer <> ""
            Answer = Answer.Substring(0, 1)
        Loop While Answer.ToUpper() = "Y"
        Console.WriteLine("Goodbye {0}", UserName)
    End Sub
End Class

Walang komento:

Mag-post ng isang Komento