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

COS201-Computer programming I - week 1

Computer programming

Uploaded by

lordfade24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
35 views43 pages

COS201-Computer programming I - week 1

Computer programming

Uploaded by

lordfade24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 43

COURSE LECTURER(S)

1
COURSE OUTLINE
 Essentials of computer programming
 programming languages paradigms
 Programming language basic variables, data types, and expression

 Operators and Statements

 Control statements
 C++ structures, Arrays and union

 Strings/string processing
 Inheritance and polymorphism

COMPUTER PROGRAMMING I– COS211 2


ESSENTIALS OF COMPUTER PROGRAMMING

Ashioba Nwanze C. (PhD)

WEEK ONE
3
Objectives – week one
 Know the history of programming languages

 Differentiate between programs and programming languages

 Identifies programming language paradigms

 Implement the different paradigms

COMPUTER PROGRAMMING I– COS211 4


WHAT IS PROGRAMMING ?
 Computer programming is the process of designing and writing computer programs.
 A program is a step-by-step sequence of instructions or commands telling a computer to perform a specific task.
 It is a sequence of meaningful symbols used to specify the solution to a complex problem
 For this program to be executed by the machine, it must be translated from one form to another using a language
translator.

High-level or low-level
Language translator Target code
language

COMPUTER PROGRAMMING I– COS211 5


WHAT IS PROGRAMMING ?
 Example: A program to find the area of a rectangle
 Step 1: Start
 Step 2: Enter the input parameters – length and width
 Step 3: Compute the Area: Area = length x width
 Step 4: Display the Area
 Step 5: Stop

COMPUTER PROGRAMMING I– COS211 6


WHAT IS PROGRAMMING ?
 Example: A program to display “Hello World”
 Step 1: Start
 Step 2: Enter the input parameters – “Hello World” as a string
 Step 4: Display the string “Hello World”
 Step 5: Stop

COMPUTER PROGRAMMING I– COS211 7


WHAT IS PROGRAMMING LANGUAGE?

 A programming language is a system of notation for writing computer programs.


 it is a set of computer languages used by programmers to write computer programs
 A programming language is a vocabulary and a set of grammatical rules for instructing a computer
or computing device to perform a specific task.
 A programming language is a set of symbols, grammar, and rules used to translate or convert
algorithms to programs that the computer execute.
 Programmers communicate with a hardware machine using a programming language.

8
Programming languages I– COS211
PROGRAMS AND PROGRAMMING LANGUAGES

Generations of Programming Languages


3rd Generation 4th Generation Languages
Languages (High-level language like Python)
(High-level language)

Generation of 5th Generation Languages


Computer Languages (Prolog and Mercury)

2 nd Generation Languages 1st Generation


(low-level Languages) Languages
(Machine language)

COMPUTER PROGRAMMING I– COS211 9


PROGRAMS AND PROGRAMMING LANGUAGES
 The primary classifications of programming languages are:
 Machine language
 Low-level language
 High-level language
 In machine code or language, each line of code contains 16 bits or binary digits.
 A line represents either a single machine language instruction or a single data value
 Example: 6 is written as 0000 0000 0000 0110
 Machine language also called machine code or object code is difficult to read and write because it is usually
displayed in binary or hexadecimal codes.
 It does not require a language translator to translate or convert programs to machine code. This is because it
is the natural language of every machine and computers directly understand the machine language program.

COMPUTER PROGRAMMING I– COS211 10


PROGRAMS AND PROGRAMMING LANGUAGES
 Assembly language (ASM) or low-level language is a language designed for a particular processor.
 It uses mnemonic symbols for instructions instead of binary digits used by machine language
 It allows programmers to introduce names for blocks of memory that hold data.
 It uses an assembler to convert or translate the assembly language to machine code
 Example:
 Add pay, total; meaning Total = pay + total instead of using binary code like 1011110111000011.
 The instructions are translated to binary code and after translation, another program called a loader,
automatically loads the machine code for this instruction into a computer memory .

COMPUTER PROGRAMMING I– COS211 11


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

COMPUTER PROGRAMMING I– COS211 12


LANGUAGE TRANSLATORS
 For a programming language to be useful, it must have a translator.
 A translator is a program that accepts other programs written in the language in question and either executes
them directly or transforms them into a form suitable for execution.
 The different types of language translators include:
 Assembler
 Interpreter
 Compiler
 Hybrid (combination of interpreter and compiler)
 A translator that executes a program directly is called an interpreter while a translator that produces an
equivalent program in a form suitable for execution is called a compiler.

COMPUTER PROGRAMMING I– COS211 13


LANGUAGE TRANSLATORS
 Compilation is a two-step process
 The source code is input to the compiler and a new program (target program) is output from the compiler.
 The target program may be executed, if it is in a form suitable for direct execution (machine language) or may
be translated by an assembler into an object program, linked with other object programs, and loaded into the
computer memory location for execution.
 Hybrid compiler like Java compiler: which compiles and interprets high-level language to machine code

Interpreter (virtual machine)


Compiler

Java Byte code Object code


code

COMPUTER PROGRAMMING I– COS211 14


TYPES OF LANGUAGE TRANSLATORS

Language Translators

3rd Generation Languages 4th Generation Languages


(High-level language) (High-level language like Python)

Language Translator system


5th Generation Languages
(Prolog and Mercury)

Assembler
2nd Generation Languages 1st Generation Languages
(low-level Languages) (machine language)

COMPUTER PROGRAMMING I– COS211 15


COMPONENTS OF A PROGRAM
 A program has three (3) major components:
 Logic + abstraction + control
 Logic means coming up with a high-level specification of a solution to a problem.
 It requires repeated breaking up of a complex problem into a structured combination of simpler problems, and
combining the solutions of these simpler problems using well-defined operations to compute the final solution.
 Abstraction means modeling an entity by the desired attributes needed to solve the problem at hand.
 The entity may have many attributes; however, all the attributes may not be needed to model the solution of the
problem.
 The advantage of abstraction is that programs are easily comprehended and easily modified resulting in ease of
program maintenance.
 Programming language abstraction falls into two (2) categories:
 Data abstraction
 Control abstraction
16
COMPUTER PROGRAMMING I– COS211
COMPONENTS OF A PROGRAM

 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; }

COMPUTER PROGRAMMING I– COS211 17


COMPONENTS OF A PROGRAM

 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 .

COMPUTER PROGRAMMING I– COS211 18


LANGUAGE DEFINITION
 Language definition are loosely divided into three parts:
 Lexical analyzer
 Syntax or structure
 Semantics or meaning
 The lexical analyzer tokenizes the words or lexeme in a program into token classes (tokens).
 The token classes include identifiers, operators, delimiters, keywords or reserved words, numbers, strings, literals
etc.
lexeme Token class
 Example: the statement { int x = 6; } is analyze as:
int keyword
x Identifier
6 Number (integer)
; Delimiter or terminator

COMPUTER PROGRAMMING I– COS211 19


LANGUAGE DEFINITION

 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;}

COMPUTER PROGRAMMING I– COS211 20


LANGUAGE DEFINITION
 Semantics analyzer: gives meaning to the program. A program may be syntactically correct but semantically
wrong.
 Example: int main() {int y = 4+5*6; cout<<y<<endl; return 0;}
 The semantic correctness of a program depends on the user specification.
 The operator’s precedence rule is used to eliminate the ambiguity of an expression.
 Example:
 int main {int y = 4+5*6; cout<<y<<endl; return 0;} gives y = 34 and
 int main() {int y = (4+5)*6; cout<<y<<endl; return 0;} gives 54

COMPUTER PROGRAMMING I– COS211 21


PROGRAMMING LANGUAGE PARADIGMS
 Programming language paradigms are different ways or styles in which a given program or programming
language can be organized.
 It is an approach to solving problems using some programming languages.
 A paradigm is a way or approach to classifying programming languages based on their features, patterns,
coding styles, framework, etc., to solve a particular problem.
 In simple terms, it is defined as a way of thinking and structuring the functionalities of a program.

COMPUTER PROGRAMMING I– COS211 22


PROGRAMMING LANGUAGE PARADIGMS
 Popular programming language paradigms include:
 Imperative programming languages (Algorithmic programming languages)
 Procedural programming languages
 Functional programming languages
 Declarative programming languages
 Object-Oriented programming languages
 Scripting programming language
 Logic programming language

23
Programming languages I– COS211
PROGRAMMING LANGUAGE PARADIGMS

COMPUTER PROGRAMMING I– COS211 24


PROCEDURAL PROGRAMMING LANGUAGES

 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

COMPUTER PROGRAMMING I– COS211 27


PROCEDURAL PROGRAMMING LANGUAGE PARADIGMS (Example)
 C++ functions to calculate the difference of two square

COMPUTER PROGRAMMING I– COS211 28


PROCEDURAL PROGRAMMING LANGUAGE PARADIGMS (Example)
 C function to calculate the sum of two integers

COMPUTER PROGRAMMING I– COS211 29


FUNCTIONAL PROGRAMMING LANGUAGES

 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:

Function creation Function call


int sum(int x, int y){ int main(){

return x+ y; int a = 5; int b = 7;


int z = sum(a,b);
} int p = sum(a,b);
cout<<z<<p<<endl;
return 0;

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.

Function creation Function call


int sum(int x, int y){ int main(){
int a = 5; int b = 7;
return x+ y; int z = sum(a,b);
} int p = sum(a,point(b));
int point(int y){ cout<<z<<p<<endl;
return y; return 0;
} }

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

 Applications of scripting programming languages include:


 Scripting languages are used in web applications. It is used on the server side as well as the client side.
Server-side scripting languages are: JavaScript, PHP, Perl etc. and client-side scripting languages are:
JavaScript, AJAX, jQuery etc.
 Scripting languages are used in system administration. For example: Shell, Perl, Python scripts etc.
 It is used in Game applications and Multimedia.
 It is used to create plugins and extensions for existing applications.

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

 Rules, also called axioms, are logical clauses


 Rules describe the circumstance under which a relationship is valid
 They are statements that define relationships or conditions between facts; such as: if (fact) Then (fact)
 A rule contains a head and a body and takes the form:
 “X is true if Y and X are true”
 Facts: X Y X and Y
 “Alice is a person” T T T
 “X is Mortal” T F F

 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

 “X is true if Y and X are true”


 The Facts: “X is true” section forms the head of the clause while the “if Y and X are true” portion forms the body.
 A rule containing head (H) and body clauses B1, B2, …….., Bn can be expressed symbolically using the following
notation.
 H: B1, B2, …….., Bn
 In the simplest case, the head and all body components are definite clauses. This means, they are atomic and do
not contain any sub-clauses or connective components.
 However, negations of definite clauses are still allowed; such as
 If B = “X is Y”
 ~B = “X is not Y”
 Some implementations also permit “if and only if or iff clauses”

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

You might also like