Qasic Programming - ND I Comp Engineering
Qasic Programming - ND I Comp Engineering
Programming: This is the act of coding or writing of programs in a given language to instruct
the computer to solve a problem. On the other hand, it is the process whereby which a user
specifies to the computer what he wants the computer to do for him.
COMPUTER PROGRAMMING LANGUAGES
There are three levels of programming languages;
Machine language: is a language made up of strings of binary digits e.g. Binary (0’s
and 1’s) or decimal codes its machine dependent.
Low level Language: - it is a symbolic representation of a specific machine language.
The symbolic language program writing so much easier for the programmer than
machine language. Low level languages are closed to machine language e.g. Assembly
language. In which symbols are used instead of numbers to represent operation code
and storage address.
HIGH LEVEL LANGUAGES: - the need to make programming task less
cumbersome led to the development of another type notation, which took, into account
the type of problem and the normal approach to solving it, rather than the features of the
computer hardware. This notation is called problem oriented or high-level-language, the
popular ones being C, C++, BASIC, COBOL, and PASCAL, JAVA , FORTRAN and
etc.
In high-level programming, act of program debugging (correcting) and maintenance are
much easier and less error-prone than both machine and low-level-language programs for one
computer can easily be transported to another computer.
However, any computer cannot directly execute high-level-language programs such
programs have to be translated to machine language.
Source program: this is the name given to program written in high-level-language; this needs
to the translated into the language, which the computer understands (machine language).
Object Language: this is name given to the translated version of the source program, i.e. the
machine language equivalence of the source program.
TRANSLATORS: - these are programs that allow users to write programs in languages
different from the ones understood by the computer. They translate programs written in high
and low level language to the machine language equivalence.
1
The manufacturers write them. They include:
Assembler: converts Assembly language to machine language
Compiler: converts high level language to machine language at once.
Interpreter: also converts high level language to machine language, but does so
one statement after the other, sequentially.
Differences between Interpretive and Compiled Languages.
Interpreter: This is a language translator that translates the high–level-language to machine
language. It translate them one after the other sequentially or translate. Any time translated, it
executed by the computer before the next line is translated. If the interpreter meets a syntax
error in any line, it will display the error and the number. The error must be debugged (i.e.
executed) before the interpreter can go to next line. The problem with the interpreter unlike the
compiler is that any time the program will be executed or run again, it must have to be
translated by the interpreter, e.g. Basic, Lisp, Appl, Snobol-4, etc.
Compiler: This language translator converts programs from high- level –language to machine
language. It picks the high level languages contained in the source file and converts all to
machine language. It creates a file called object file by itself as I am doing the transaction
where it puts the translated version of the source code called object code. In essence the
compiler maintains two files i.e. source file and object file. The object file is in executed form.
(b) Design : the way or method of how your problem is solved is produced
(c) Implementation: the method found in design is then coded here in a given
programming lang.
2
(d) Testing: here we verify that the program written is working correctly
ALGORITHM
Algorithm is a step-by-step procedure for solving a problem in a finite amount of time.
Example: write a QBasic program that can accept two integer numbers and compute and display
their sum.
Algorithm
1. Accept the two integer numbers as A, B
2. Compute the sum of the numbers as C = A + B
3. Display C
4. Stop
3
The arrows indicates the flow of logic each box contains outline of the steps to be
Executed by the computer at each stage. The symbols and their functions are shown below:-
4
Example: Draw a flowchart that can accept two integer numbers and compute and display their
sum.
Start
Enter A, B
C=A+B
Display C
Stop
INTRODUCTION TO QBASIC
5
However, BASIC is very slow in execution compared to other programming languages
VERSION OF BASIC
QBASIC GWBASIC, TURBO BASIC, VISUAL BASIC, etc
Q BASIC PROGRAMMING
QBASIC is a version of many different versions of BASIC program language in the market.
QBASIC is the name for the version BASIC. It is user friendly, which makes it differ from all
other versions of BASIC. it is provided with many list of options which make it easier to use
than other versions of BASIC without knowing the commands to LOAD SAVE< RUN a
program one can easily use the menu option to perform all these operations. it also has an on-line
help facility where a user get all necessary help on how to use a command or keyword without
going through a volume of textbooks. It also supports the use of mouse to perform some
operations or the use of combinations of keys achieves severe typing of command lines and so
on.
STARTING QBASIC
Basic can be started in a number of ways, depending on how your system is set up:
if your system is set up to start with dos prompt, e.g. C:/> if basic is installed under a
directory BASIC, then change to the directory first by typing C:/>CD QB45
The system will change the prompt to:
C:/QB45>
Then type QB like this:
C :/> QBASIC, then press enter key, then the system will display an opening logo; press (esc)
key to clear this, then you will be taken to the Basic screen.
If your system is booting from Microsoft windows, follow these steps to open your Basic
if the Basic was installed under the windows environment.
click start button
select programs
click Microsoft Basic in the secondary windows displayed
The opening logo will be displayed, press escape (ESC) to clear it, then you are taken to
the Basic editing screen.
6
7
For now, just exit QBasic by typing "escape" then "alt" and then "f".
Push the down arrow on your keyboard until "Exit" is highlighted, and then hit "enter". You will
return to the DOS prompt.
(Note: you will not be able to use your mouse to make selections by clicking on words and icons
within the QBasic window. To make selections from the menu, use the "Alt" key, and then use
the arrow keys to change the selection. Another way to do this is to hit "Alt" and then use the
first letter of the menu selection you want.)
8
QBASIC SCREEN
The Basic interpreter is made up of three screens: these are the HELP screen, PROGRAM
screen and the output screen.
Basic has an extensive on-line help system that easy to use
the help screen is used by the basic interpreter it display assistance on QBASIC
commands and menu options
The program screen is used by the user to type in and to edit program.
The output screen is used by the interpreter to display their user output when executing
his/her programs.
Program screen
This program has three features, which are: menu bar, program window, edit window
Menu bar this bar is at the top of the screen on which the Basic interpreter displays the list of
menu options.
Program windows: this is the screen used by the user to type and edit commands and instruction
needed in accomplishing a given task.
Edit window: this is below the program window, where the user program can be edited while
the user is viewing the help screen.
QBASIC OPTIONS
The Basic interpreter contains list menu options from which a user can make choice on
what operation is to carry out. These include file, edit, view, and search, run Debug, option and
help menus.
EACH OF THE MENUS IS HAS A LIST SUB-MENUS IN A PULL DOWN BOX FOR
EXAMPLE the file menu.
The file menu
The file menu is used to carry-out all the major operations on files and the program in
the program screen. The options under this menu and their operations are:
New: this is used to clean and remove loaded files from the memory.
Open: used to retrieve and load a new file from the disk to the memory.
Save: used to write the contents of the current file on the screen to the disk.
Save-as: used to write the current file with a specific name to a disk (i.e. to duplicate a
file).
9
Print: to print or direct a specified file display on the screen to a printer.
Exit: use to exit and close the editor and return control to the DOS prompt.
10
Logical operators
Logical operators obtain logical results from comparing two logical or relational expressions this
operator includes:-
AND conjunction "(logical "and")
OR disjunction (inclusive "or")
NOT bit while compliment (logical "not") works with a single expression.
Syntax
EXPRESSION
An expression can be described as being of literals, other constant and arithmetic or logical
operators, or a single literal string.
In other words, it provides a value to be assigned. Expression can be of different types,
arithmetic's expression for arithmetic's operation expression for comparing two or more values.
e.g. c= A*B, D = A + B ; E = A – B ; Sum < 25
If {mark >30} AND (mark < 45) etc.
11
they may be composed of alphabets and digits
reserved words are not allowed
Declaration of a variable type.
A variable name ending with one of the following characters implies a type declaration
Declaration Data Type Max. Min.
$ - string variable 32,767 characters 0 characters
% - integer variable 32,767 -32,767
& - long integers variable 2,147,483,647
! - Single precision variable (7-digit fraction numbers) precision
# - double precision variable (15-digit fraction numbers) precision
The input statement in Basic
the INPUT statement is use to ask computer to accept values from the user through the keyboard
as the program is working that is the input statement reads input or entries from the keyboard or
a file. It allows a value, numeric or character string, to be typed at the keyboard and stored in the
computer memory as the value store in the computer during program execution.
Format: INPUT: - ("prompt"); variable list
Where
Prompt- is an optional literal string that is displayed before the users enter data. A semi colon
after appends a question mark to the prompt string e.g. INPUT A, B, C, where A, B, C, are all
value to input their values. INPUT A$, B#, C$, D this is calls for input of the during literal
numeric value, another string literal and another numeric is that order separated by commas. The
4 input constant value or values must be entered in the same line separated by commas.
12
DATA constant (, constant)...
Where constant = one or more numeric or string constants specifying the data to be read.
Line: the label or line of a DATA statement
1. READ A, B, C NAMES$
DATA 5, 3, 2 " SAIDU"
This assign 5 to A, 3 to B, 2 to C and SAIDU to variable NAMES$
The LET statement
This is for assignments. Before the LET statement is used, there must exist a variable that must
have been assigned a value. The LET statement operates right to left. That is, the value on the
right of the equal sign is assigned to the variable on the left of the equal sign. The equal sign
means an assignment and not equality i.e. LET A = 20, which could be interpreted as
"LET the value in A be replaced by 20",
"LET SUM = A+B+C, hence the current values of A, B, C are added together and the result
assigned to the variable SUM"
The OUTPUT statement in Basic
PRINT statement: - these statements allows a programmer to specifies accurately an edited
format for printing numeric and string values which send output to the screen.
Syntax
PRINT syntax
PRINT (item list) i.e. PRINT A, B
Where A, B are item list which may be composed of string variables and numeric variable name
are separated by commas or semi colons.
e.g. "PRINT STUDENT NAME"
This will cause to sends STUDENT NAME output to the screen.
LPRINT statement: - this statement allows programmer to specify accurately an edited format
for printing numeric and string value through the line printer. Before the program is run the user
should make sure that the printer is ON, the general syntax.
LPRINT syntax
LPRINT (item list)
I.e. LPRINT A, B, C
Where A,b and C are item list which may be composed of string variables and numeric e.g. 10
13
LPRINT "STUDENT RECORD"
This will cause the hard copy of STUDENT RESULT to be produced
END STATEMENT
This statement end a program, procedure, blocks, or user defined data type. If no argument is
supplied. END statement ends or terminates the execution of a program and closed all open files.
Your First Program
Now, its time to write your first QBasic program. This program will display "Hello,
world!" on the screen.
QBasic is a DOS based program. To run it you have to open a DOS box. Click:
The Qbasic icon on your windows desktop.
A QBasic editor will be opened for you.
Enter the following program.
To run this Qbasic program, press Shift-F5 (or choose Run|Start from the Qbasic
menu).
CLS
PRINT "Hello, world!"
END
When you run this, a blank screen will appear and the two words will appear.
Observe that QBasic displayed Press any key to continue. at the bottom of the screen.
QBasic does this when a program ends. Let's understand this simple program by
looking at each individual line to see what's going on.
This line is a comment. All comments in QBasic begin with an ' (apostrophe) or the
keyword REM followed by a space. So, all this line does is tell us what the heck the
program is.
14
QBASIC
CLS – Clear Screen
This line is a command. If you know a little DOS, you probably already know this. The CLS
command clears the screen. I used this to get rid of everything before we wrote the text.
PRINT "Hello, world!"
This is another command. As you can probably guess, PRINT displays text on the screen
at the current cursor position. Following the PRINT keyword is a literal constant, the text
to display. You can PRINT just about anything.
END
This line, easily enough, ends the program.
REM Command
Syntax: {REM | '} comment
The REM command lets you add a comment to your code. As the syntax definition shows,
you can use an apostrophe (') in place of the word REM. comment can be anything you want
Example: ' let's Learn QBasic, its fun!
CLS Command
Syntax: CLS
The simple CLS command clears everything off of the screen and puts the cursor at the
top left corner of the screen. Example: CLS
PRINT Command
Syntax: PRINT [expression {; | ,} expression {; | ,} ...] [{; | ,}]
The PRINT command is used to put text on the screen at the current cursor position. The
syntax will take some explaining. expression can be any string or number expression.
Examples: PRINT
PRINT " Name", "SSN" PRINT "My name is. . . . "; myname$;
END Command
Syntax: END
The END command quits the program and returns to the QBasic editor. Example: END
QBASIC
15
Now that you know all about logical and relational operators, we'll put them to use. There
are two main commands you can use with conditionals: IF and SELECT CASE. Both are
similar, but are better suited to some things over others. Really, IF can be used wherever
SELECT CASE can, but not the other way. Let’s start with the more common one, IF.
IF Command (if-then-else --single-line form)
Syntax: IF condition THEN statement [ELSE statement]
The IF command in this form lets you execute a line depending on a condition. condition is
a True/False value. If condition is True (not 0), then the statement following THEN is
executed. If condition is False (0) and an ELSE clause is included, the statement following
ELSE is executed; or else, flow continues to the next line. This command is small and good
if you only need to do one thing based on a condition.
Example: IF guess% = 5 THEN PRINT "Good job!" ELSE PRINT "You stink!"
IF Command (If-Elseif-else -- block form)
Syntax:
IF condition THEN
<statements>
[ELSEIF condition THEN
<statements>
] ...
[ELSE
<statements>
]
END IF
As you can see, this form of IF is designed for both complex situations and large chunks of code.
First, the top condition is tested. If true, the code between it and the next ELSEIF, ELSE, or
END
IF is run. If it's false, the next ELSEIF clause is tested, and so on. If none of the clauses are true,
the ELSE's code is run. After going through any chunk of code, flow returns to after the END IF
line. The ELSE clause is optional, and you can have as many ELSEIF clauses as you
16
QBASIC
wish.
Let’s see a sample program that demonstrates the use of both forms of IF. This is a
number guessing game.
Example:
CLS
number% = 5
PRINT "Welcome to the game! Try to guess the number I'm thinking
of!" INPUT "Guess a number between 1 and 10:", guess% IF guess% <1
OR guess% > 10 THEN
PRINT "You guessed out of range!"
ELSEIF guess% = number% THEN
PRINT "Terrific! You guessed right on the money!"
ELSE
PRINT "Too bad. You missed the target."
INPUT "Do you want to know what it was (Y/N)"; see$ IF
see$ = "Y" THEN PRINT "The answer was"; number%
END IF
END
Note: In large programs you might have a number of blocks inside each other. It's easy to
forget the closing statements, and QBasic gives cryptic, confusing errors when you leave
a block or loop open. For example, you might get a "Block IF with no END IF" error
when in fact you forgot to close one of your loops. Even QBasic admits it.
Now, here's another way to do conditionals. This way is preferred when you are only
examining the value of one variable throughout the tests.
SELECT CASE Command
Syntax:
SELECT CASE expression
CASE {expression1 [, expression2, ...] | IS relational_operator expression
| expression1 TO expression2}
<statements>
17
[CASE (see choices above)
<statements>
] ...
[CASE ELSE
statements
]
END SELECT
This block statement looks at the value of the beginning expression. It checks the first CASE.
QBASIC
The expression1 [, expression2, ...] form checks to see if the value equals a certain value.
The IS relational_operator expression form checks to see how it relates to another value.
The expression1 TO expression2 checks to see if it is between (inclusively) two other
values. If it is found to be True, the following block of code is run. If none are found True,
the C ASE ELSE block (if it exisis) is run. After a block is run, the program continues after
the END SELECT keyword.
SELECT CASE is recommend when checking the value of one number, and IF when
using multiple variables in your tests. It's all personal preference, though. The next
program shows how SELECT CASE can be used.
Example:
'SELECT CASE demonstration
CLS
INPUT "Please enter your marks: ", marks%
SELECT CASE marks%
CASE < 60
PRINT "You have a D- grade."
CASE 61 TO 70
PRINT "You have a C- grade"
CASE 71 TO 80
PRINT "You got a B - grade."
18
CASE 100
PRINT "You did very well, Excellent"
CASE ELSE
PRINT "You failed."
END SELECT
END
Iteration -- Loops
Loops are the nice and easy solution if you wanted your program to do something repeatedly. All
of the loop constructions in QBasic execute a block of commands repeatedly 0 or more times.
FOR/NEXT Loop Construct
Syntax:
The FOR/NEXT loop iterates the commands a set number of times. You assign a numeric
variable as the counter. It changes value each iteration. The start and end values are the
first and last values counter will be. You can also specify the increment, which can be
positive or negative but not 0. If you omit it, it defaults to 1. The FOR/NEXT construct is
ideal for blocks of code you want to run n times.
Example:
FOR I = 1 TO 10 STEP2
'Write my name 5 times
QBASIC
PRINT “Sani”;
NEXT I
19