0% found this document useful (0 votes)
2 views43 pages

Basic Computer Programming 1-1

The document outlines the course content for a Basic Computer Programming curriculum at Kasama College of Education, focusing on programming languages, structures, data types, and specific languages such as Pascal, C++, and Java. It explains the concepts of high-level and low-level programming languages, compilers, interpreters, and the structure of Pascal programs, including variables and data types. Additionally, it provides examples of programming tasks and activities to reinforce learning.

Uploaded by

wyntermusondawm
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views43 pages

Basic Computer Programming 1-1

The document outlines the course content for a Basic Computer Programming curriculum at Kasama College of Education, focusing on programming languages, structures, data types, and specific languages such as Pascal, C++, and Java. It explains the concepts of high-level and low-level programming languages, compilers, interpreters, and the structure of Pascal programs, including variables and data types. Additionally, it provides examples of programming tasks and activities to reinforce learning.

Uploaded by

wyntermusondawm
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

KASAMA COLLEGE OF EDUCATION

BASIC COMPUTER PROGRAMMING

SECONDARY TEACHERS’ DIPLOMA


COURSE CONTENT
1. Programming languages
2. Program structures
3. Programming data types
4. Pascal programming
5. Programming in Pascal
6. Programming in C++
7. Programming in Java
8. Object Oriented Programming using Visual Basic
WHAT IS COMPUTER PROGRAMMING?
Computer Programming:
• The process of developing and implementing
various sets of instructions to enable a computer to
do a certain task.
Computer Programming requires patience and dedication.
PROGRAMMING LANGUAGES
• A programming language is a vocabulary and set of
grammatical rules for instructing a computer or
computing device to perform specific tasks.
• There are mainly two classes of programming
languages
(a)High Level Programming languages
(b)Low Level Programming languages
HIGH-LEVEL LANGUAGE
• High-level language is a computer programming language
that is designed for a specific job and is easier to
understand by humans. Computers do not understand
this language.
• It is more like human language and less like machine
language.
• However, for a computer to understand and run a
program created with a high-level language, it must be
compiled into machine language using a Compiler.
• There are many high-level languages in use, some include
BASIC, C, C++, Cobol, Fortran, Java, Pascal, PHP, HTML,
Ruby, Python, and Visual Basic.
LOW LEVEL LANGUAGE
• Low-level programming language is a programming
language that provides little or no abstraction from a
computer's instruction set.
• Generally this refers to either machine code or
assembly language.
• Low-level languages are mainly used when writing
Device Drivers or Embedded Software.
• Low level languages are always static, and never have
garbage collection (memory management).
• One example of a low level language is Assembly
Language.
• Assembly is a mnemonic-based low-level language
replacing binary machine-code instructions.
Machine Language
• Sometimes referred to as machine code or object code.
• Machine language is a collection of binary digits or bits
that the computer reads and interprets.
• Machine language is the only language a computer is
capable of understanding.
• Machine language is a Low Level programming language.
COMPILER
 It is a program that translates the instructions of higher-
level language into machine code.
 It is called compiler because it compiles every program
instruction given in higher-level languages into machine
language.
 It scans the entire program first and then translates it into
machine code.
 The program written by the programmer in higher-level
language is called source program or source code.
 After this program is converted into machine language by
the compiler it is called object code.
ASSEMBLER
• The assembler is used to convert Assembly code into
machine code.
• Assemblers are usually very fast to convert the source
code into executable code because assembly language is
very close to computer language.
INTERPRETER
• An interpreter is another type of program translator used
for translating higher-level language instructions into
machine language instructions.
• It takes one statement of higher-level language at a time,
translates it into machine language and executes it
immediately.
• Translation and execution are carried out for each
statement. It differs from compiler, which translate the
entire source program into machine code and then involve
in its execution.
• Thus compiled machine language program runs much
faster than an interpreted program.
HIGH LEVEL Vs LOW LEVEL LANGUAGES
HIGH LEVEL LANGUAGES LOW LEVEL LANGUAGES
Easily understood by humans. Easily understood by machines.
Appropriate for developing Appropriate for developing
user-friendly applications. operating systems or firmware
codes for micro-controllers.

Slow as they have a lot of Fast as they are close to


abstraction and layers of code machine code and little
before reaching hardware. abstraction.

Creates software that can run Code written for one platform
on multiple platforms and is impossible to run on another.
architecture. (Portable). (Not Portable).
Program to add two numbers using different levels
Operation: 83 + -2 = 81
1. MACHINE LANGUAGE
Location Instruction Code Instruction Instruction Comments
Hex Binary Code
Hex

100 0010 0001 0000 0100 2104 LDA 104 Load 1st operand into AC
101 0001 0001 0000 0101 1105 ADD 105 Add 2nd operand to AC
102 0011 0001 0000 0110 3106 STA 106 Store sum in location 106
103 0111 0000 0000 0001 7001 HLT Halt computer
104 0000 0000 0101 0011 0053 operand 83 decimal
105 1111 1111 1111 1110 FFFE operand -2 decimal
106 0000 0000 0000 0000 0000 operand Store sum here
2. ASSEMBLY LANGUAGE
ORG 100 /Origin of program is location 100
LDA A /Load operand from location A
ADD B /Add operation from location B
STA C /Store sum in location C
HLT /Halt computer
A, DEC 83 /Decimal operand
B, DEC –2 /Decimal operand
C, DEC 0 /Sum stored in location C
END
3. HIGH LEVEL LANGUAGE (Java)

int a, b, c;
a = 83;
b = -2;
c = a + b;
Uses of Computer Programs
Computer programs are used in almost every field,
household, agriculture, medical, entertainment,
defense, communication, etc.
A few applications of computer programs:
 MS Word, MS Excel, Adobe Photoshop, Internet
Explorer, Chrome, etc., are examples of computer
programs.
 Computer programs are used to develop graphics
and special effects in movie making.
 Computer programs are used to perform ultrasounds,
X-Rays, and other medical examinations.
 Computer programs are used in our mobile phones
for SMS, Chat, and voice communication.
Activity 1
Differentiate between
(a) Source program and object program
(b) Higher level language and machine language
(c) Compiler and Interpreter
VARIABLES
• Variables are memory locations that are generated
in a computer’s memory that are dedicated to store
a specific type of data to be used in a program.
• A variable is identified and distinguished from other
variables by an Identifier.
• An Identifier is a sequence of one or more letters,
digits or underscore characters ( _ ) that can be
used to represent a variable.
• Neither spaces nor punctuation marks nor symbols
can be part of an identifier.
• Variable identifiers always have to begin with a
letter and must not match any reserved keyword.
DATA TYPES
• Unlike humans, a computer does not know the difference
between "1234" and "abcd."
• A data type is a classification of the type of data that
a variable or object can hold in computer programming.
• Data types are an important factor in virtually all computer
programming languages,
• Most often the term is used in connection with static typing
of variables in programming languages like C/C++, Java and
C# etc, where the type of a variable is known at compile
time.
• Data types must be referenced and used correctly, to
ensure the proper result and an error-free program.
Common examples of data types are:
1. Boolean (e.g., True or False)
2. Character (e.g., a) (8 bit)
3. Date (e.g., 03/01/2016)
4. Double (e.g., 1.79769313486232E308)
5. Floating-point number (e.g., 1.234)
6. Integer (e.g., 1234) (32 bit)
7. Long (e.g., 123456789) (64 bits)
8. Short (e.g., 0) (16 bits)
9. String (e.g., James)
10. Void (e.g., no data)
• Understanding the different data types allows
programmers to design computer applications more
PASCAL
PROGRAMMING
What is Pascal

 Pascal is a general-purpose, high-level language that


was originally developed by Niklaus Wirth in the early
1970s.
 The Pascal language was named for Blaise Pascal,
French mathematician and pioneer in computer
development
 It was developed for teaching programming as a
systematic discipline and to develop reliable and
efficient programs.
 Pascal is a case non-sensitive language.
WHAT IS COMPUTER PROGRAMMING?
Structure of Pascal program

program {name of the program}


uses {comma delimited names of libraries you use}
const {global constant declaration block}
var {global variable declaration block}
begin { main program block starts} ...
end. { the end of main program block }
PASCAL VARIABLE TYPES

• A variable is a user defined name that represents


memory location in a computer that a program can
manipulate.
• Variables are declared using the var word and the data
type is specified.
• The basic data types are : String, Integer, Boolean, Char,
Real.
• The name of a variable can be composed of letters,
digits, and the underscore character. It must begin with
either a letter or an underscore.
• Pascal is not case-sensitive, so uppercase and lowercase
letters mean same here.
Variable Types
Type Description
Char Holds single characters (letters/symbols)
enclosed in single quotes, e.g. 'a' 'B' '+‘.
Integer Contains integers from -32768 to 32767
Real A single-precision floating point value
ranging from 3.4x10-38 to 3.4x1038.
Boolean Specifies true or false logical values. It
has only two values, TRUE or FALSE.
String Stores an array of characters (text
words).
Subrange Represents variables, whose values lie
within a range.
Enumerated Specifies a user-defined list.
PASCAL RESERVED WORDS

• The statements in Pascal are designed with some


specific Pascal words, which are called the reserved
words.
• Reserved words have some meaning of action attached
to them.
• These words can not be used as variables as they will
tell the program to perform an action instead of
representing memory locations.
LIST OF RESERVED WORDS

and array begin case const


div do downto else end

file for function goto if

in label mod nil not


of or packed procedure program
record repeat set then to

type until Var while with


Pascal Program creation
• A program in Pascal starts with the reserved word
'Program' and ends with 'End', followed with a full stop.
• A full-stop is never used within the program, except when
dealing with records and at the end of the program.
• To display any message on the screen, the word 'write' (or
'writeln') is used.
• The 'readln' statement is used to 'stop' the program and
wait until the user presses enter key (or any key) on the
keyboard.
• If the 'readln' statement is missing in the program, then
the message is displayed on the screen without giving any
chance for the user to read it and obviously halts!.
FIRST PROGRAM

This program will display on the screen the statement


‘Hello World. Prepare to learn PASCAL!!’
Open the Pascal IDE. Type the following code. Debug the
program.
• Messages enclosed between the braces, { }, are
called comments or in-line documentation.
• Comments within the braces are not read or compiled by
the compiler/interpreter.
• A error is detected by the Operating System if something
goes wrong with the input.
• The main two types of errors are - Runtime Errors and
Compilation Errors.
• Runtime errors are those which occur unexpectedly
during the execution of the program.
• Compilation error is one which is detected during the
compilation process.
• The following program is written showing an example of a
string variable, prompting the user to input his/her name,
whatsoever:
• The variables 'name' and 'surname' are not reserved
words, but are used by the programmer as variables.
Program Full_Name;
Var name, surname: String;
Begin
Write('Enter your first name:');
readln(name);
Write('Enter your surname:');
readln(surname);
writeln;{new line}
writeln;{new line}
Writeln('Your full name is: ',name,' ',surname);
Readln;
End.
Constants and the Assignment Statement
• Constants are memory locations that keep their value or
string unchanged for the whole program.
• A constant is assigned a value or string using the
assignment sign (:=). Note the full colon, :, it must be used.
• The statement assigns what ever is on the right hand side
of the := to the variable name on the left hand side.
• The declaration of a constant is done using the word
‘const’.
• The following program shows the use of a string constant.
Program Name_Full;
Var surname: String;
Const name := 'Victor';
Begin
Write('Enter your surname:');
readln(surname);
Writeln('Your full name is: ',name,' ',surname);
Readln;
End.
Arithmetic Operators in Pascal
• These are symbols or phrases that tell the program to
perform a specific mathematical calculation and return a
value as the answer.
• Some of the arithmetic operators are:
Logical Operators
 They allow a program to make a decision based on
multiple conditions.
 Each operand is considered a condition that can be
evaluated to a true or false value.
ACTIVITY 2
Write the source code in your book then test them on the
computer.
1. Write a program in Pascal that can add two values in US
Dollars and convert sum from US Dollar currency to
Kwacha, when the exchange rate is K9.50 per US Dollar.
Use the program to answer the following questions.
(a) A car is bought at a selling price of $3,500, the duty
and tax on the car amount to $1,850. What is the total
cost of the car in Kwacha?
2. Write a program in Pascal that can convert distance in
miles to kilometers. 1 mile = 1.61 kilometers.
3. Write a program in Pascal that can calculate the area of a
right-angled triangle. A = 1/2bh
ACTIVITY 3
1. Give the meaning of each of the following terminologies
in Pascal Programming.
(a) Begin
(b) End
(c) Write
(d) Writeln
(e) Readln
(f) Var
(g) Const
(h) ;
(i) Program

You might also like