2. QBasic
It all started back in
1963 when John
Goerge Kemeny
and Tom Kurtzas
got together at
Dartmouth College.
It is an interpreter
which reads every
line, translates it and
lets the computer
execute it.
NOTE: BASIC stands for Beginner’s All Purpose
Instruction Code.
3. RulesRules and Itsand Its FeatureFeature
QBasic programs are
executed in the order
which they are
written.
Every statement
should have at least
one QBasic
command.
All commands have
to be written using
the Syntax Rules.
Syntax is the
grammar of writing.
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.
Language is easy
since the variables
can be named
easily and uses
simple English
phrases.
4. DataData
(A collection of facts and figures that is entered into the computer.)(A collection of facts and figures that is entered into the computer.)
CONSTANTCONSTANT
(Data whose values do not change)
Numeric Constant
(numbers – negative or positive – used for mathematics)
e.g. 100
Alphanumeric Constant/String
(numbers of alphabets written within double quoutes)
e.g. “computer”
VARIABLEVARIABLE
(Data whose values can change fue to some calculation)
Numeric Variable
(numbers that are constant for arithmetic)
e.g. A = 50
Alphanumeric Variable
(variable that holds an Alphanumeric constant)
e.g. Name$ = “Akanksha”
5. ModesModes
(QBasic can be made to translate your instructions in two modes.)(QBasic can be made to translate your instructions in two modes.)
Direct
Mode
Progra
m Mode
Accepts single line instructions from the
user and the output is viewed as soon as
the key is pressed.
This mode is used to types a program which
is stored in the memory. They have line
numbers. We have to give the command to
get the output.
Note: Every programming language has its own SYNTAX (rules)
and COMMANDS.
6. Keywords and their FunctionsKeywords and their Functions
LIST
Used to list the
programs on the
screen.
RUN
Used to execute
the program.
LLIST
Used to list the
program as a
hardcopy.
LPRINT
Used to get the
output of the program
on the hard copy.
NEW
Used to clear the
memory of a existing
program.
7. Keywords and their FunctionsKeywords and their Functions
SYSTEM
Used to take
you back to dos
prompt.
PRINT & CLS
Print is used to
display on screen.
CLS is used to clear
the screen.
RME
Used to show the
position of the
mistake.
SAVE
Used to save the
program.
LOAD
Used to load the
program from the
disk to the memory.
8. QBasic CommandsQBasic Commands
CLS
This command is used to
clear the screen.
PRINT
This command is used to
display the output on the
screen.
REM
It stands for ‘remark’. It
makes the program more
understandable to the
reader.
LET
It assigns a value to a
variable in a program.
NOTE: A numeric data should be assigned to a numeric
variable and aplhanumeric data to an alphanumeric
variable.
9. QBasic CommandsQBasic Commands
END
This command is usually
given at the end of the
program.
INPUT
It allows the user to enter a
value for the variable while
running the program.
DELETE
To delete a line number in a
program.
10. QBasic Reminders!QBasic Reminders!
A program consists
of a line number.
It contains keywords
like: PRINT, END etc.
Each program line
begins with positive
number.
Run is used to
execute a program.
It is possible to overwrite a
statement but if you want to
write a new program, use
the NEW command.
To exit the QBasic
program, SYSTEM
command is used.
NO TWO
LINES
SHOULD
HAVE SAME
NUMBER.
11. When the PRINT command,When the PRINT command,
you can also print NUMBERSyou can also print NUMBERS
to the screen. Delete theto the screen. Delete the
current program (unless youcurrent program (unless you
already have) and write thealready have) and write the
following:following:
PRINT 512 (or ?512)PRINT 512 (or ?512)
<press enter><press enter>
Press F5 to run the program.Press F5 to run the program.
The program outputs:The program outputs:
512512
COMMANDSCOMMANDS
There are also specialThere are also special
functions calledfunctions called
“COMMANDS” (also“COMMANDS” (also
calledcalled
“INSTRUCTIONS”). A“INSTRUCTIONS”). A
“COMMAND” tells the“COMMAND” tells the
QBASIC interpreter to doQBASIC interpreter to do
something.something.
The PRINT commandThe PRINT command
tells the QBASICtells the QBASIC
interpreter to printinterpreter to print
something on the screen.something on the screen.
In this case, theIn this case, the
interpreter printedinterpreter printed
“HELLO WORLD!”.“HELLO WORLD!”.
TIP: Instead of typing
PRINT, you can enter a
question mark. For
example:
?“HELLO WORLD!”
12. EXPRESSIONSEXPRESSIONS
An expression is something the interpreterAn expression is something the interpreter
calculates (or evaluates). Such as:calculates (or evaluates). Such as:
1+11+1 (returns 2)(returns 2)
100-147100-147 (returns 53)(returns 53)
3*343*34 (returns102)(returns102)
80/480/4 (returns 20)(returns 20)
(100*3)+56(100*3)+56 (returns 356)(returns 356)
13. 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
16. RETRIEVING KEYBOARDRETRIEVING KEYBOARD
INPUT FROM THE USERINPUT FROM THE USER
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 is the program 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.
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$
17. To receive a number, use aTo receive a number, use a
non-string variable.non-string variable.
INPUT numberINPUT number
PRINT numberPRINT number
If you enter text instead of aIf you enter text instead of a
number, the QBASICnumber, the QBASIC
interpreter displays an errorinterpreter displays an error
message (“Redo frommessage (“Redo from
start”).start”).
RETRIEVING KEYBOARDRETRIEVING KEYBOARD
INPUT FROM THE USERINPUT FROM THE USER
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
18. THE IF AND THEN COMMANDS
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 “ X equals
5”
Since X does equal 5 in this
case, the program outputs:
X equals 5
EXPRESSION SIGNS
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”
19. THE IF AND THEN COMMANDSTHE IF AND THEN COMMANDS
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 to 5)
Run the following example:
CLS
X=5
IF (X>=5) THEN PRINT “X is
greater than 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
20. ELSEELSE
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 to 5, the output
is:
No
END IF
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.
21. Prepared by:Prepared by:
Obusan, Janssen Roselle T.Obusan, Janssen Roselle T.
Opiana, Christian Joseph I.Opiana, Christian Joseph I.