You are on page 1of 34

QBASIC

BCA-PPU-1ST YEAR
QBASIC
➢ Overview of QBASIC
▪ History of QBASIC
▪ Features of QBASIC
▪ Applications of QBASIC
▪ Advantage/Disadvantage of QBASIC
➢ Installing QBASIC
➢ How to write, save and execute QBASIC program
➢ I/O operation
➢ Tokens
▪ Character Set
▪ Keyword
▪ Constant/Literals
▪ Data type
▪ Identifiers
▪ Variables
▪ Operators
▪ Comment
QBASIC
➢ Control Statements
▪ Decision Making/ Statements
▪ Iterative/Looping Statements
▪ Jumping Statements
➢ Array
▪ One Dimensional Array
▪ Two Dimensional Array
➢ Procedure
▪ Function
▪ Subroutine
➢ Type
➢ File Handling
QBASIC
History of QBASIC
 It stands for Quick Beginners All Purpose Symbolic Instruction Code.
 It was developed and launched by Microsoft in the year 1991 and is considered to be one of the most ideal languages for
absolute beginners.
 This language is derived form BASIC language. This language was developed by John G Kemeny and Thomas E Kurtz in 1967.
 It was developed for MS DOS operating system.
 Its main objective is to develop programming skills for learning users. In this case we can create only console base application
that can be executed in DOS environment.

Features of QBASIC/ Advantage of QBASIC


 It supports procedural programming.
 It supports high level programming language.
 It is not case sensitive programming language,
 It is simple to learn.
 It is reach set of pre defined library function such as mathematical function, string function, conversion function.
 It is English like language so any user can learn easily.
 It is check syntax automatically.
QBASIC
Disadvantage/ Limitation/ Demerits/ Drawback/ Pit falls/ problems of QBASIC
 It does not support window based application.
 It does not support object oriented programming.
 It is not secure language.
 The execution speed of QBASIC program is slow due to interpreter.

Application of QBASIC
 It is use to develop console based application.
 It is support graphics programming, In this case we can design differ types of shapes. Such as rectangle, oval, human face etc.
QBASIC
I/O operation
How to write program in QBASIC:
Simple Program
CLS
Print “Welcome to QBASIC”
End
Extension of QBASIC:- .bas
CLS:
This command is used to the clear previous output on the console.
End:
This command is used to terminate a program.

Print statements:
PRINT statement is used to send data to the screen. It is used for output purpose that means print program result on VDU(Screen).
This is used to print any message, value of variable, expression and result.
Syntax:
Print “String message”, variables
Note:- Print commands generates newline implicitly.
QBASIC
How to input/read value during run time.
Input statements:
This command is used to input value through keyboard during run time.
Syntax:
Input “String Message”,a
Token:
Tokens is the most important element to be used in creating a program in QBASIC. We can define the token as the smallest individual
element in QBASIC. For `example, we cannot create a sentence without using words; similarly, we cannot create a program without
using tokens. Therefore, we can say that tokens is the building block or the basic component for creating a program in QBASIC
language.
Character Set
Character set represents different character set.
Alphabet:
(A - Z)- Uppercase
(a - z)- Lowecase
Digits:
(0 – 9)
QBASIC
Special Characters:
Key/symbol Explanation
Windows
Esc Esc (escape) key.
F1 - F12
Tab Tab key.
Caps lock Caps lock key.
Shift Shift key.
Ctrl Control key.
Fn Function key.
Alt Alternate key (PC only; Mac users have an Option key).
Spacebar Spacebar key.
Arrows Up, down, left, right Arrow keys.
Back Space Back space (or Backspace) key.
Delete Delete or Del key.
Enter Enter key.
Prt Scrn Print Screen key.
QBASIC
Special Characters:
Key/symbol Explanation
Insert Insert key.
Home Home key.
Page up Page up or pg up key.
Page down Page down or pg dn key.
End End key.
Num Lock Num Lock key.
~ Tilde.
` Acute, back quote, grave, grave accent, left quote, open quote, or a push.
! Exclamation mark
@ at the rate
# hash.
$ Dollar sign
% Percent
^ Caret
& Ampersand
QBASIC
Special Characters:
Key/symbol Explanation
* Asterisk
( Left parenthesis.
) Right parenthesis.
- Hyphen, minus, or dash.
_ Underscore.
+ Plus.
= Equal.
{ Open brace, curly bracket.
} Close brace, curly bracket.
[ Open square bracket.
] Closed square bracket.
| Pipe, or, or vertical bar.
\ Backslash
/ Forward slash, mathematical division symbol.
QBASIC
Special Characters:
Key/symbol Explanation
: Colon.
; Semicolon.
" Double Quote,
‘ Apostrophe or single quote.
< Less than or left angle brackets.
> Greater than or right angle brackets.
, Comma.
. Period, dot or full stop.
? Question mark.
QBASIC
Keywords
Keywords in QBASIC can be defined as the pre-defined or the reserved words having its own importance, and each keyword has its
own functionality. Since keywords are the pre-defined words used by the compiler, so they cannot be used as the variable names. If
the keywords are used as the variable names, it means that we are assigning a different meaning to the keyword, which is not allowed.
GOSUB END SUB FOR NEXT IF THEN SELECT LET COLOR PRINT VAL
INT ELSE ELSEIF CLS REM DATA INPUT TAB LOOP UNTIL DO LEFT$
END IF FUNCTION READ WRITE APPEND CLOSE KILL WHILE NOT CASE AND TO
SWAP DIM IS ASC CHR$ MID$ LEN USING UCASE$ STEP OR
QBASIC
Constant/ Literals
Literals are the constant values assigned to the constant variables. We can say that the literals represent the fixed values that cannot be
modified.
There are two types of constant:
Numeric Constant: Numeric constants are numbers with or without decimal points or negation.
Numeric constant divide into two parts:
Integer Constant:- 5,56,-47,
Float/ Real Constant:- 56.254, 12.06
String Constant: String constants are the collection of alphabets, numbers or symbols which is enclosed by double quote.

“Cimage college”
“12”
““
“”
“87.97”
“@#$”
“3b”
QBASIC
Identifier:
Identifier refers to name given to entities such as variables, functions, procedure, array, type in QBASIC..
Identifiers must be unique. They are created to give a unique name to an entity to identify it during the execution of the program -.:
There are various rules for identifier in QBASIC, as follows:

✓ The first character of an identifier must start with an alphabet or underscore, that could be followed by any sequence of digits (0-
9), letter or underscore.
✓ An identifier should not contain any reserved keyword.
✓ It should not start with any digit.
✓ An identifier can contain two underscores, but should not be consecutive.
✓ It should not include any commas or white spaces in-between characters.

✓ A, b, num, num1, while, 1n, s name, _1n, s_name, __name


QBASIC
Data Type:
A data type specifies the type of data in QBASIC. It defines any data such as Integer, Float, String etc.
There are two types of data type:
1. Predefined/ fundamental/ system defined/ inbuilt/ pre-emptive data types.
2. User define/ defined data type.

Predefined: - QBasic has five built-in types: INTEGER (%), LONG(&) integer, SINGLE(!) float, DOUBLE(#) float and STRING($).
Data Type Size(in byte) Range
INTEGER 2 -215 to 215-1
LONG 4 -231 to 231-1
SINGLE 4 - Single Precision- Maximum 7 fractional digits
DOUBLE 8 - Double Precision- Maximum 17 fractional digits
STRING 0 TO 32767
QBASIC
Variables:
A variable can store value in computer memory. A variable is also a quantity whose value changes during the execution of a program.
Like in mathematics a variable holds certain value Just in QBASIC; it is a placeholder for storing value in computer memory.
Some rules for making variable
1. The first character always start with letter (i.e. lowercase or uppercase letter).
2. The first character should not begin with numbers (i.e. numeric character).
3. The string variable always ends with dollar ($) symbol.
4. The maximum variable length must be 40 characters
5. the name of the variable must not be a reserved name like PRINT, INPUT, LET, ABS etc.
QBASIC
Variables:
Types of variable
 Numeric Variable
 String Variable
1. Numeric variable: A variable which can store numeric value is called numeric variable.
e.g.
A=12
pi=3.14
c=79.89
2. String variable: A variable which can store string variable is called String variable. String is a array of character enclosed within
the double inverted comma.
e.g.
N$="ram"
Place$=“CIMAGE"

Note:
A string variable always ends with $ symbol.
QBASIC
Variables:
Declaration of variables:
There are two ways to declare a variable in Qbasic:
1. Implicit Declaration: is the default method of declaring variable in QBASIC. Using this method, we can declare variable at the
time of assigning value to the variable. One should take care of type declaration symbol during the declaration and use of
variable.
The first is to put a data type symbol after the name.
Syntax:
<Variable name><Symbol name>
Symbol Data type
$ String
% Integer
& Long
! Single
# Double
C$=“Cimage”
QBASIC
Variables:
Declaration of variables:
There are two ways to declare a variable in Qbasic:
2. Explicit Declaration: Explicit declaration of variable is done before the use of variables. DIM statement is used to declare
variables explicitly in QBASIC. Eg: DIM a AS INTEGER, DIM b AS STRING, etc. Its not mandatory to use type declaration
symbol while declaring variables explicitly. LET statement to declare variables implicitly but LET statement is not mandatory in
newer versions of QBASIC.
Syntax:
DIM [Variable Name] As Data Type

DIM stands for dimension.


Example:
DIM [Variable Name] AS STRING
DIM [Variable Name] AS INTEGER
DIM [Variable Name] AS LONG
DIM [Variable Name] AS SINGLE
DIM [Variable Name] AS DOUBLE
QBASIC
Operators:
Operator is the symbol or a command that is used to perform operation on operands(values or variables) in program. There are
different types of operators supported by QBASIC.
Arithmetic operator: Arithmetic operators are those operators which helps program perform mathematical calculations in data.
Below is the list of Arithmetic operators used in QBASIC.
Name of operator Symbol
Addition + 5+2=7
Subtraction - 5-2=3
Multiplication * 5*2=10
Division / 5/2=2.5 6/2=3.0
Integer Division(returns quotient without decimal points) \ 5\2=2 6/2=3
Modular Division(Returns remainder) MOD 5 mod 2=1
Exponent ^ 5^2=25

Note: Mod operator sign depend on left operand


QBASIC
Relational/ Comparison operator:
The operators which are used to compare two values are called relational or comparison operators. Its give result either true (-1) or
false (0). The relational operators used in QBASIC are as follows.

Name of operator Symbol


Greater than >
Less than <
Greater than or equal to >=
Less than or equal to <=
Equal to =
Not equal to <>
QBASIC
Logical Operator
The operators which are used to combine two or more relational (conditional) expressions are called logical operators. It gives two
logical value "True" or "False". There are three logical operator in QBASIC:
 AND
 OR
 NOT

AND Logical Operator:


AND operator gives 'true' result, only when all the inputs/ conditions are 'true’ otherwise it gives result ‘false’. The AND truth table is
given below.

X Y X AND Y
T T T
T F F
F T F
F F F
QBASIC
OR Logical Operator:
OR operator gives 'true’, when any one or all inputs/ conditions are 'true’. It gives ‘false’, only when all the inputs/ conditions are
‘false’. The OR truth table is given below.

X Y X OR Y
T T T
T F T
F T T
F F F

NOT Logical Operator:


NOT Operators perform operation on one operand and return 'True' if the logical/ conditional operation returns 'False’ and vice- versa.
The NOT truth table is given below.
X NOT X
T F
F T
QBASIC
String Operator:
String operator joins two or more strings. This process is called concatenation. The '+' sign is used as the String operator. + is also called concatenation
operator when both operands are strings.
Python Comments:
A comment is one or more sentences that explain the purpose of a section of code. A comment is placed just before (or after) the code to which it refers. It
is a programmer-readable explanation or annotation In the source code of a computer program.
‘ and Rem are used for QBASIC Comments.
Rem “This is my comment”
‘This is my comment
Rem stands for remarks

Delimiters:
Delimiters is a special characters that performs particular operation such as separate multiple variable, declaring array variable, designing a function and
subroutine etc.
Delimiters Meaning
, Separate multiple variable
; Terminating newline with print command
: Write two line statement in one line
() Declaring array variable, function and subroutine
# Identify file member
“ Enclosing string
QBASIC
Expression:
An expression is collection of operator and operands.
 Extension of QBASIC:- .bas
 Cls: This command is used to clear previous output on the console.
 End: This command is used to terminate a program.
Control Statements:
Control Statements are used to execute/transfer the control from one part of the program to another depending on a condition. It is
mainly used to control the flow of execution of statements in program.
There are three types of control statements:
 Decision Making/ Conditional / Selection Statements
 Looping/ Iteration Statement
 Jumping Statements

Conditional Statements in QBASIC programming are used to make decisions based on the conditions. If we put some condition for a
block of statements, the execution flow may change based on the result evaluated by the condition. This process is called decision
making.
Control Statements:
Decision Making/ Conditional / Selection Statements:
It is based on condition. In this type of statements there are two parts, true and false. If condition is true the true part will execute
otherwise false part will execute.
There are four types of decision making statements:
 If…….end if (Simple if statement)
 If…..else…end if statements
 If……..elseif…..end if statements(if ladder)
 Nested if statements
 Select case statements
Control Statements:
If…….end if (Simple if statement):
In this statements there are only one part that means true part. If condition is true then if statements are execute otherwise no acrtion performed.
Syntax:
If (condition) then
statements
………….
End if
If…..else…end if statements
In this statement there are two part if and else. If contain condition, if condition is true then if block will executed otherwise else block will
executed. Else block can not contain condition.
Syntax:
If(condition) then
statements
………….
Else
statements
………….
End if
Control Statements:
If…….Elseif…….end if (Ladder if statement):
In this statements there are multiple conditions occurred. In this case only one condition is true and executed. The program checked first
condition, if condition is true then it executed otherwise next condition is checked. If no any condition met true then else part will executed.
Syntax:
If (condition) then
statements
………….
Elseif(condition) then
statements
………….
Elseif(condition) then
statements
………….
……………………………
……………………………
Elseif(condition) then
statements
………….
Else
statements
………….
End if
Control Statements:
Nested if statements:
When one if statement inside another if statement is called nested if statement.

Syntax:
If (condition) then ‘Outer if
if(condition) then ‘ Inner if
Statements
………….
Else
Statements
………….
End if
Else
Statements
………….
End if
Control Statements:
Select Case
Syntax 1: Using Exact Match
Select case <Value/ Variable>
Case <Value1>
Statements
…………..
Case <Value2>
Statements
…………..
………………………
………………………
Case <Value3>
Statements
…………..
Case Else
Statements
…………..
End Select
Control Statements:
Select Case
Syntax 1: Using Exact Match
Select case <Value/ Variable>
Case <Value1>
Statements
…………..
Case <Value2>
Statements
…………..
………………………
………………………
Case <Value3>
Statements
…………..
Case Else
Statements
…………..
End Select
Looping Statements:
A loop statement allows us to execute a statement or group of statements multiple times. Sometimes we may need to alter the flow of
the program. The execution of a specific code may need to be repeated several numbers of times. For this purpose, The programming
languages provide various types of loops which are capable of repeating some specific code several numbers of times.

Advantages of loops
 There are the following advantages of loops in Python.
 It provides code re-usability.
 Using loops, we do not need to write the same code again and again.
Looping Statements:
Types of loop
 while loop
 do …..while loop
 for loop
while loop
In QBASIC, while loop is used to execute a block of statements repeatedly until a given a condition is satisfied. And when the
condition becomes false, the line immediately after the loop in program is executed.
Syntax:
<Initialization>
While (condition)
Statements
…………….
<modification>
Wend

You might also like