Uploaded by MOHAMAD ZUL FARISH BIN RASID FT21110044

CP0015 - Topic 1

advertisement
TOPIC 1: OVERVIEW OF
PROGRAMMING
LANGUAGES
CP0015 INTRODUCTION TO PROGRAMMING
LEARNING OUTCOMES
• At the end of this topic, students should be able to:
• Explain the basic concept of programming
• List the common types of programming languages
• Explain the three paradigm of programming; modular, structured
and object-oriented
INTRODUCTION
• A computer is an electronic device that is programmable for storing,
processing and retrieving data and information in binary form.
• Processing includes performing mathematical calculations, logic
operations, and decision-making based on pre-set condition.
• Computers have been used as devices for computing and making
logical decisions in place of humans.
INTRODUCTION
• The use of computers have increased in every field, not only for
work, but also for entertainment and education.
• Computers have evolved from big bulky machines, gradually reducing
in size until they become small and handy – supercomputers,
mainframes, desktop pc, laptops, tablets, smartphones, etc.
INTRODUCTION
• Most of household equipment are built
with computer chips, and software
embedded within them – washing
machine, microwave, television, etc.
INTRODUCTION
• A computer is capable of carrying out complex tasks. This is because of installed
programs.
• A computer program contains step-by-step instruction about what tasks to
perform and how to perform them – able to perform long, repetitive, boring tasks.
• Operating a computer is done through executing the computer programs:• Clicking a software icon
• Selecting a command from the menu
• Writing a computer command
PROGRAMMING LANGUAGE
• A program is a set of instructions executed by a computer in order to
produce a desired behaviour from the computer.
• The instructions cover one or more of the following operations:
• Read inputs
• Process data
• Make decisions
• Repeat tasks
• Display results
PROGRAMMING LANGUAGE
• A programming language can be defined as:
• A notation for the precise description of computer programs.
• A vocabulary and set of grammatical rules (syntax) for instructing a computer to perform specific
tasks.
• A programming language is needed because a computer is designed only to understand only
instructions written in programming language.
• C programming language is one of the most popular language invented in 1972 by Dennis Ritchie.
• C++, java and c# programming language were based on C
PROGRAMMING LANGUAGE
• A programmer is a person responsible for writing computer programs using a programming
language.
• Must be familiar with the vocabulary and syntax of a programming language.
• Vocabulary – set of commands
• Syntax – grammar of the programming language
• Computer programmer ≠ computer engineer.
• Computer engineers build computer hardware
• Computer programmer writes a program that runs on a computer hardware.
PROGRAMMING LANGUAGE
• Programming is a set of technical activities in the production of a program.
• writing, testing, debugging/troubleshooting, and maintaining the source code of computer programs.
• A programmer writes computer code in a source file – text file.
• A source file is not a program, therefore it cannot be executed by a computer.
• It must be converted into a file that can be understood by the computer.
• Two ways to do it:
• Compile the program
• Interpret the program
PROGRAMMING LANGUAGE
• A compiler reads the whole source code and translates it into a
complete machine code or object code to perform the required
tasks.
• An interpreter reads the source code one instruction or line at a
time, converts this line into machine code and executes it.
TYPES OF PROGRAMMING LANGUAGE
• Choosing a suitable programming language
is important.
• Early programming was done by punching
codes on a role of tape – very inefficient.
• Newer programming are easier for learning
and offer more capabilities.
TYPES OF PROGRAMMING LANGUAGE
• Generally there are two categories of languages:
• low-level languages – the code runs directly on the computer
hardware. The language follows specific instructions of the
hardware.
• high-level languages – the code is more abstract and the
representation is closer to human language. Not dependent on
the hardware.
TYPES OF PROGRAMMING LANGUAGE
• Comparison of low-level languages and high-level languages.
Low-level Language
High-level language
Uses binary, hexadecimal numbers or abbreviations
More English-like for easier reading
Many lines of code are needed to execute one basic
operation
One or two lines are sufficient to carry out a similar basic
operation
Used to write programs that control hardware, such
as a printer driver
Used to write programs that solve business or scientific
problems.
Very high speed execution
Relatively slower but has been compensated by optimization
nowadays
Machine-oriented. Need to know the details of the
hardware
Problem-oriented. Need to know about the procedure
TYPES OF PROGRAMMING LANGUAGE
• Machine language is a low-level language.
• Each instruction is represented by a binary code.
• The parameters which are used to store values, such as register number
and memory cell address, are also referred to using binary codes.
• Programs written by machine language are very efficient, but it depends on
specific hardware architecture instructions.
TYPES OF PROGRAMMING LANGUAGE
Example 1.1: The following machine code performs
c = a + b:
01
0c
01
0a
01
02
0b
02
Binary code
0b
0a
03
0c
03
Description
01
01
0A
Copy content of memory cell 01 to register A
01
02
0B
Copy content of memory cell 02 to register B
02
0B
0A
03
0C
03
04
0C
04
Add value of register A to register B, then store the result in
register C
Copy from register C to memory cell 03
Stop
TYPES OF PROGRAMMING LANGUAGE
• Because machine language was difficult, new languages were
introduced.
• New machine language was the first generation language.
• Improvements have been made several times as computer became
more sophisticated.
• There have been five generations of programming languages.
TYPES OF PROGRAMMING LANGUAGE
• First-generation language (1GL)
• Uses binary instructions in 0s and 1s. The coding is very long and difficult.
• Second-generation language (2GL)
• Uses assembly language, that is slightly higher than machine language by replacing binary with
abbreviations. Need to understand the hardware chipset instructions.
• Third generation language (3GL)
• Use procedural language. The first high-level language. Many contemporary programming
languages belong to this generation.
TYPES OF PROGRAMMING LANGUAGE
• Fourth-generation language (4GL)
• Uses friendlier English-like scripts to reduce overall time, effort and cost of
software development.
• Fifth-generation language (5GL)
• Uses problem constraints instead of algorithm. Mainly used to solve
problems in artificial intelligence.
TYPES OF PROGRAMMING LANGUAGE
• Assembly language is a second-generation language.
• Developed to make programming easier than using machine language.
• The instructions use English-like abbreviations called mnemonics
such as LOAD, ADD, STORE, HALT and are translated via assemblers.
TYPES OF PROGRAMMING LANGUAGE
Example 1.2: The following assembly
language performs c = a + b:
main proc
LOAD 01 0A
LOAD 02 0B
ADD 0A 0B 0C
STORE 0C 03
HALT
end main
Assembly code
Description
LOAD
01 0A
Copy content of memory cell 01 to register A
LOAD
02 0B
Copy content of memory cell 02 to register B
ADD
0B 0A 0C
Add value of register A to register B, then store the
result in register C
STORE 0C 03
Copy from register C to memory cell 03
HALT
Stop
TYPES OF PROGRAMMING LANGUAGE
• Third-generation languages (3GLs) are based on algorithms –
specification of every step of a task.
• Used to write software applications, software systems, software
drivers and embedded programs.
• C is a popular 3GL.
• Easier to write than assembly language, but must be written
according to a strict syntax.
TYPES OF PROGRAMMING LANGUAGE
Example 1.3: The following C code
performs c = a + b:
Code
line
1
To include a library called stdio.h that contains built in
functions such as reading inputs from keyboard or
displaying output on the computer screen.
2
It is read as ‘main function that returns an integer’.
Required in every program and it indicates the start of the
program’s execution. Curly brace { marks the beginning of
the program body.
3
Declares 3 variables A, B and C as of data type int.
4
Arithmetic operation of adding the value inside A to the
value inside B and storing the result in variable C.
5
Display the result on the computer screen.
6
Return statement required at the end of a main function.
7
Curly brace }marks the end of the program body.
#include <stdio.h>
int main(){
int A=1, B=2, C;
C = A + B;
printf(“C = %d ”, C);
return 0;
}
Description
TYPES OF PROGRAMMING LANGUAGE
• Fourth-generation languages (4GLs) are refined 3GLs.
• Designed for high productivity in order to reduce the overall time, effort
and cost of software development.
• Uses icons, symbols, graphical interfaces and automated environments to
minimalized manual programming.
• Normally built for database systems.
• Examples include PL/SQL, SPSS, Python and Perl.
TYPES OF PROGRAMMING LANGUAGE
Example 1.4: The following Python code performs
c = a + b:
A = 1
B = 2
C = A + B
print(“C = ”, C)
TYPES OF PROGRAMMING LANGUAGE
• Fifth-generation languages (5GLs) are language that solve problems
using constraints instead of algorithms.
• Used mainly in artificial intelligence applications.
• A fully working 5GL is non-existing.
PROGRAM DESIGN TECHNIQUES
• A very large program containing only one block of code is describe as monolithic.
• Very hard to maintain and reuse.
• Various software design techniques have been introduced to improve the structure of programs
for better understanding and efficiency, especially in large systems. The techniques:• Modular Programming
• Structured Programming
• Object Oriented Programming
• The goal of these techniques is to facilitate the construction of a large software programs and
system by decomposition into smaller pieces –modules, units, functions, objects, etc.
PROGRAM DESIGN TECHNIQUES
• Modular Programming refers to high-level
decomposition of the entire program into modules.
• Modules are differentiated by an independent set of
tasks or functions such as input/output, mathematical
process, or domain specific process.
• In Structured Programming approach, the modules are
functions.
• In Object-Oriented Programming approach, the
modules are objects.
Modular
Programming
Structured
Programming
ObjectOriented
Programming
PROGRAM DESIGN TECHNIQUES
• Structured Programming approach decomposes a program module
to a low-level code using structured control flow such as sequencing,
selection or iteration (loop).
Structure Elements
Sequencing
Description
Each step follows each other linearly
Selection
Use a condition to determine which alternative sequence
of code that the program should follow
Iteration
Instruct the program to repeat similar operations for
several times.
PROGRAM DESIGN TECHNIQUES
• Object-Oriented Programming approach is a newer design technique
introduced to support Modular Programming for component-based
system.
• Uses a new data structure called object.
• Focuses on objects and relationships instead of a list of steps that the
code required to do.
• Common Object-Oriented Programming languages are C++, Java and
C#.
PROGRAM DESIGN TECHNIQUES
• Comparison between Structured Programming and Object-Oriented Programming
Structured Programming
Object-Oriented Programming
Based on a list of commands executed in top-down
manner
Based on objects and their interactions
Limited reusability compared to object-oriented
programming
Improved productivity through object reusability. Programs
tend to be more understandable, better organized and easier
to maintain, modify or debug.
Commands directly change the data through direct
assessments and common data structures.
Data can only be changed through an object’s defined
methods. Uses concept such as information hiding and
message passing.
Programs run faster during execution.
Programs may run slower because of object creation.
Suitable for small programs.
Suitable for building complex systems.
SUMMARY
• Programming is an important aspect of computer literacy, allows the creation of programs that
allow the computers to carry out complex tasks.
• A program is a set of instructions executed by a computer in order to produce a desired
behaviour.
• To write a program, a programmer uses a programming language to write a code of the program.
• Programming languages have evolved from low-level machine language in binary to high-level
languages which are similar to English.
• Modular Programming decomposes a large program into many independent pieces.
• Structured Programming uses control flows.
• Object Oriented Programming focuses on the creation and the use of objects.
Download