Ict 155 Intro To Programming
Ict 155 Intro To Programming
Computer Programming:
Programming is the art of writing lines of
instructions (sometimes referred to as codes)
that tell the computer how to solve a problem or
carry out a task.
Computer Programme:
A computer programme is a set of step-by-step
instructions that tell a computer how to solve a
problem or carry out a task.
R D Appiah
Coding:
Coding refers to entering a list of commands
that become a computer programme.
R D Appiah
Examples of PLs:
Basic Fortran
C FORTH
C++ LISP
Pascal APL
Java Visual Basic
COBOL C# (pron. C sharp)
8088 Assembly Smalltalk
R D Appiah
Categories of PLs:
Criteria 1. Programming Languages can be
divided into two: Low-Level and High-Level
Languages.
Criteria 2. Programming Languages can
also be categorized by generations: First,
Second, Third, Forth and Fifth Generations.
Criteria 3. Programming Languages are
also categorized by paradigms: Procedural,
Object-Oriented, Declarative, Functional &
Event-Driven Paradigms.
R D Appiah
Low-Level Languages: These require a
programmer to write instructions for specific
computer hardware elements like the
processor, registers and the RAM. Instructions
here are written in 0s and 1s (i.e. in binary).
High-Level Languages: These make the
programming process easier by replacing
difficult commands in low-level programming
with easy-to-understand commands such as
PRINT and WRITE.
Also, multiple low-level commands are replaced
by a single high-level command.
R D Appiah
Generations of Languages
R D Appiah
Second Generation of PLs: Here,
programmers use abbreviated command words
rather than 1s and 0s in machine languages (an
improvement over First Generation of PLs.
Assembly Languages are examples of Second
Generation of PLs.
Third Generation of PLs: This marked the
entry point into the high-level programming era.
They use easy-to-remember command words
for instructions. Examples include COBOL and
Fortran, which were used for business and
scientific applications.
R D Appiah
Forth Generation of PLs: In an attempt to come
out with high-level languages which more closely
resemble human languages or natural languages,
than the third generation languages do, scientist
developed the Forth-Generation of PLs. Example is
the SQL (Structured Query Language).
R D Appiah
Programming Paradigm Types:
R D Appiah
Object-Oriented & Event-Driven
Paradigm: These formulate programmes
as a series of objects and methods that
interact to perform a specific task.
R D Appiah
Declarative Paradigm: This focuses on
the use of facts and rules to describe a
problem.
R D Appiah
Functional Paradigm: This emphasizes
the evaluation of expressions, called
functions.
R D Appiah
TRANSLATION.
• Programmes today are normally written in one
of the high-level languages.
• To run the programme on a computer, the
programme needs to be translated into the
machine language of the computer (ie the
target computer/ platform/ machine) on which it
will run.
• The programme in a high-level language is
called the source programme or source
codes.
R D Appiah
• The translated programme in machine
language is called object programme.
METHODS OF TRANSLATION
• Compilation and
• Interpretation.
R D Appiah
Compilation: A compiler normally
translates the entire source programme into the
object programme – before executing it.
Source Object
File File
R D Appiah
Lexical Analyzer:
A lexical analyzer reads the source codes,
symbol by symbol, and creates a list of tokens
in the source language.
R D Appiah
Semantic Analyzer:
R D Appiah
Code Generator:
R D Appiah
Algorithm:
Pseudocodes:
Is an English-language-like representation of an
algorithm.
R D Appiah
Flowchart:
• Flowchart is a graphical representation of the
way a computer should progress from one
instruction to the next when it performs a task.
R D Appiah
A sample algorithm is as follows:
1. Assume that:
R D Appiah
A sample algorithm:
1. Let ft and inch be the components of the
length in feet and inches respectively.
2. Display: Prompt the user to enter the
distance in feet and inches as integers.
R D Appiah
Identifiers
Most computer programmes are made up of
featured data items and elements, usually
referred to as objects.
In order to uniquely recognize and control these
objects in programming settings, objects are
assigned names, through identifiers.
Identifiers, thus, allow a programmer to name
objects in a programme.
This also helps the compiler to keep track of
where objects are physically located.
R D Appiah
Data Types
A data type defines a set of values and a set of
operations that can be applied to those values.
The set of values for each type is known as the
domain for that particular data type.
At the highest level, most programming
languages define two categories of data types:
R D Appiah
Composite Data Types
A composite data type is a set of elements in
which each element is a simple type or a
composite type.
Most languages define the following composite
types:
1. An array: A set of elements each
of the same type.
2. A record: A set of elements in
which the elements can be of different
types.
R D Appiah
Variables
Variables are names for memory locations.
Every memory location in a computer has a
unique address, called memory address;
which the computer uses internally.
Importance of Variables
1. Physically, the programmer does not know
the relative addresses of data items in
memory and through variables, the
programmer gets indirect access to the
computer’s memory resources.
R D Appiah
2. Secondly, a data item may occupy more than
one location in memory, which makes it
inconvenient for a programmer to control -
and through variables, such internal details
are hidden from the programmer.
R D Appiah
Normally, variables are created and managed
so as to hold data items of a particular type.
Variable Declaration.
As part of the process of creating variables,
variable declaration alerts the computer that
a variable with a given name and type will be
used in the programme.
In response, the computer reserves the
required storage area and names it
accordingly.
R D Appiah
Example: The following illustrates the
declaration of the variables response, num
and result:-
1. In C, C++ and Java:
char response;
int num;
double result;
2. In Visual Basic:
Dim response As String
Dim num As Integer
Dim result As Double
R D Appiah
In the C, C++ and Java example, response is
declared as of type character; num of type
integer and result of type real
R D Appiah
Variable Initialization
This is the process whereby declared
variables are assigned values before they are
used in the programme.
R D Appiah
Consider an example in a C, or C++
programme: The tax rate to be utilized in a
programme can be defined at the beginning
and used during the programme.
Illustration:
const float taxRate = 2.09;
. . .
cost = price * taxRate;
Note also that a constant, like a variable, has a
type and must be defined when the constant is
declared.
R D Appiah
Input and Output
Input:
Data is input by either a statement or a
predefined function.
scanf(“%d”, &num)
R D Appiah
• When the programme encounters this
instruction, it waits for the user to type an
integer.
Expressions
Operator:
An operator is a language-specific token that
requires an action to be taken.
R D Appiah
Examples include:
Arithmetic Operators in C, C++ and Java
Operato Definition Example
r
+ Addition 3+4
- Subtraction 4-5
* Multiplication Num * 4
/ Division Sum/count
% Modulo Operator 7%2
++ Increment (adds 1 to the value) Count ++
-- Decrement (subtract 1 from the Count --
R D Appiah value)
Relational Operators in C, C++ and Java:
They are used to compare data to see if a
value is greater than, less than, or equal to
another.
Operato Definition Example
r
< Less than Num1 < 4
<= Less than or equal to Num 1 <= 4
> Greater than Num 2 > 8
>= Greater than or equal to Num2 >= 8
== Equal to Num1 == num2
!= Not equal to Count != num2
R D Appiah
Logical Operators:
They combine Boolean values (true or false) to
get a new value.
The C language uses the following three
logical operators:
• Assignment statements
• Compound statements
• Control statements
R D Appiah
Assignment Statements
An assignment statement assigns a value to a
variable.
We can also say that an assignment statement
stores the value in the variable, which has
already been created in the declaration section
of a programme
In algorithms, we can use the symbol
to define assignment.
R D Appiah
Most programming languages like C, C++, and
Java use the symbol = for assignment.
Other programming languages, like Ada and
Pascal, the symbol := is used for assignment.
The following illustrates the declaration
followed by assignment statements involving
variables and operators in C and C++:
int count, x, y;
char ch;
ch = ‘Y’;
count = x + 34;
R D Appiah
count = (x + y)/10;
Compound Statements
In programming, a compound statement is a
unit of code consisting of zero or more
statements.
A compound statement is also known as a
block.
A compound statement allows a group of
statements to be treated as a single unit.
Normally, a compound statement consist of an
opening brace, a statement section,
followed by a closing brace.
R D Appiah
Illustration:
if( x == 1)
{
x++;
y = x + 24;
}
R D Appiah
Control Statements
Control statements are statements that specify
the sequence in which a programme is
executed.
Most programming languages have three
types of control statements:
1. Sequence Control Statement
R D Appiah
Start
GOTO mySkip
mySkip
End
R D Appiah
Selection Control Statements
These are used to tell the computer what to
do, based on whether a condition is TRUE
or FALSE.
A simple example of a selection control
structure is the IF … THEN … ELSE
command.
Example: Consider the following programme
construct, in which a selection control is
used to test a number – and based on the
outcome, a particular output statement is
printed on the screen:
R D Appiah
Input “Enter a number from 1 to 40: ”; num
If num > 10 Then
Print “The number is greater than 10.”
Else
Print “The number is 10 or less.”
End
NO Is
YES
number
> 10
End
R D Appiah
Repetition Control Statements
These are used to direct the computer to
repeat one or more instructions until a
certain condition is met or satisfied.
The section of code that repeats is usually
referred to as a loop or an iteration.
R D Appiah
Start Illustrating a loop in
programming
START LOOP
NO LOOP
DONE ?
YES
R D Appiah
End
Example1 using the FOR … NEXT Loop
For N =1 To 10
Print “There is no place like home”
Next N
End
Above is a simple programme in basic using
the For … Next command to print the
message “There is no place like home” ten
times.
R D Appiah
Example2 using the FOR … NEXT Loop
scores(0) = 23
scores(4) = 90
scores(10) = 44
R D Appiah
Subprogrammes
Generally speaking, this is a very important
concept more in procedure-oriented
programming paradigms than it is in object-
oriented programming paradigms.
In procedure-oriented programming, the main
computer programme is divided into “smaller”
programmes, referred to as subprogrammes.
Each subprogramme is so designed to handle
or to solve a specific problem – which
represents a well-defined task.
R D Appiah
Thus, a subprogramme is considered to be a
smaller programme within a bigger/ main
programme, to perform a specific task.
Some advantages of subprogrammes:
1. They make programming easier
2. They help in making a programme more
organized and structured
3. They help programmers to avoid repeating
writing same source codes over and over
again.
4. Also, detecting errors in a subprogramme is
easier than in the main programme.
R D Appiah
Recursion
This is a programming concept and technique
used mostly in advanced programming.
Recursion occurs in a computer programme if
the programme calls itself within its own
structure or definition in handling a task.
R D Appiah