Principles of Programming Prof Appiahene
Principles of Programming Prof Appiahene
OF PROGRAMMING
Prof. Peter Appiahene
BSc Information Technology
Level 100 A, B, C & D
Introduction to Programming
01 Definitions, Processes of programming
Algorithms
03 Definition, Properties, Characteristics, Guidelines, examples
Flowcharts
05 Deep dive into flowcharts
Programming?
Writing computer codes(set of instructions) that tells a computer what to do and how to do it.
The goal of computer programming is to find a set of instructions that will allow a computer to solve a given
problem (Martin, 2021).
Computer Program
Programs are a collection of instructions that tell a computer what to do and how to do it.
Alarm Auto-Reply
Reminder
Dr. Peter Appiahene
The Art of Problem Solving
Following the steps above, let’s see how to solve some common life problems.
o An algorithm is a finite series of instructions telling a computer how to transform a set of facts
about the world into useful information.
o In programming we usually refer to the sequence of actions which the computer follows to
execute a particular task as a programming algorithm
o An algorithm works by having your computer perform a series of prescribed actions that
describe how to do something, and your computer will execute it the same way every time.
o A programming algorithm can be thought of as a recipe for the computer to follow to solve a
problem or achieve a goal.
• Finiteness: An algorithm should have a finite number of steps and it should end after a finite time.
• Definiteness: Each step must be clear, well-defined and precise. There should be no ambiguity.
• Effectiveness: Each step must be simple and should take a finite amount of time, and must do what
its intended for.
1. It should be straightforward.
4. You can begin coding the answer only after it has some
structure.
5. Calculating the weekly wage of an employee given hours worked and hourly rate
Natural Language
Pseudocodes Flowcharts
Algorithms
These algorithms are typically written in a way that is easily understandable by humans and may
serve as a high-level description before being translated into a specific programming language.
Here are some key points regarding the representation of natural language algorithms:
1. Pseudocode will be enclosed by START (or BEGIN) and STOP (or END).
3. To accept data from the user, generally used statements are INPUT, READ,
GET or OBTAIN.
Note:
• You can move only 1 piece at a time
• You can not place a larger piece on a smaller one
Dr. Peter Appiahene
Examples of Pseudocodes
1. BEGIN
2. NUMBER firstnumber, secondnumber, sum
3. OUTPUT("Input first number:")
4. INPUT firstnumber
5. OUTPUT("Input number2:")
6. INPUT secondnumber
7. sum=firstnumber+secondnumber
8. OUTPUT sum
9. END
Dr. Peter Appiahene
Examples of Pseudocodes
• A flowchart often displays the processes as various types of boxes, with arrows
connecting them in the correct order.
EXAMPLE OF A FLOWCHAT
FLOWCHAT SYMBOLS
• Process
• A box indicates some particular
operation/process. It can represent a
single step ("add two cups of flour"),
or an entire sub-process ("make
bread") within a larger process.
DOCUMENT
• Document
• This represents a printout, such as a
document or a report. This symbol,
for example, could be used to show
a document input, such as receiving
an email or a report. It can also be
used to symbolize the result of a
document, such as a presentation or
a project
DECISION
• Decision
• A diamond represents a
decision or branching point.
Lines coming out from the
diamond indicate different
possible situations, leading
to different sub-processes.
DATA
• Data
• It represents information
entering or leaving the
system. An input might be an
order from a customer.
Output can be a product to be
delivered.
FLOW
• Flow
using System;
class Program
float b, h, area ;
b =5 ;
h = 8;
area = ( b * h) / 2;
}}
CHAPTER THREE
• Programming Language
• This language is the most understandable language used by computers to perform their
operations. The program instructions written in these languages are in binary form.
Procedural-Oriented Language
HIGH-LEVEL
PROGRAMMING Logic Programming Language
LANGUAGES
Object-Oriented Programming
Problem-Oriented Language
• This programming executes a series of
PROCEDURAL statements that lead to an outcome. Usually, this
ORIENTED
kind of programming uses heavy loops, multiple
variables, and some other elements, which is also
LANGUAGE a major difference between procedural and
functional languages.
Its emphasis is on the
algorithm.
CHARACTERISTICS
OF PROCEDURAL The function can
PROGRAMMING communicate by a global
LANGUAGE variable.
• MOV a, R1
• ADD #2, R1
• MOV R1, b
ROLEOF AN ASSEMBLER
COMPILERS
• It also reports to its users the presence of errors in the source program.
C++ uses a compiler.
ROLE OF A COMPILER
• A source code must go through several steps before it becomes an executable program.
• In the first step, the source code is checked for any syntax errors.
• After the syntax errors are traced out a source file is passed through a compiler which first
translates high-level language into object code (A machine code not ready to be executed).
• A linker then links the object code with pre-compiled library functions, thus creating an
executable program.
• This executable program is then loaded into the memory for execution.
• The general compilation process is shown in Figure below:
Preprocessing
STEPS OF Compiling
COMPILATION
Assembling
Linking
DIFFERENCE BETWEEN AN INTERPRETER AND A
COMPILER
• The interpreter translates just one statement of the program at a time into machine code
whereas a compiler scans the entire program and translates the whole of it into machine
code at once.
• An interpreter takes very less time to analyze the source code. However, the overall time
to execute the process is much slower. A compiler takes a lot of time to analyze the source
code. However, the overall time taken to execute the process is much faster.
• An interpreter does not generate an intermediary code. Hence, an
interpreter is highly efficient in terms of its memory. A compiler
always generates an intermediary object code. It will need further
linking. Hence more memory is needed.
DIFFERENCE • Interpreter Keeps translating the program continuously till the
• What are two advantages of writing computer programs using a high-level language?
• Compiler
• interpreter
• C++ is a programming language created by Bjarne
Stroustrup and his team at Bell Laboratories in 1979.
Forty years later, it has become one of the most widely
used languages in the world; we can find C++
applications everywhere, from the bottom of the oceans
PROGRAMMING to the surface of Mars.
WITH C++ • As the name implies, C++ was derived from the C
language; Bjarne’s goal was to add object-oriented
programming into C, a language well-respected for its
portability and low-level functionality.
WHY C++?
● It is fast and flexible.
● It is well-supported.
● C++ programs are stored in files that usually have the file extension .cpp, which simply
stands for “C plus plus”.
STRUCTURE
OF A C++
PROGRAM
PROGRAMMING WITH PYTHON
• Python is a widely-used general-purpose, high-level programming language.
• It was initially designed by Guido van Rossum in 1991 and developed by Python
Software Foundation.
• It was mainly developed for emphasis on code readability, and its syntax allows
programmers to express concepts in fewer lines of code.
WHY PYTHON?
Simple and easy to use.
• There are also more uses of python for a growing range of applications from machine
learning, statistical sciences, data sciences and many more
TYPICAL PYTHON PROGRAM
• # Example 1
OPERATIONS
information processing system. Inputs are the
signals or data received by the system and outputs
are the signals or data sent from it.
I/O
OPERATIONS
CPU-(MEMORY )
Programs that are loaded into memory has two areas that is,
Machine instructions – the instructions for what we want the computer to do for us.
Data storage – the variables that we are using in our program.
Often programs contains instructions to interact with the input/output devices.
Data is moved into (read) and/or out of (write) the memory data area.
A device is a piece of equipment that is electronically connected to the memory so
that data can be transferred between the memory and the device.
INPUT-PROCESS-OUTPUT MODEL
Variables and data types form part of the integral part of all programming
languages. This is because variables serve as storage locations in memory to hold
the values of our data during programming whereas data types serve as attributes
used in specifying what kind of data (value) we can store in our variables.
VARIABLES
Integers
Character
A character variable contains a single letter, number or special character, such as $,
% or @. The value of a character variable is always enclosed in quotes.
Examples: “A”, “G”, “*”, “8”
Note that the character “8”, which is declared as a character, is not regarded as
numeric and may not be used in calculations. When declared as a character, it can be
used as a code – for instance, the group number is “8”.
Non-Numeric Data Types.
String
A string variable consists of two or more characters and must also be enclosed in
quotes.
Examples: “32 Long Street” or a message, such as “The name is found.”
Boolean
A Boolean value can only contain a true or false value. In an everyday life
situation, a Boolean value can be compared with a light switch, which is either on or
off. In a computer language, a true value is represented by a non-zero value, whereas a
false value is represented by zero.
ARITHMETIC AND LOGICAL
OPERATORS
^ Exponentiation 2^5 32 1
Integer division can only be done when dividing an integer value by another.
Integer value because it discards (drops) the decimal part and doesn’t round the
answer. Example: 27 \ 10 = 2
Modulus arithmetic is done when the user only wants to know what the value of
the remainder is when dividing. Example: 52 mod 10 = 2
Note that when operators with the same precedence occur in one expression, they
will be executed from left to right.
ARITHMETIC OPERATORS
&& Logical Returns true if both statements are x < 5 && x < 10
and true
! Logical not Reverses the statement, returns false !(x < 5 && x < 10)
if the result is true
CONTROL STRUCTURES
Loop statements” are nothing more than the automation of multi-step processes by
organizing sequences of actions and grouping the parts that need to be repeated.
In general terms, there are two types of “Looping techniques”:
“For Loops”: are the ones that execute for a prescribed number of times, as
controlled by a counter or an index.
“While Loops” and “Repeated Loops”: are based on the onset and verification of
a logical condition. The condition is tested at the start or the end of the loop
construct.
FOR LOOP
In this Control Structure, statements are executed one after another in consecutive
order over a sequence of values that gets evaluated only when the “For Loop” is
initiated (never re-evaluated).
In this case, the number of iterations is fixed and known in advance. A for loop
enables a particular set of conditions to be executed repeatedly until a condition is
satisfied.
STRUCTURE
OF FOR
LOOP
WHILE LOOP
The do while loop checks the condition at the end of the loop. This
means that the statements inside the loop body will be executed at
least once even if the condition is never true. The syntax for the do
while loop is as follows
do {
/* statement(s); */
The do while loop checks the condition at the end of the loop. This
means that the statements inside the loop body will be executed at
least once even if the condition is never true. The syntax for the do
while loop is as follows
do {
/* statement(s); */