Programming Intro and Qbasic

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

A programming language is a machine-readable artificial language designed to express

computations that can be performed by a machine, particularly a computer. Programming languages


can be used to create programs that specify the behavior of a machine, to express algorithms
precisely, or as a mode of human communication. Programming languages also allow humans to
communicate instructions to machines. The earliest programming languages predate the invention
of the computer, and were used to direct the behavior of machines such as automated looms and
player pianos.

Programming languages differ from most other forms of human expression in that they require a
greater degree of precision and completeness. When using a natural language to communicate with
other people, human authors and speakers can be ambiguous and make small errors, and still expect
their intent to be understood. However computers
"do exactly what they are told to do", and cannot
"understand" what code the programmer intended to
write.

They are basically of three types:

i) Machine Level Language


ii) Low level or Assembly Level Language
iii) High Level Language

Machine Level Language:

Machine languages are the only languages understood by computers. While easily understood by
computers, machine languages are almost impossible for humans to use because they consist entirely
of numbers (binary bits i.e. 0 or 1).
The disadvantages of Machine Level Language are:

i. It is extremely difficult to learn as program codes are to be written in binary form.


ii. It is machine dependent so program written on one computer cannot be run on
another computer of the same type.
iii. As they are machine dependent proper knowledge of CPU architecture ( addressing
methods, register types etc) is a must.

The advantage of Machine Level Language is the speed of execution of the programs. As the
programs are written in binary form, there is no need of assemblers or compilers to convert the codes
to machine readable form so the execution is fast. This also leads to smaller file sizes.

Low Level or Assembly Level Language:

Assembly languages are a type of programming language used to program computers,


microprocessors, microcontrollers, and other (usually) integrated circuits. They implement a symbolic
representation of the numeric machine codes and other constants needed to program a particular CPU
architecture. This representation is usually defined by the hardware manufacturer, and is based on
abbreviations (called mnemonics) that help the programmer remember individual instructions,
registers, etc. An assembly language is thus specific to certain physical or virtual computer
architecture. An assembly language programmer must understand the microprocessor's unique
architecture (such as its registers and instruction). Program written in assembly language are
converted to binary codes using special programs called assemblers.

 In assembly language a mnemonic is a code, usually from 1 to 5 letters, that


represents an opcode, followed by one or more numbers (the operands).
 Opcode or operation code is between one and three bytes in length and uniquely
defines the function that is performed. It is the data that represents a microprocessor
instruction.

Advantages of Assembly Language are:

i. Program written in assembly language are simpler than program written in machine
codes as use of binary codes to represent operational codes is replaced by words
(mnemonics).
ii. Program written for a family of microprocessors need not be rewritten i.e.
machine dependence is somewhat reduced.
iii. Less knowledge of CPU architecture compared to machine level language is required.

Disadvantages of Assembly Languages are:

i. Requirement of knowledge of CPU architecture is not completely eliminated.


ii. Assembly Languages are to be converted into binary codes using assemblers so final
executable file size is large compared to machine level ones.
iii. Execution of program is slow compared to that of machine level language.

High Level Language:

High level programming languages are those programming language which use normal everyday
word to represent the executable operational codes. These types of programming languages follow
strictly followed rule for writing these instruction. This rule is known as syntax. High level languages
are easy to learn as they avoid the need to understand the complex CPU architecture and also because
the commands are in plain understandable English form. These programs written in plain English
form following syntax are converted in machine understandable form using either compilers or
interpreters.

Advantages of High Level Language are:

i. They are easy to understand and user friendly.


ii. It reduces the complexity of programming as need of knowledge of CPU architecture
is eliminated.
iii. High level programs are very easy to maintain than machine and lower level
languages. In machine and lower level languages instruction are difficult and very
hard to locate, correct and modify but in high level languages it is very easy to
understand and modify when desired.
iv. Each high-level language provides a large number of built-in functions or procedures
that can be used to perform specific tasks during designing of news programs. In this
way, a large amount of time of programmer is saved.
v. Program written in high-level language is machine independent. It means that a
program written on one type of computer can be executed on another type of
computer.

Disadvantages of High Level Programming Language are:

i. The additional process of compilation needs more machine time than the straight
assembly process.
ii. There is no control of hardware part while writing high level programs.
iii. The programs have to be compiled every time a change is made.

Compiler:
A compiler is a computer program (or set of programs) that transforms source code (program) written
in a computer language (the source language) into another computer language (the target language,
often having a binary form known as object code). The most common reason for wanting to transform
source code is to create an executable program. The name "compiler" is primarily used for programs
that translate source code from a high-level programming language to a machine level language.

Assembler:
An assembler is used to translate assembly language statements into the target computer's machine
code. The assembler performs a more or less a one-to-one mapping from mnemonic statements into
machine instructions and data.

Interpreter:
An interpreter is a computer program which reads source code written in a high-level programming
language, transforms the code to machine code, and executes the machine code. Using an interpreter,
a single source file can produce equal results even in vastly different systems. Using a compiler, a
single source file can produce equal results only if it is compiled to distinct, system-specific
executables. It converts a high level language into an intermediate code which will be immediately
executed. The main disadvantage of interpreters is that when a program is interpreted, it typically runs
more slowly than if it had been compiled. The difference in speeds could be tiny or great; often an
order of magnitude and sometimes more. It generally takes longer to run a program under an
interpreter than to run the compiled code but it can take less time to interpret it than the total time
required to compile and run it.

QBASIC
The full form of BASIC is Beginner's All-purpose Symbolic Instruction Code.
In 1985 Microsoft released a version of BASIC called QBasic with its MS-DOS 5.0 operating system.
It resembles BASIC in its statements, but is more structured -and- flexible at the same time. For
instance, line numbers are not required but can be used - as well as "line labels" with text names. It’s a
great "learner’s language”.
QBASIC certainly is the easiest programming language of all to learn. Also, when knowing this
language, other programming languages are very easy to learn.

Variables and Constants:


Computers have thousands of spaces in the memory in which commands and information can be
stored. Before we can store commands in a memory space, we need to give the memory space a name.
Variables are these memory spaces where information is stored. Variable name is the name given to
these memory spaces. Any value can be given to the memory space. Variables are temporary storage
places of different kinds of values. Variables have a unique name. There are different types of
variables which can all contain different values. In other words, variable are those type of information
whose value at the start of the program and at the end of the program is different i.e. those type of
information whose value changes after the execution of the program. Whereas, constants are those
information whose value remains the same throughout the execution of the program.

Data Type used in QBASIC:

They refer to different types of data that can be used in QBASIC. They are basically of five different
types namely, integer, long integer, single precision floating point number, double precision floating
point number and string.

Integer is the first and most important data type. This data type is the most used data type of all.
Since an integer contains 16 bits, you can only store values from –32768 up to 32767.
Long integer is the second. Long integers are not used that often, since mostly integers work
fine. Long integers are actually the same as normal integers, so only non-decimal numbers. However,
a long integer contains 32 bits, allowing you to store values from –2147483648 to 2147483647.
Single precision, which can contain decimal numbers. Single precision are 32 bit and can
store values from around 9 e40 to 9 e-39.
Double precision, which can also contain decimal numbers. The only difference is that
doubles are 64 bits, thus providing space for values from around 9e305 to 9e-310.
String, can contain characters like ‘A’ or ‘ƒ’. Strings are always notated with double
quotation marks around them. (“ ”).They can also include numbers and symbols as long as they are
included within the double quotation (“”) .The length of the character-list can range up to 64k.
STRING "hello, this is a string"
INTEGER 5 or -5
LONG 92883
SINGLE 39.293211
DOUBLE 983288.18111112

Data types are declared adding postfix. The postfixes are as follows:

Integer:%
Long Integer: &
Single Precision: ! or write nothing
Double Precision: #
String: $
(FOR SYNTAX OF DIFFERENT STATEMENTS REFER TO THE
CLASS NOTES)
About loops:

Loops are used to repeat a block of statement. They are repeated desired number of times or until a
condition becomes true or while a condition is satisfied.

DO [WHILE/UNTIL expression] Runs commands between the DO and


… LOOP until or while an expression is
LOOP [WHILE/UNTIL expression] true depending upon the use of WHILE
or UNTIL with the DO..LOOP. When
no WHILE or UNTIL is used, the loop
will run forever.

FOR counter = start TO end [STEP factor] Runs commands between the For and
… Next while a counter is running. The
NEXT counter ‘loop’ will be exited if the counter
reaches the specified end-value. The
counter starts at start and ends at end,
and is increased every loop by factor.

WHILE [expression] This loop runs commands between the


… WHILE and WEND while an expression
WEND is true. The loop terminates if the
expression is false.

Functions:

A subroutine or subprogram (also called procedure, function, or routine) is a portion of code


within a larger program, which performs a specific task and is relatively independent of the remaining
code. In other words, functions are set of instructions, given a particular name that will be executed
when the main program calls for it. Two terms associated with sub routines are parameter and return
value. Parameters are the values taken to the function from the main body of the program where as
return value is the result that is passed by the function to the calling program. So, a subroutine may be
coded so as to obtain a specific set of data values from the calling program (its parameters), and
eventually provide a specific set of values to it (its return values). In other words, a subprogram
receives values called arguments or parameters from a calling program, performs calculations and
returns the results to the calling program.

The advantages of using of functions are:

i. The length of the source program can be reduced by using functions


at appropriate places.
ii. A function helps to reduce the amount of memory the program occupies.
iii. A program consisting of a number of functions are easy to debug compared to
programs written in single bulk.
iv. A subroutine or functions can be used to avoid rewriting the same sequence of code at
two or more locations in a program. This is especially useful if the code involved is
long or complicated.

Functions statements used in QBASIC are DEF FN statement, FUNCTION END FUNCTION
statement and SUB END SUB statement.

(Refer class notes for syntax and examples)

Global and Local Variables:

A variable used only within the routine or function it is defined in is called local variable. When the
function is finished and control is returned back to the part of the program that called it, the local
variables no longer exist. If a variable can be used by all the modules and functions within the
program, such variables are referred to as global variables. The common practice is to keep global
variables to a minimum, because they can be manipulated by any subroutine in the program at any
time.

Array:

Arrays are defined as systematic arrangement of similar data types. Arrays are declared at the start of
the program so that the compiler at run time can assign the array declared number of memory spaces.
Arrays share the same variable name. In other words, they can be referred to as an ordered
arrangement of data elements. A vector is a one dimensional array; a matrix is a two-dimensional
array. Arrays are also referred to as subscripted variables. An array can also be defined as a
systematic arrangement of objects, usually in rows and columns. Arrays can be single dimensions as
in marks(45) or multi dimensional as in r(3,2) which is a two dimensional array, y(1,2,3) which is a
three dimensional array, record(1,2,3,4,.....,n) which is a n-dimensional array. If the array sixe is of
more than two dimension, such type of array is referred to as multi dimensional array.

Advantages of using an Array:

i. Arrays permit efficient random access.


ii. We can use one name for similar objects and save then with the same name but different
indexes.
iii. Arrays are very useful when working with sequences data with same size and data type.
iv. Declaring an array large number of inputs can be taken using a loop which avoids
repeating of input statement for each of the n-variables the input is to be taken.
v. Arrays use low memory space.
vi. Arrays data fields in close proximity to each other.
vii. Data cache utilization.(Utilizes the cache memory set aside for speeding data fetch and
stororage.)

(About declaration, inputting an array refer class note)


Sorting:

Sorting is any process of arranging items in some sequence.

Sorting Techniques:

An internal sort is any data sorting process that takes place entirely within the main memory
of a computer. This is possible whenever the data to be sorted is small enough to all be held in the
main memory. For sorting larger datasets, it may be necessary to hold only a chunk of data in memory
at a time, since it won't all fit. The rest of the data is normally held on some larger, but slower
medium, like a hard-disk. Any reading or writing of data to and from this slower media can slow the
sorting process considerably.

External sorting is a term for a class of sorting algorithms that can handle massive amounts
of data. External sorting is required when the data being sorted does not fit into the main memory of a
computing device (usually RAM) and a slower kind of memory (usually a hard drive) needs to be
used.

 The difference between internal and external sorting is that internal sorting is done in internal
memory whereas external sorting is done in external memory like hard disk or magnetic tape.

Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to
be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.
The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
(The arrangement of inputted 10numbers in ascending order is an example of bubble sort.)

Insertion sort is a simple sorting algorithm that is relatively efficient for small lists and
mostly-sorted lists, and often is used as part of more sophisticated algorithms. It works by taking
elements from the list one by one and inserting them in their correct position into a new sorted list.

Shell sort was invented by Donald Shell in 1959. It improves upon bubble sort and insertion
sort by moving out of order elements more than one position at a time. One implementation can be
described as arranging the data sequence in a two-dimensional array and then sorting the columns of
the array using insertion sort.

Merge sort takes advantage of the ease of merging already sorted lists into a new sorted list.
It starts by comparing every two elements (i.e., 1 with 2, then 3 with 4...) and swapping them if the
first should come after the second. It then merges each of the resulting lists of two into lists of four,
then merges those lists of four, and so on; until at last two lists are merged into the final sorted list.

You might also like