COS201-Computer programming I - week 1
COS201-Computer programming I - week 1
1
COURSE OUTLINE
Essentials of computer programming
programming languages paradigms
Programming language basic variables, data types, and expression
Control statements
C++ structures, Arrays and union
Strings/string processing
Inheritance and polymorphism
WEEK ONE
3
Objectives – week one
Know the history of programming languages
High-level or low-level
Language translator Target code
language
8
Programming languages I– COS211
PROGRAMS AND PROGRAMMING LANGUAGES
Unlike assembly language, high-level languages such as C, and C++, Python supports notations closer to the
abstractions, such as algebraic expressions used in mathematics and science.
Example: The code for adding two numbers in C++ is:
int main(){ int x = 5;
int y = 6;
int sum = x+y;
cout<<sum<<endl;
return 0;}
The program requires the use of a language translator to translate it to machine code for execution – C++
compiler.
The program uses variables (storage locations in memory) to hold data and programs or instructions
Language Translators
Assembler
2nd Generation Languages 1st Generation Languages
(low-level Languages) (machine language)
Data Abstraction means behaviour and attributes of data, such as numbers, characters, strings, and search
trees.
Example of data abstraction: The use of symbolic names to hide locations in computer memory that contain
data variables.
Example { int x = 6; }
Such named locations are called variables and such variables must have a data type that shows the kind of
values it can hold.
Control abstraction simply means properties of the transfer of controls. That is the modification of the
execution path of a program based on the situation at hand.
The control abstraction shows the algebraic notation of the arithmetic and assignment expression
Example: { int x = 6 + 8 * 3; }
Control simply means mapping the solution of the problem based on the von Neumann machine where the
memory of the computer is continuously altered to derive a final state of computation that contains a solution.
Every instruction alters the state of computation to a new state.
Example:
int main(){
int x = 6;
x = x+9;
cout<<x<<endl;
return 0;}
The addition instruction changes the state of the variable x from x = 6 to x = 15 .
Syntax analyzer: The syntax analyzer generates the grammar rule of a natural language.
It defines the syntactic structure of the programming language.
The syntax for assignment statements in C++ is
<variable> = <Value>|<Expression>;
Example:
int main(){ int x = 6; int y = 4+5*6; cout<<x<<endl; return 0;}
23
Programming languages I– COS211
PROGRAMMING LANGUAGE PARADIGMS
Procedural programming is an imperative programming paradigm that uses a sequence of instructions that
may be contained within procedures, functions, or subroutines.
These instructions are carried out in a linear (step-by-step) or top-down manner.
It relies on procedures, subroutines, or functions to perform computations.
In procedural programming, a program consists of data and modules or procedures (functions) that operate on
the data (variable).
These two entities are treated separately.
25
Programming languages I– COS211
PROCEDURAL PROGRAMMING LANGUAGES
Procedural-oriented programming language is derived from structured programming and is based upon the
procedure call concept.
It divides a program into small procedures called subroutines, procedures, or functions.
Examples of procedural programming languages include C++, C, Fortran, Basic, Pascal etc
The advantages of procedural programming language is that it helps programmers to easily track the program
flow and code can be reused in different parts of the program
26
Programming languages I– COS211
PROCEDURAL PROGRAMMING LANGUAGE PARADIGMS (Example)
C++ function to calculate the sum of two integers
Functional programming is a programming paradigm that uses pure functions in sequence to solve complex
problems.
They are designed on the concept of mathematical functions that use conditional expression and recursion to
perform computation.
A pure function is a function that always returns the same output when called with the same argument value.
A pure function has no side effects like modifying an argument, or global variable, or outputting variables.
The only result of calling a pure function is the return value.
Void functions are functions that never produce any output
30
Programming languages I– COS211
PURE FUNCTIONS
A pure function returns the same value as output when called with the same arguments.
Example:
31
Programming languages I– COS211
FUNCTIONAL PROGRAMMING LANGUAGES
In functional programming language, functions are treated as first-class citizens, meaning that they can be
bound to names, including local identifiers, passed as arguments, and return value from other functions, just
as any other data type can.
Example:
32
Programming languages I– COS211
FUNCTIONAL PROGRAMMING LANGUAGES
Functional programming is a higher-order function if the program functions accept other functions as
arguments or return a function. It simply means a function that operates upon other functions.
Example of passing a function as argument. It has no side effects since it does not implicitly change the value
of the input.
33
Programming languages I– COS211
SCRIPTING PROGRAMMING LANGUAGES
The scripting language is a language where instructions are written for a run run-time environment.
They do not require the compilation step and are rather interpreted.
It brings new functions to applications and glues complex systems together.
A scripting language is a programming language designed for integrating and communicating with other
programming languages.
They are programming languages that support scripts
Scripts are short programs that automate tasks that could alternatively be executed one by one by a human
operator.
34
Programming languages I– COS211
SCRIPTING PROGRAMMING LANGUAGES
Different types of scripting languages include:
bash: It is a scripting language to work in the Linux interface. It is a lot easier to use bash to create scripts than other
programming languages. It describes the tools to use, code in the command line, create useful reusable scripts and conserve
documentation for other people to work with.
Node js: It is a framework for writing network applications using JavaScript. Corporate users of Node.js include IBM,
LinkedIn, Microsoft, Netflix, PayPal, Yahoo for real-time web applications.
Ruby: There are a lot of reasons to learn Ruby programming language. Ruby’s flexibility has allowed developers to create
innovative software. It is a scripting language which is great for web development.
Python: It is easy, free, and open source. It supports procedure-oriented programming and object-oriented programming.
Python is an interpreted language with dynamic semantics and huge lines of code that are scripted and is currently the most
hyped language among developers.
Perl: A scripting language with innovative features that make it different and popular. Found on all Windows and Linux
servers. It helps in text manipulation tasks. High-traffic websites that use Perl extensively include priceline.com, IMDB.
35
Programming languages I– COS211
SCRIPTING PROGRAMMING LANGUAGES
Advantages of scripting programming languages include:
Easy learning: The user can learn to code in scripting languages quickly, not much knowledge of web
technology is required.
Fast editing: It is highly efficient with a limited number of data structures and variables to use.
Interactivity: It helps in adding visualization interfaces and combinations in web pages. Modern web pages
demand the use of scripting languages. To create enhanced web pages, fascinating visual descriptions which
include background and foreground colors, and so on.
Functionality: There are different libraries that are part of different scripting languages. They help in creating
new applications in web browsers and are different from normal programming languages.
36
Programming languages I– COS211
SCRIPTING PROGRAMMING LANGUAGES
37
Programming languages I– COS211
LOGIC PROGRAMMING LANGUAGES
A logic programming paradigm defines the syntax and semantics of the facts, rules and queries, as well as the
inference mechanism that derives new facts and rules from existing ones.
A logic program is divided into three (3) sections:
A series of definitions/declarations that define the problem domain
Statements of relevant facts
Statement of goals in the form of queries
Logic programming is based on the idea of declarative programming which means that you write programs by
stating facts and rules, rather than instructions or steps.
38
Programming languages I– COS211
LOGIC PROGRAMMING LANGUAGES
Facts are statements that describe the state of the world; such as:
“Alice is a person”
“Bob likes pizza”
“X is Mortal”
Facts are simple statements that do contain a body clause. They express the core information about a domain.
The fact can take the form
“X is true”
“X is Y ” where Y is a statement about X
Example: (Real world example)
“Rex is a dog”
In symbolic logic, a fact only has a head named H and is expressed as follows:
H:
39
Programming languages I– COS211
LOGIC PROGRAMMING LANGUAGES
Rule(s): F T F
F F F
If x is a person then x is mortal
40
Programming languages I– COS211
LOGIC PROGRAMMING LANGUAGES
41
Programming languages I– COS211
LOGIC PROGRAMMING LANGUAGES
Facts and rules together form a logic program that can be used to answer queries or solve problems.
One of the most popular and influential logic programming languages is Prolog
Prolog stands for Programming in Logic
A logic program is a variation of declarative programming based on a type of formal logic called predicate
calculus.
Declarative language describes what the program should do but not how to do it.
Predicates can be classified as either facts or rules. They must have a head component and can also have a
body.
Predicate →facts or rules
Must have → {head and body}
42
Programming languages I– COS211
43
THANK YOU
FOR
LISTENING
THE END