Vous êtes sur la page 1sur 6

Starting GW-BASIC

Muhammad Talha Ahmed Lodhi

History and Introduction


BASIC is an easy high-level computer programming language. It was developed in 1964 at Dartmouth College by Professor John G. Kemeny and Thomas E. Kurtz. Because of its simplicity and conversational nature, BASIC quickly became one of the most popular programming languages. It is the first HLL to be used with micro-computers. First interpreter of BASIC language was written in 1975 by Microsoft. At one time, there were many versions of BASIC. Most popular are GW-BASIC and QBASIC. BASIC has the following features common in all versions: It is an easy, student friendly language. It is a general purpose language, suitable for scientific and commercial programming. It has simple syntax rules and is easy to test and debug. Communication with computer is simple and straight forward with it.

Getting Started
Ok, now open BASIC, (if you dont have it, you can get at www.gwbasic.webs.com for free). The BASIC window will look something like this:

Now type in the following code: PRINT 27+32 Theres nothing hard to understand here, first we used the PRINT command, and this is probably the most useful command in basic as it is the only way to produce output in BASIC. When you press enter, basic prints 59 and then prints ok which means that the program has ended. And thats it, were making programs in BASIC! Result: 59 Ok_ Now try using double commas, retype the program like this: PRINT 27+32 Result: 27+32 Now we see that if we use commas, instead of calculating the data, BASIC prints it as it is, So lets put this all together: PRINT 27+32=;27+32 Theres nothing particularly hard to understand, the only new thing is the semicolon ;, which is used to separate numeric data from string data, in the same program if we use comma , instead of semicolon, BASIC would have printed it in indents (tabs). Try experimenting with PRINT commands and commas and semicolons.

Let Statement
Ok, so this is a rather interesting statement as it lets you assign a value to a data place known as variable. Its rather easy, as we just have to give the variable name, = sign, and then the value, for example, type the following code: X=5 This assigns the value 5 to the variable X, Now in the same way, assign value 10 to variable Y: Y=10 We can also store different variables in one variable for example: Z=X+Y Now lets test it, type in the following code: X=5 Y=10 Z=X+Y PRINT Z The result should look something like this: 15 Ok_ We can also assign string data to variables, which are alphanumeric data, we just have to add $ sign in front of variable, and add double quotation around the phrase, for example:

X=10 Y$=The sum of 2 and 8 is Print Y$;X The result should be like this: The sum of 2 and 8 is 10 Ok_ (Note: you cannot combine different data types into one variable, e.g. z=y$+x)

Rules of Data Assignment


BASIC has some rules as to assigning variables:

Invalid Name Reason


5NUM Cannot begin with a digit TOT PAY Blank spaces not allowed NET-PAY Hyphen not allowed B&W Special symbol & not allowed LIST Cannot use reserve words* *Reserve words are predefined words, for example PRINT.

Valid Name
NUM5 TOTPAY NETPAY BANDW

Indirect Mode (Programmer Mode)


So far we have been making programs that are executed directly, BASIC also allows us to make programs that can be designed to work for a specific task and can be saved. Programs are written starting with a line number followed by the commands, lines execute in ascending order. To start program mode, just type AUTO command, this command automatically assigns line numbers as you work, the default pattern is 10, 20, 30 Another important command you should know is CLS command, this command clears the screen so you can work easily.

Now lets make our first program to calculate average of 3 numbers: 10 A=2 20 B=4 30 C=6 40 AVG= (A+B+C)/3 50 ? AVG 60 END Ok, now we see two new things, firs is the question mark ? which is actually a shortcut for PRINT command, which requires no explaining, second is the END command, this command tells the computer when to stop the program. Now type in RUN to start our first program, the result should look like this:

Vous aimerez peut-être aussi