Sei sulla pagina 1di 10

COMPUTER

APPLICATION

Compiled by: Osaigbovo Timothy


CHAPTER ONE
TYPES OF COMPUTER PROGRAMMING LANGUAGE AND TRANSLATORS
WHAT IS A LANGUAGE?
It is a system of communication between you and me. Some of the basic natural languages that
we are familiar with are English, Hindi, Oriya etc. These are the languages used to communicate

1
among various categories of persons. Your computer will not understand any of these natural
languages for transfer of data and instruction. So there are programming languages specially
developed so that you could pass your data and instructions to the computer to do specific job.
You must have heard names like FORTRAN, BASIC, COBOL etc. These are programming
languages. So instructions or programs are written in a particular language based on the type of
job.
PROGRAMMING LANGUAGES
There are two major types of programming languages. These are Low Level Languages and High
Level Languages. Low Level languages are further divided in to Machine language and Assembly
language.
Low Level Languages
The term low level means closeness to the way in which the machine has been built. Low level
languages are machine oriented.
(a) Machine Language
Machine Language is the only language that is directly understood by the computer. It does not
needs any translator program. We also call it machine code and it is written as strings of 1's (one)
and 0’s (zero).For example, a program instruction may look like this: 1011000111101.It is not an
easy language for you to learn because of its difficult to understand. It is efficient for the
computer but very inefficient for programmers. It is considered to the first generation language.
(b) Assembly Language
It is the first step to improve the programming structure. You should know that computer can
handle numbers and letter. Therefore some combination of letters can be used to substitute for
number of machine codes.
The set of symbols and letters forms the Assembly Language and a translator program is required
to translate the Assembly Language to machine language. This translator program is called
`Assembler'. It is considered to be a second-generation language.
HIGH LEVEL LANGUAGES
High level languages are simple languages that use English and mathematical symbols like +, -, %, /
etc. for its program construction.
High level languages are problem-oriented languages because the instructions are suitable for solving
a particular problem. There are mathematical oriented languages like FORTRAN (Formula
Translation) and BASIC (Beginners All-purpose Symbolic Instruction Code) where very large
processing is required.
LANGUAGE TRANSLATOR
ASSEMBLER
An assembler is a program which is used to translate an assembly language program into machine-
level equivalent. The program in assembly language is termed as source code & its machine language
equivalent is called object code.
COMPILER
It is a program translator that translates the instruction of a high level language to machine language.
It scans the entire program first and then translates it into machine code. The programs written by the
programmer in high level language is called source program or code. After this program is converted
to machine languages by the compiler it is called object program or code.
Higher Level Language --> (Compile) ---> Program --> Machine Language Program

INTERPRETER
An interpreter is another type of program translator used for translating high level language into
machine language. It takes one statement of high level languages, translate it into machine language
and immediately execute it. Translation and execution are carried out for each statement. The
advantage of interpreter compared to compiler is its fast response to changes in source program. It

2
eliminates the need for a separate compilation after changes to each program. The disadvantage of
interpreter is that it is time consuming method because each time a statement in a program is executed
then it is first translated. Thus compiled machine language program runs much faster than an
interpreted program.

PROGRAMMING STRUCTURE
Monolithic programming structure is the method where a program is coded and run as a whole.
Modular programming structure is made up of independent units: closed coding and restricted
entry and exit. Each module can be coded and tested, compiled and run separately.

COMPUTER PROGRAMMING WITH QBASIC


Steps in Program Development
1. Clearly State the Problem
2. Plan the Logic of the program
3. Code the program
4. Key the program
5. Test and Debug the program.
6. Complete the Documentation
BASIC stands for Begginers All purpose Symbolic Instruction Code.
Qbasic Character Set
 Letters: a-z and A-Z
 Digits: 0-9
 Blank: the space character ( )
 Special characters: + - * / \ = < > . , ’ ” ( ) : ; ^ _ $ # ? ! % &
Qbasic Lines and Keywords
A program consists of lines of text. The order of the lines in the program is the order in which the
lines are read by the computer. The lines in the program contain statements which tell the
computer what to do. Each statement begins with a keyword.
A keyword has a predefined meaning within Qbasic. E.g
LET,END,REM,PRINT,CLS,INPUT,READ,DATA,IF,GOTO,etc

CONSTANTS & VARIABLES


Numerical constants are written in the usual way, with or without a decimal point, and with or
without a minus sign:
7, -54, 3.14159, -0.0065.
Numerical constants may also be written with a scale factor, which is a power of 10. The scale
factor is the letter E followed by a positive or negative integer, for example:
2.75E4 means 27500, 2.75E-3 means 0.00275.
Symbolic constants are constants with a name defined by the keyword CONST, as in the
following example:
CONST Pi = 3.142
String constants are enclosed in quote marks. They may be used for printing messages during the
running of a program. The string constant in the above example is:

3
"Enter X and Y".
A variable is the name of a location in the computer memory where a number or a string is stored.
In the example above X, Y, and Dist are numerical variables.
A numerical variable consists of one or more letters, and may also contain digits. Two types of
numerical variable are commonly used: integers (no decimal point), and single-precision numbers
(with a decimal point and up to seven significant digits).
If the last character in the variable name is %, then the number stored is an integer. If the last
character in the variable name is !, then the number stored is a single-precision number. For
example,
n% is an integer variable
x! is a single-precision variable.
You may declare numerical variable types without % or ! by using the keywords DIM, AS, and
INTEGER or SINGLE as follows:
DIM VariableName AS INTEGER
DIM VariableName AS SINGLE
A string variable is the name of a location in the computer memory where a string is stored. You
may declare a variable to be a string by ending its name with the character $. For example, A$ is a
string variable. You may also declare a string variable as follows:
DIM VariableName AS STRING
Rules for NAMING Variables in QBasic:
1. The first character must always be a letter (i.e.  A,G,D,F,z,e,t,y, ...)
2. After the very first letter in a variable name you can have letters, digits or underscores.
3. The final character MAY either be %,&,!,#,$ or nothing.
4. Remember that variable's name can not be reserved words.
For example, in QBasic there is a statement called "PRINT", you must not use this or any
other reserved words for a variable name.
5. In QBasic, it does not matter if your variable names are either UPPERCASE or lowercase.
Assignment Statements
Syntax: [LET] variable = expression
Assignment statements contain the keyword LET, a numerical variable, the symbol =, and a
mathematical expression. For example, the line LET X = A + 2.54 tells the computer to
calculate A + 2.54 and store the result in the location named X. The keyword LET is optional: it
may be omitted.
The CLS statement
CLear Screen:erases all characters from the terminal
The PRINT statement
Writes information to the terminal. Syntax: PRINT output-list
PRINT X$ displays the string or word(s) stored in the location X
PRINT “Hello World” displays Hello World
The INPUT statement
Syntax: INPUT [;] [literalstring$ {; | ,}] var [, var, ...]
The INPUT command allows you to read characters from the keyboard and stores the information
in a variable. For example, if you wanted to input a number into a variable named "choice", you
would use the QBasic code ...
INPUT choice
For example, if you wanted to input a string into a variable named "name$", you would use the
QBasic code ...
INPUT name$
The DATA and READ statements
To save space, and typing, QBASIC provides the DATA and READ statements. You can place a

4
list of data values in a DATA statement and read them into an array using READ. The READ
statement is the command used to access the DATA. When the computer sees a READ command,
it looks for the first DATA statement in the program.
Examples
REM Read and Data statements SAMPLE OUTPUT
READ x, y, z X=10
PRINT " x="; x Y=20
PRINT " y="; y Z=30
PRINT " z="; z sum=60
PRINT "sum= "; x + y + z
DATA 10,20,30
END
REM stands for REMark.
Q-Basic knows that when it sees REM it should just ignore what you wrote after it on that line.
Always leave a space after you write REM.Here is an example:
REM This program is by Tim
QBASIC Data Types
All data in QBASIC is identified by a data type
Numbers
 % – Integer -32,768 to 32,767
 & – Long integer -2,147,483,648 to 2,147,483,647
 ! – Single precision 7 digit (1038 ) (Default data type)
 # – Double precision 15 digit (10308 )

Strings:
 $ - data type identifier
 Any set of characters enclosed in double quotation marks. “ “
Arithmetic Operators
 – Multiplication
 ^ – Exponentiation
 / – Division
 \ – Integer Division
 MOD – Modula (remainder)
 + – Addition
- – Subtraction
Calculations
Order of operations (precedence)
 ^ (Power)
 *, /, \, MOD, +, - (Arithmetic)
 <, <=, >, >=, <>, = (Relational)
 Boolean operators
 NOT, AND, OR, XOR, EQV, IMP
Boolean Operators
 NOT – negation
 AND – logical addition
 OR – logical subtraction

NOT Boolean Truth Tables AND Boolean TruthTables OR Boolean Truth Tables

5
Functions
A function is a set of instructions that perform a specific task.
 FunctionName (argument1, …)
 Built-in & User defined
X$ = INT(X)
Built-in Functions
The following functions may be used in mathematical expressions: ABS(X) : Absolute value of
X. SQR(X) : Square root of X. EXP(X) : Exponential X. LOG(X) : Natural logarithm of X.
Use SQR to find the "square root" of a number. Output:
PRINT SQR(1) 1
PRINT SQR(4) 2
PRINT SQR(9) 3
PRINT SQR(16) 4
PRINT SQR(25) 5
BRANCHING
Branching is a set of control statements that instruct the computer to execute a set of statements
under certain conditions and or execute another set under different condition.
Unconditional Branching Statements
Syntax: GOTO Line No
GOTO makes QBasic run lines of commands in an order different from how they are written.
GOTO requires that a line label be implemented at some point in the code as a destination.In
QBasic, the line label can be either a number, a word, or a combination of the two as long as it is
unique within the program.
Conditional Branching Statements
Syntax: IF condition THEN statement [ELSE statement]
IF...Then...Else
The If... Then statement is a statement which is used when a decision is required. It should be
used with the ELSE statement even if it is 'empty'. This promotes good coding procedures and
avoids pitfalls later on.
REM IF...THEN statements
INPUT "What is your name"; NAM$
INPUT "what is your age "; AGE
IF AGE < 18 THEN
PRINT NAM$; " you cannot vote."
ELSE
PRINT NAM$; " you can vote if registered."
END IF
END Please note how the IF... THEN... ELSE statements are aligned and INDENTED!
ITERATION -- LOOPING
Looping are set of control statements that instruct the computer to repeat the execution of certain
procedures in a program several times.
FOR/NEXT Loop Construct
Syntax: FOR counter = start TO end [STEP increment] NEXT [counter]
For / Next Statements
These are a way of creating a loop which is executed a fixed number of times. A loop
(repitition)is a part of a program which is executed more than once.
Example 1:
Write a looping program that prints all integers from 1 to 1000000
Solution
10 REM PROGRAM TO PRINT ALL INTEGERS FROM 1 TO 1000000

6
20 FOR X=1 TO 1000000 STEP 2
30 PRINT X
40 NEXT X
50 END
Example 2
Write a program that numbers from 1 to 5 and display the numbers and their squares.
Solution
10 CLS Output
20 REM for/Next statements Number Square
30 PRINT "Number", "Square" 1 1
40 FOR number = 1 TO 5 2 4
50 PRINT number, number ^ 2 3 9
60 NEXT number 4 16
70 END 5 25
ARRAYS
An array is a list of variables of the same type. To create an array, use the DIM (dimension)
command. Arrays Hold Collections of Variables. An array is like having a variable with more
than one compartment to store one piece of data in each. The elements in an array variable can
only be of the same data type. The size of the array has to be set when the array variable is
declare. you can re-dimension arrays, Each element in the array has a unique address - it’s name
plus it’s element number.
There are two types of arrays namely: One dimensional and two dimentional arrays.
asName(1) = “Fred”
asName(2) = “Julie” (1) Fred
asName (2)
asName(3) = “Kim” Julie
asName(4) = “John” Kim
(3)
asName(5) = “Chris” John
(4)
(5) Chris
Array name element number data
stored in that element
Defining an Array
We use the standard keywords used to declare variables:
Dim
We need to say what the size of the array is when we declare it
Dim aiCounters(14) As Integer
Dim asNames(5) As String
We need to be able to address the individual elements in an array.We use the array name and the
element number to access it asName
Dim asName(5) As String
asName(2) = “Pete”
asName(3) = “Lucy” (1) “Jane”
(2) “Pete”
INPUT “enter a name “; asName(4) (3) “Lucy”
How big is an Array (4) “Dave”
Dim sNames(2) As String will create an array that has 3(5) “Ian”
elements
sName(0)
sName(1)
sName(2)

Lowest element
Note:Be careful! By default QBasic sets the lowest array element as 0, so in our case this means

7
we have three elements
Suppose we want to store a series of names in an array.
0 1 2 3
John Jill Gordon Louise
The design:
1. for 0 to 3 names
2. input name
3. next name
The code
DIM asNames(3) AS STRING
DIM iCount AS INTEGER
FOR iCount = 0 TO 3
INPUT “ Enter a name > ”; asNames(iCount)
NEXT iCount
Lets extend this to store the names of four families

1 family
2 3 4

member

John Jill Gordon Louise


Bill Sue Rita Chris
Cyril Ranjit Celine Ken
June Peter David Alison
We need a multi-dimensional array to store the family names. Each column belongs to one
family. The design
For families 0 to 3
1.1 for family member 0 to 3
1.1.1 get name
1.2 next family member
2. Next family
The code
DIM asName(3,3) AS STRING
DIM iFamName AS INTEGER
Dim iFamMember AS INTEGER
FOR iFamName = 0 TO 3
PRINT “enter the names of this family “
FOR iFamMember = 0 TO 3

8
INPUT asName(iFamName, iFamMember)
NEXT iFamMember
NEXT iFamName
SELF CHECK 1
1.Determine the output of the following programming segment:
10 READ A,B,C,D
20 LET E=A+B**C/D
30 PRINT E
40 DATA 10,10,2,5
50 END
2.Write the output of the following BASIC program below:
10 PRINT “NOT A DO OR DIE AFFAIR”
20 PRINT
30 REM I WILL TRY MY BEST
40 W=13
50 Y=200
60 X=Y-(W*15)
70 Q=25
80 IF X<=Q THEN PRINT “I AM THROUGH”ELSE PRINT “ONE MORE LEVEL TO
CROSS”
90 PRINT X,Y,W*10
100 END

3.Write a program to produce a set of squares, cubes and square roots of all integers from 1 to
100.

4.Write a BASIC program that inputs an employees Basic salary and performs the following
computations: Housing allowance=50% of basic salary
Utility allowance=15% of basic salary
Transport allowance=20% of basic salary
Take home pay=basic salary+housing+utility+transport.
The program should output the take home pay.

5.Write a BASIC program to compute the gross pay and the net pay of ten employees. The
number of hours worked, the hourly pay rate and the tax should be embedded in the program. The
output should clearly state the result.(Note: Gross pay is the number of hours worked multiplied
by the hourly rate: Net pay is gross pay minus tax: Tax is 500)

6a.Using the rules of operator precedence, evaluate the following expressions:


K=(((2**2)+9*3-6)/2-3)

6b.Given the expression C=3X-4YZ2.Write a program to compute the value of C.

Potrebbero piacerti anche