qbasic tutorial

Click here to load reader

Download Qbasic tutorial

Post on 21-Apr-2017

12.066 views

Category:

Devices & Hardware

15 download

Embed Size (px)

TRANSCRIPT

COMPUTER PROGRAMMING

COMPUTERPROGRAMMING: QBASIC TUTORIALSubmitted by:Jovelle D. LuzonDianne Myrell V. Sandoval

Submitted to:Mr. Angel Corona

BASIC stands for Beginners All Purpose Symbolic Introduction Code. It was invented in 1963, at Dartmouth Collage, by the mathematicians John George Kemeny and Tom Kurtzas.BASIC is an interpreter which means it reads every line, translates it and lets the computer execute it before reading another. Each instruction starts with a line number.INTRODUCTION TO QBASIC

FEATURES OF QBASICIt is a user friendly language.It is widely known and accepted programming language.It is one of the most flexible languages, as modification can easily be done in already existing programLanguage is easy since the variable can be named easily and uses simple English phrases with mathematical expressions.RULES OF QBASICEvery programming language has a set of rules that have to be followed while writing a program, following are some rules of QBASIC language:All QBasic programs are made up of series of statements, which are executed in the order in which they are written. Every statement should have at least one QBasic command word. The words that BASIC recognizes are called keywords.All the command words have to be written using some standard rules, which are called Syntax Rules. Syntax is the grammar of writing the statement in a language. Syntax Errors are generated when improper syntax is detected.

QBASIC DATAData is a collection of facts and figures that is entered into the computer through the keyboard. Data is of two types:

CONSTANT: Data whose value does not change or remains fixed. There are two types of constants:(a) NUMERIC CONSTANT: Numbers negative or positive used for mathematical calculations.e.g. -10, 20, 0 (b) ALPHANUMERIC CONSTANT/STRING: Numbers or alphabets written within double quotes(inverted commas ).e.g. Computer, Operating SystemVARIABLE: Data whose value is not constant and may change due to some calculation during the program execution. There are two types of variables.(a) NUMERIC VARIABLE: The value that holds a Numeric Constant for arithmetic calculations (+, -, *, /) is called a Numeric Variable.e.g. A = 50, here A is the Numeric Variable(b) ALPHANUMERIC VARIABLE: Cannot be used for arithmetic calculations. Alphanumeric Number must end with a $ sign and must be enclosed in inverted commas.e.g. Name$ = Akanksha, here Name$ is an Alphanumeric Variable.

Once QBASIC PROGRAM is loaded into the computer memory, it displays Ok prompt. Ok means it already to accept the commands. QBASIC can be made to translate your instructions in two modes:Direct ModeProgram ModeDirect Mode: The accepts single line instructions from the user and the output is viewed as soon as enter key is pressed. The instructions are not stored in the memory. This mode can be used to do quick calculation. They do not have lines. E.g.Print 3+4Print This is the Direct mode in QBasic2. Program Mode: The mode is used to type a program which is stored in the memory. They have line numbers. We have to give the command to get the output.e.g.10 Print 3+420 EndRUNPrograms are built up with set of instructions or commands. Every programming language has its own SYNTAX (rules) and COMMANDS.

TYPES OF MODE IN QBASIC

The following commands do not need line number.LIST- the command is used to list the program on the screen.RUN- the command is used to execute the program.LLIST- the command is used to list of program as a hardcopy.LPRINT- the command is used to get the output of the program on the hardcopy.NEW- the command is used clear the memory of the existing program.SYSTEM- the command is used to take you back to dos prompt.PRINT AND CLS- the command cam also be used without a line number. Print is used to view the display on the screen and CLS to clear the screen.RME- the command is used to show the position of the mistake.SAVE- the keyword is used to save the program.LOAD- the keyword is used to LOAD the program from the disk to the memory.COMMAND/KEYWORDS IN QBASIC AND THEIR FUNCTIONS

CLS: This command is used to clear the screen.PRINT: Print command is used to display the output on the screen.E.g. Print HELLO WORLD!!!Print 80 * 8Print Tab(10) Navrachana- will print Navrachana on 10 column.

3.REM: Stands for Remark. It gives an explanation of the program or of the statements in the program more understandable to the reader.

4. LET: It assigns a value to a variable in a program. It stores value in the memory location.QBASIC COMMANDS

SYNTAX: Let =e.g.Let A = 15..Assigning constant to a variableLet A = B..Assigning variable to a variableLET C = A+B.Assigning an expression to a variableUsing Let with Numeric Variables: Let A = 50, here A is a Numeric Variable and 50 is a Numeric Constant and the value 50 is assigned to a and stored in the computers memory for further calculations.Using Let with Alphanumeric Variable: Let N$ = QBasic Program, here N$ is an Alphanumeric Variable and QBasic Program is the Alphanumeric Constant assigned to N$.

Note: A numeric data should be assigned to a Numeric variable and an alphanumeric data to an alphanumeric variable otherwise TYPE MISMATCH error is displayed.

5. END- This command is usually given at the end of the program.6.INPUT: This statement allows the user to enter a value for the variable while running the program.SYNTAX: INPUT e.g.Input A..Enter Numeric constantInput N$.....Enter Alphanumeric constantInput Enter name:;N$.....Giving relevant message to avoid erroneous data input

7. DELETE : To delete a line number in a program. e.g.Delete 10 will delete line number 10Delete 30-50 will delete all the line between 30 to 50.Print with Comma (,): The screen of the computer is made of 80 columns and 40 rows. The columns are divided into five (5) zones 14 columns each. Comma placed after the message prints the message zone wise on the screen.

Print with Semi-Colon (;) Semi-Colon placed after the message to be displayed, leaves no space between two messages.e.g. Print This is an example; of QBasic program Output: This is an example of QBasic program

QBASIC REMINDERA QBASIC program consists of lines containing1. A line number2. A QBASIC keyword like PRINT ,END etc3. Each program line begins with positive number.4. No two lines should ha same number.RUNNING A PROGRAMRUN is the command used to execute the program and get the output on the screen.WRTING A NEW PROGRAMIt is possible to overwrite lines with a new statements, but if you want to write a totally new program use a NEW command.EXITING QBASICIn order to exit the QBASIC program SYSTEM command is used.

There are also special functions called commands (also called instructor). A command tells the QBasic interpreter to do something.

The PRINT command tells the QBasic interpreter to print something to the screen. In this case, the interpreter printed Hello World!.

With the PRINT command, you can also print numbers to the screen. Delete the current program (unless you already have) and write the following:PRINT 512 (or ?512)Press F5 to run the program. The program outputs:512COMMANDSTIPS: Instead of typing PRINT, you can enter a question mark. For example:?Hello World!

An expression is something the interpreter calculates (evaluates). Such as:1 + 1 ( returns 2)100 47 ( returns 53)3 + 34 ( returns 102 )80 / 4 ( returns 20 )( 100 * 3 ) + 56 ( returns 356 )

EXPRESSIONS

NOTE: The asterisk ( * ) means to multiply two numbers; the slash ( / ) means to divide

If you pass an expression to the PRINT command, the value returned (a number) is printed.Clear the current program, and then run the following:PRINT 512 + 478Program output:990

IF you enclose the expression with quotation marks, the expression becomes a string and isnt evaluated. For example:PRINT 512 + 478Output:512 + 478

TIP: To clear the output screen, use the CLS command.

You can use multiple print statements in your program.PRINT HelloPRINT WorldOutput:HelloWorldTo place World onto the previous line, place a semi-colon after PRINT Hello.PRINT Hello;PRINT WorldOutput:HelloWorldAlso, if you put a comma instead of a semi-colon on the first line, the program will insert spaces between the two words.PRINT Hello,PRINT WorldOutput:Hello WorldMore about the PRINT command

This chapter discusses an important topic in programming, variables. Please read this section thoroughly.

A variable is a piece of data kept in the computers memory (RAM). The location of a variable in RAM is called the address.

VariablesMemory addressValue99999709999980999999010000000100000101000002010000030

Variable

16

How a variable is stored in RAMThe following program prints the variable x to the screen.print x

Since the variable hasnt been assigned a number, the value of the variable is 0. So, the output of the program is:0The next program sets x to15, and then prints the variable:x = 15print xThe time, the output is:15

In the above example, the number 15 was stored in the computers RAM at a certain memory address. Then the PRINT command accessed (or looked at) that address when it printed 15 to the screen.

Memory address Value99999709999980999999010000000100000101000002010000030

Variable X

(NOTE: The memory address of x is not necessarily 1000000)

As in the program above, a variable is accesses by calling its name. Variable names can have a combination of letters and numbers. The following are valid variables:YnumVALUExYzabc123

ADVANCED TIP: Although you dont normally need to, you can find the actual memory address of a variable (x, for example) by using the VARSEG and VARPTR commands.

PRINT (VARSEG(X) * VARPTR(X)

One way to receive input from the keyboard is