2. 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
3. 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.
4. 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.
5. 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 Mode
Program 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+4
Print “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+4
20 End
RUN
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
6. 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
7. 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 * 8
Print 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
8. SYNTAX: Let <Variable>=<Constant/Variable or
Expression>
e.g. Let A = 15……..Assigning constant to a variable
Let A = B……..Assigning variable to a variable
LET C = A+B….Assigning an expression to a
variable
Using 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$.
9. 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 constant
Input “Enter name:”;N$.....Giving
relevant message to avoid erroneous data input
10. 7. DELETE <LINE NO.>: To delete a line
number in a program. e.g.
Delete 10 will delete line number 10
Delete 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
11. QBASIC REMINDER
A QBASIC program consists of lines containing
1. A line number
2. A QBASIC keyword like PRINT ,END etc
3. Each program line begins with positive number.
4. No two lines should ha same number.
RUNNING A PROGRAM
RUN 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.
12. 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!”
13. 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
14.
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 + 478
Program 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 + 478
TIP: To clear the output screen, use
the CLS command.
15. You can use multiple print statements in your program.
PRINT “Hello”
PRINT “World”
Output:
Hello
World
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
16. 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 0
999998 0
999999 0
1000000 0
1000001 0
1000002 0
1000003 0
Variable
17.
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:
0
The next program sets x to15, and then prints the
variable:
x = 15
print x
The time, the output is:
15
18.
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 Value
999997 0
999998 0
999999 0
1000000 0
1000001 0
1000002 0
1000003 0
Variable X
19.
(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:
Y
num
VALUE
xYz
abc123
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)
20. 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$
21. To receive a number, use non-string variable.
INPUT number
PRINT 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$
22. The IF and THEN commands are used to compare an
expression and then perform some task based on that
expression.
x = 5
IF 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
23. 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 = 16
IF (x > 5) THEN PRINT “x is greater than 5”
Ouput:
x is greater than 5
Expression signs
24. 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:
CLS
x = 5
IF (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 5
x is less than or equal to 5
25.
Using the ELSE command, you can have the
program perform a different action if the statement is
false.
x = 3
IF x = 5 THEN PRINT “Yes” ELSE PRINT
“No”
Since x doesn’t equal 5, the output is:
No
ELSE
26. 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 = 5
IF (x = 5) THEN
INPUT a$
PRINT a$
END IF
The following program uses ELSE with the END IF command:
x = 16
IF (x = 5) THEN
INPUT a$
PRINT a$
ELSE
PRINT x * 2
END IF
Output:
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$
27. 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 = 6
IF ( 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 = 8
IF (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
28. 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 = 20
IF (x = 5 OR x = 20) THEN PRINT “Yes”
Output:
Yes
The AND operator requires both expressions to be true.
x = 7
IF (x > 5 AND x < 10) THEN PRINT “True”
Output:
True
This is a slightly more complex examples:
x = 16
y = 3
IF ((x > 5 AND x < 10) OR y = 3) THEN PRINT “Correct”
Output (since Y is 3):
Correct
Multiple expressions
29.
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