Qbasic Theory Notes

Definition

QBASIC (Quick Beginner’s All-purpose Symbolic Instruction Code) is a high-level programming language developed by Microsoft Corporation in 1985. It is a modular programming language, where program is divided into different modules or procedure. QBasic is simple and easy to learn. It uses English like keywords and mathematical symbols to write a program.

Data and Data Type

Data is any value (raw or unprocessed) given to the program for processing. Basically, QBASIC supports two types of data.

i) String - any alphanumeric values and valid symbols

ii) Numeric - only numbers (0-9) QBASIC supports further sub-types of numeric data:

QBASIC supports further sub-types of numeric data:

Integer Non-floating-point numbers from -32,768 to 32,767

Examples: -34, 987, 0

Long Integer Non-floating-point numbers from -2,147,483,648 to 2,147,483,647

Examples: 340035, 7, -24, 0, -127010, 14, 8, -10

Single-Precision

Floating-point numbers from -3.37x10^38 to 3.37x10^38 (up to 7 digits)

Examples: 3.45, 31.4567, -35.23, 0.45, -3.15, 890, -98

Double Precision

Floating-point numbers from -1.67x10^308 to 1.67x10^308 (up to 15 digits)

Examples: 723661.90226, -128.78, 185321.093423, 2345544, -123456897

Character Set

Characters from the QBASIC character set are combined to form keywords, variables, operators and labels. They are further combined to form instructions that make up a program. The QBASIC character set consists of all alphabetic letters (both uppercase and lowercase), all digits and some special characters having special meaning to the basic translators.

#, $,!, %,& à  They are suffixes used at the of variable. end of variable names to specify the type.

+,-,*,/,\  à They are mathematical operators.

<,>,=  à They are relational operators.

(,) à They are parenthesis.

: à It is used to write multiple statements in a single line.

, ; à They are separators and are used to separate the arguments in input and output statements.

^ à It is exponentiation and is used to raise power to.

?  à It can be used in place of a PRINT statement.

‘ à It is used to mark a line as a comment instead of a REM statement.

. à It is a decimal point and is used in numbers.

A character denotes any alphabet, digit or special symbol used to represent information. The below table shows the valid alphabets, numbers and special symbols supported by QBASIC.

Alphabets à A,B,C, …., X,Y,Z  or a,b,c, …., x,y,z

Digits à 0,1,2,3,4,5,6,7,8,9

Special Characters à , =, +, -, _, *, /, \, ^, %, $, &, !, #, “, ;, ., ‘, ,, ?, <, >

Words : These are mainly the following two types:

Reserved words: Words having a special meaning to QBASIC such as the name of the statements, library functions etc. are termed as reserved words or keywords. E.g. Statements PRINT, INPUT, LET, CLS etc are reserved words and should not be used as the name of the variables or functions.

User-Defined words: These are the words used in statements as variables or user-defined functions. These variables may have different meanings in different programs. Example: LET NAME$=”RAM” Here, LET is a keyword and is a reserved word while NAME$ is a variable and is a user-defined word.

(b) Variable : A variable is a small amount of computer memory (RAM) that has been given a name. It stores the value of a particular data type while the program is being executed. The location of a variable in RAM is called the "address."

Rules for naming variables

i) Each variable name must start with an alphabetic character though it can contain alphabetic and numeric characters and periods. E.g. 1BC  × The first letter is a numeric character

ii) The variable name cannot be a BASIC keyword. E.g. LET × Keyword

iii) Variable names can be up to 40 characters long.

iv) Each variable name is followed by a symbol. The symbol refers to the data type it might hold. Eg. BC+  à The invalid symbol is used. ABC$ Ö Starts with alphabets and $ sign indicates the data type string.

(c) Constants : A constant is an entity that doesn’t change, whereas a variable is an entity that may change.

Example: a = 4 , 4 , a

Since the location whose name is a can hold different values at different times, so a is known as a variable. As against this, 4 does not change, hence is known as constant.

Types of Constants

Numeric Constant: It has only numbers from 0 to 9. For example: 45,456.35, -44 etc.194

String Constant: It accepts both numbers and alphabets (alphanumeric values). Examples: “Nepal”,

“45.67”, “A45” etc.

Symbolic Constant: A constant whose value remains the same through the whole program is called a symbolic constant. In QBASIC, CONST statement is used to declare a symbolic constant.

(d) Variable Declaration : QBASIC supports two types of variable declaration.

i) Implicit Declaration : In this type of declaration, the type of variable is defined as certain special declaration characters. We use %,&,!,# and $ symbols to declare the variable type implicitly.

Example:

A$=”Nepal”

Here, we use $ symbol after the variable name A, which indicates the type of variable is string.

INPUT “Type any number “;B%

Here, we use % symbol to declare the variable type integer.

ii) Explicit Declaration : In this type of declaration, we use DIM …AS statement.

DIM variable AS type : It defines the type of variable and we can use the following keywords.

INTEGER, LONG, SINGLE, DOUBLE, and STRING

Example:

DIM A AS STRING, B AS INTEGER

CLS

INPUT "Type your name "; A

INPUT "Your roll number "; B

C$ = MID$(A, 1, 1)

PRINT "First character of your name is "; C$

PRINT "Your roll number is "; B

END

Here, in the above program, the declaration characters ($ and %) are not used for the string-type variable A and integer-type variable B. So, A and B are defined explicitly using DIM…AS statement and C is defined implicitly.

Data types and variable declaration in QBASIC

String $ [STRING] ,1 byte/char,  32,767 chars to  0 chars

Integer % [INTEGER], 2 bytes,  32,767 to -32,768

Long Integer & [LONG] 4 bytes 2,147,483,647 to -2, 147, 483, 648 ,

Single Precision ! [SINGLE] (up to 7 digits)  4 bytes ±3.402823 E+38 to ±1.401298 E-45

Double Precision # [DOUBLE] (up to 15 digits) 8 bytes ± 1 . 7 9 7 6 9 3 1, D+308 to ±4.940656 D-324

Operators, expressions and operands


RIGHT (Correct) Ways to Declare Variables

Variable name length: Up to 40 characters

Must start with a letter

No spaces or special symbols

Type symbols only at the end

Arrays must use DIM

Example.

A

X

Total

Marks

Number1


A%        'Integer

Total&    'Long integer

Average!  'Single precision

Salary#   'Double precision

Name$     'String


DIM A AS INTEGER

DIM Name AS STRING

DIM Marks AS SINGLE

DIM Salary AS DOUBLE


DIM A AS INTEGER, B AS INTEGER

DIM Name AS STRING, Address AS STRING


DIM Marks(10) AS INTEGER

DIM Name$(20)


WRONG (Incorrect) Ways to Declare Variables

Starting with a number

1A

2Total


Using special characters (except type symbols)

Total@

Marks#

Roll-No


Using spaces in variable name

Total Marks

Student Name


Using reserved words

PRINT

INPUT

FOR

DIM


Wrong placement of type symbols

%Marks

Na$me


Multiple type symbols together

Marks%$

Total!#


Declaring without DIM in arrays

Marks(10)


(a) Operators : Operators are the symbols used to represent a particular operation. QBASIC supports

three types of operation: arithmetic, relational and logical operators.

i) Arithmetic Operators : These operators represent arithmetic operations such as addition, subtraction,

multiplication etc.

Operator          Operation        Example           Result

^ (caret) à Exponentiation à 10^3 = 1000

/(slash) à Division à 10/3  = 3.333333

\(back slash) à Integer Division à 10\3 = 3

* (asterisk) à Multiplication à 10*3  = 30

+ (plus) à Addition à 10+3 = 13

- (minus) à Subtraction à 10-3 = 7

MOD à Modular Division à 10 MOD 3 = 1

ii) Relational (Comparison) Operators

Relational operators are used to comparing two values and return either true or false. Assuming that the values of a and b are 6 and 3 respectively.

Operator          Operation        Example           Result

= à Equal to  à a = b à False

< à Less than à a < b à False

> à Greater than à a > b à True

<= à Less than or equal to à a <= b à False

>= à Greater than or equal to àa >= b à True

<> à Not equal to à a <> b  à True

 

iii) Logical Operator : Logical operators are used to combining two or more relational expressions and return a single value as True or False after evaluating them.

AND Operator : An AND operator returns True (1) at its output only when all of the inputs are high (1).  False (0) is returned when one or more of the inputs are low (0).

OR Operator : An OR operator returns True (1) at its output when one or more of its inputs are high (1). False (0) is returned only when all of the inputs are low (0).

NOT Operator : The NOT operator (Inverter) performs the logic function called inversion or  complementation. If the input signal is low then the high output signal is obtained and vice versa.

 

iv) String Operator : +(plus) → Concatenation, Concatenation is the process of joining two string expressions.

A$ = “Visit”

The value of C$ is:

B$= “Nepal”

VisitNepal

C$=A$+B$

(b) Expressions : An expression is a set of variables and constants having a definite value (operands) in

which various mathematical operators are used to show the relationship between the  variables and constants. Example:

a+b)^2-2*a*b

X$=MID$(A$,N,1)

Types of expression

Algebraic Expression : The expressions that are used in mathematical and other fields are called Algebraic expression.  Example: S = ut + at2

QBASIC Expression: The expressions that can be used in QBASIC programs are called QBASIC expressions. Example: S = u*t + (a*t^2)/2

(c) Operands : Operands are the values on which the operator performs its operation.

For example, in the below expression, C = A+B where,  The symbols ‘=’ and ‘+’ are the operators and the variables ‘C’, ‘A’ and ‘B’ are the operands.



No comments:

Post a Comment