qbasic tutorial

30
COMPUTER PROGRAMMING: QBASIC TUTORIAL Submitted by: Jovelle D. Luzon Dianne Myrell V. Sandoval Submitted to: Mr. Angel Corona

Upload: jovelleluzon

Post on 21-Apr-2017

12.111 views

Category:

Devices & Hardware


19 download

TRANSCRIPT

Page 1: Qbasic tutorial

COMPUTERPROGRAMMING:

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

Submitted to:Mr. Angel Corona

Page 2: Qbasic tutorial

BASIC stands for Beginner’s 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

Page 3: Qbasic tutorial

FEATURES OF QBASIC

It 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 program

Language is easy since the variable can be named easily and uses simple English phrases with mathematical expressions.

RULES OF QBASIC

Every 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.

Page 4: Qbasic tutorial

QBASIC DATA

Data 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 System”

VARIABLE: 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.

Page 5: Qbasic tutorial

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 Mode

1. Direct 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 QBasic”

2. 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 EndRUN

Programs are built up with set of instructions or commands. Every programming language has its own SYNTAX (rules) and COMMANDS.

TYPES OF MODE IN QBASIC

Page 6: Qbasic tutorial

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

Page 7: Qbasic tutorial

1. CLS: This command is used to clear the screen.2. 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

Page 8: Qbasic tutorial

SYNTAX: Let <Variable>=<Constant/Variable or Expression>

e.g. Let A = 15……..Assigning constant to a variable

Let 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 computer’s 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$.

Page 9: Qbasic tutorial

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 <VARIABLE>e.g. Input A…..Enter Numeric constant

Input N$.....Enter Alphanumeric constantInput “Enter name:”;N$.....Giving relevant

message to avoid erroneous data input

Page 10: Qbasic tutorial

7. DELETE <LINE NO.>: 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

Page 11: Qbasic tutorial

QBASIC REMINDERA QBASIC program consists of lines containing

1. 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 PROGRAM

It is possible to overwrite lines with a new statements, but if you want to write a totally new program use a NEW

command.EXITING QBASIC

In order to exit the QBASIC program SYSTEM command is used.

Page 12: Qbasic tutorial

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 Enter>

Press F5 to run the program. The program outputs:512

COMMANDS

TIPS: Instead of typing PRINT, you can enter a

question mark. For example:

?”Hello World!”

Page 13: Qbasic tutorial

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

Page 14: Qbasic tutorial

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 isn’t evaluated. For example:

PRINT “512 + 478”Output:

512 + 478TIP: To clear the output screen,

use the CLS command.

Page 15: Qbasic tutorial

You can use multiple print statements in your program.

PRINT “Hello”PRINT “World”

Output:HelloWorld

To place World onto the previous line, place a semi-colon after PRINT “Hello”.PRINT “Hello”;PRINT “World”

Output:HelloWorld

Also, 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 “World”

Output:Hello World

More about the PRINT command

Page 16: Qbasic tutorial

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

A variable is a piece of data kept in the computer’s memory (RAM). The location of a variable in RAM is called the “address.”

Variables

Memory address

Value

999997 0999998 0999999 01000000 01000001 01000002 01000003 0

Variable

Page 17: Qbasic tutorial

How a variable is stored in RAM

The following program prints the variable x to the screen.print x

Since the variable hasn’t 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 x

The time, the output is:15

Page 18: Qbasic tutorial

In the above example, the number 15 was stored in the computer’s 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 Value999997 0999998 0999999 01000000 01000001 01000002 01000003 0

Variable X

Page 19: Qbasic tutorial

(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 don’t 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)

Page 20: Qbasic tutorial

One way to receive input from the keyboard is with the INPUT command. The INPUT command allows the user to enter either a string or a number, which is then stored in a variable.

INPUT data$PRINT data$

When this program is executed, the INPUT command displays a question mark, followed by a blinking cursor. And when you enter text, the program stores that text into the variable data$, which is printed to the screen.

Retrieving Keybord input from the user

TIP: If you place a string and a semi-colon between INPUT and the variable, the program will print the string.

INPUT “Enter some text:”; data$

Page 21: Qbasic tutorial

To receive a number, use non-string variable.

INPUT numberPRINT number

If you enter text instead of a number, the Qbasic interpreter displays an error message ( “Redo from start “ ).

Below is another example of the INPUT command:

PRINT “ Enter some text : “INPUT text$

PRINT “ Now enter a number : “INPUT num

PRINT text$PRINT num

TIP: you can have the question mark displayed on the previous

line by using a semi-colon.PRINT “ Enter some text:”;

INPUT text$

Page 22: Qbasic tutorial

The IF and THEN commands are used to compare an expression and then perform some task based on that expression.

x = 5IF x = 5 THEN PRINT “ equals 5 “Since x does equal 5 in this case, the program outputs:

x equals 5

The IF and THEN commands

Page 23: Qbasic tutorial

You can also enter the following statements, instead of the equals sign:x < 5 (x is less than 5)x > 5 (x is greater than 5)Run the following:x = 16IF (x > 5) THEN PRINT “x is greater than 5”Ouput:x is greater than 5

Expression signs

Page 24: Qbasic tutorial

You can also combine the signs like this:x <= 5 (x is less than or equal to 5)x >= 5 (x is greater than or equal to 5)x <> 5 (x does not equal 5)Run the following example:CLSx = 5IF (x >= 5) THEN PRINT “x is greater than or equal to 5”IF (x <= 5) THEN PRINT “x is less than or equal to 5”IF (x <> 5) THEN PRINT “x does not equal to 5”Output:x is greater than or equal to 5x is less than or equal to 5

Page 25: Qbasic tutorial

Using the ELSE command, you can have the

program perform a different action if the statement is false.

x = 3IF x = 5 THEN PRINT “Yes” ELSE PRINT

“No”Since x doesn’t equal 5, the output is:

No

ELSE

Page 26: Qbasic tutorial

END IF allows you to have multiple commands after the IF. . .THEN statement, but

they must start on the line after the IF statement. END IF should appear right after the list of commands.

x = 5IF (x = 5) THEN

INPUT a$PRINT a$

END IFThe following program uses ELSE with the END IF command:

x = 16IF (x = 5) THEN

INPUT a$PRINT a$

ELSEPRINT x * 2

END IFOutput:

32

END IF

TIP: There is a way to have multiple commands after

IF. . .THEN without using END IF. To do so, place a colon between each command.

IF (x = 5) THEN PRINT a$: PRINT a$

Page 27: Qbasic tutorial

The ELSEIF command allows you to perform a secondary action if the first expression was false. Unlike ELSE, this task is only performed if specified statement is true.

x = 6IF ( x = 6 ) THEN

PRINT “ Statement 1 is true “ELSEIF ( x = 6 ) THEN

PRINT “ Statement 2 is true “ENDIF

Output:Statement 2 is true

You can have multiple ELSEIF commands, along with ELSE.x = 8IF (x = 5) THEN

PRINT “Statement 1 is true”ELSEIF (x = 6) THEN

PRINT “Statement 2 is true”ELSEIF (x = 7) THEN

PRINT “Statement 3 is true”ELSE

PRINT “No above statement are true”END IF

Output:No above statements are true

ELSEIF

Page 28: Qbasic tutorial

You can have more than one expression in IF. . .THEN by using either the OR operator or the AND operator.

The OR operator only requires one expression to be true in order to print “Yes” in the following program:

x = 20IF (x = 5 OR x = 20) THEN PRINT “Yes”

Output:Yes

The AND operator requires both expressions to be true.x = 7IF (x > 5 AND x < 10) THEN PRINT “True”

Output:True

This is a slightly more complex examples:x = 16y = 3IF ((x > 5 AND x < 10) OR y = 3) THEN PRINT “Correct”

Output (since Y is 3):Correct

Multiple expressions

Page 29: Qbasic tutorial

So far this chapter, we’ve only been dealing with numbers, but you can also use strings with the IF. . .THEN command.

x$ = “Hello”IF (x$ = “Hello” OR x$ = “World”) THEN PRINT x$

Output:Hello

Strings in IF. . .THEN

Page 30: Qbasic tutorial

Jovelle D. LuzonDianne Sandoval