0% found this document useful (0 votes)
3 views

Computer Programminglesson1 - Copy

The document provides an overview of computers, programming, and programming languages, explaining the computer information processing cycle and the distinction between system and application software. It details various programming languages, including machine, assembly, and high-level languages, along with their advantages and disadvantages. Additionally, it covers the roles of compilers and interpreters in translating high-level code into machine language for execution.

Uploaded by

masachibwatoh
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Computer Programminglesson1 - Copy

The document provides an overview of computers, programming, and programming languages, explaining the computer information processing cycle and the distinction between system and application software. It details various programming languages, including machine, assembly, and high-level languages, along with their advantages and disadvantages. Additionally, it covers the roles of compilers and interpreters in translating high-level code into machine language for execution.

Uploaded by

masachibwatoh
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 89

UNIT2 COMPUTER

PROGRAMMING

LESSON1
What is a Computer?
An electronic device that:
•accepts data (input),
•manipulates the data
(process),
•provides output (output) and
•stores the results (storage).
HE COMPUTER INFORMATION
ROCESSING CYCLE
COMPUTER
PROGRAMMING?
Is the process of developing and
implementing various sets of instructions
to enable a computer to do a certain task.

These instructions are considered computer


programs and help the computer to operate
smoothly.


WHAT IS A COMPUTER
PROGRAM?
A computer program is a
sequence of instructions
written using a Computer
Programming Language
to perform a specified
task by the computer.
WHAT IS A Computer
Program?
A computer program is a collection
of step-by-step instructions that tell
the computer how to perform a
particular task or group of related
tasks.
PROGRAM IS ALSO KNOWN AS

SOFTWARE
MAIN TYPES OF
Software(PROGRAMS)
There are two general types
of software.
•System software &
•Application software
SYSTEM SOFTWARE:
Operating Systems
The two operating systems most
users encounter are Microsoft
Windows and Mac OS X.

Operating systems contain the


instructions that allow the parts of a
computer to work together by
performing most, if not all, of the
“behind the scenes” tasks such
SYSTEM SOFTWARE:
Device
Device Drivers
drivers contain the
instructions which tell the computer
how to interact with that particular
device.
Many device drivers come
preinstalled on the operating
system but devices such as printers
and scanners come with a device
driver to install on the computer if
necessary.
Application Software
Software the computer uses to
carry out a specific task as specified
by the user.
Word Processing
Software that enables the user to
enter, edit, format and print
documents containing primarily, but
not limited to text.
Spreadsheets
Software that enables the user to
enter, edit, format, print, sort and
calculate primarily numeric data.
Computer Programming
Languages:

A programming language is an
artificial language that can be used to
control the behavior of a machine,
particularly a computer.

 Programming languages, like human languages,


are defined through the use of syntactic and
semantic rules, to determine structure and
meaning respectively.
Programing languages
A PL is
A special purpose and limited
language
A set of rules and symbols
used to construct a
computer program
A language used to interact
with the computer
Computer Programming
Languages (Contd…):

Programming languages are


used to facilitate
communication about the task
of organizing and
manipulating information, and
to express algorithms
precisely.
Computer Programming
Languages (Contd…):
In order for a computer to be able to execute a
program, the program must first be present in
binary form within the computer's memory.

However, writing a program directly in binary


form is completely out of the question.

 The solution to this conflict is for the


programmer to write a program in a more
suitable language and then to use a special
computer program to translate it into the
binary machine code necessary for the target
computer.
Levels of Programming
Languages

Programming languages are


said to be "lower" or "higher,"
depending on how close they
are to the language the
computer itself uses (Os and 1s
= low) or to the language
people use (more English-like-
high).
Levels of Programming
Languages
We will consider five levels of language.
In terms of ease of use and capabilities,
each generation is an improvement over its
predecessors.
The five generations of languages are:
Machine language
Assembly languages
High-level languages
Very high-level languages
Natural languages
Machine Languages, Assembly
Languages, and High-level Languages
 Three types of
programming languages
Machine languages
Strings of numbers giving
machine specific instructions
Example:
+1300042774 (these would really
be in binary)
+1400593419
+1200274027
20
 2000 Prentice Hall, Inc. All rights
Machine language:
 While easily understood by computers,
machine languages are almost impossible for
humans to use because they consist entirely of
numbers.

For example, an x86/IA-32 processor can


execute the following binary instruction as
expressed in machine language:

Binary: 10110000 01100001 (Hexadecimal: 0xb061)


Machine language:
A computer is a binary machine.

The signals used by the various parts of the


machine to communicate with each other must
be in binary form.
A program shown in binary form (or in hex for
ease of reading) so as to reflect these values is
said to be in machine language.

Machine language is the most basic and
elementary form in which any program can exist
Machine Language :
Machine Language is the only language that is
directly understood by the computer.
 It does not need any translator program.
 We also call it machine code and it is written as
strings of 1's (one) and 0’s (zero).
 When this sequence of codes is fed to the
computer, it recognizes the codes and converts it
in to electrical signals needed to run it. For
example, a program instruction may look like this:

1011000111101
Advantage Machine
Language :

The only advantage is that


program of machine
language run very fast
because no translation
program is required for the
CPU.
Disadvantages Machine
Language:
1. It is very difficult to program in machine
language.

The programmer has to know details of


hardware to write program.

2. The programmer has to remember a lot


of codes to write a program which results in
program errors.

3. It is difficult to debug the program.


Assembly Level Language:
An assembly language is a low-level
language for programming computers.

The word "low" does not imply that the


language is inferior to high-level programming
languages but rather refers to the small or
nonexistent amount of abstraction between
the language and machine language, because
of this, low-level languages are sometimes
described as being "close to the hardware."

It implements a symbolic representation of


the numeric machine codes and other
constants needed to program a particular CPU
architecture.
Levels of Programming
Languages
Assembly languages
English-like abbreviations
representing elementary
computer operations
(translated via assemblers)
Example:
LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY
Assembly Level
Language (contd…):
 A utility program called an assembler, is used to
translate assembly language statements into the
target computer's machine code.

 The assembler performs a more or less isomorphic
translation (a one-to-one mapping) from mnemonic
statements into machine instructions and data.
Example: Assembly language representation is easier
to remember (more mnemonic)

mov al, 061h

This instruction means:


Move the hexadecimal value 61 (97 decimal) into the
processor register named "al".

The mnemonic "mov" is an operation code or opcode,


A comma-separated list of arguments or parameters
Assembly Language
A symbolic representation of the machine
language of a specific processor.
 Is converted to machine code by an assembler.
 Usually, each line of assembly code produces one
machine instruction (One-to-one correspondence).
 Programming in assembly language is slow and
error-prone but is more efficient in terms of
hardware performance.
 Mnemonic representation of the instructions and
data
 Example:
Load Price
Add Tax
Store Cost
Example (Adds 2 numbers):

name "add"
mov al, 5 ; bin=00000101b
mov bl, 10 ; hex=0ah or bin=00001010b
add bl, al ; 5 + 10 = 15 (decimal) or
hex=0fh or

bin=00001111b
Advantages Assembly
Language:

1.The symbolic programming of Assembly
Language is easier to understand and saves a lot of
time and effort of the programmer.

2.It is easier to correct errors and modify program
instructions.

3.Assembly Language has the same efficiency of
execution as the machine level language.

 Because this is one-to-one translator between


assembly language program and its corresponding
machine language program.
.
Disadvantages Assembly
Language:

1.One of the major disadvantages is that
assembly language is machine dependent.

A program written for one computer might


not run in other computers with different
hardware configuration
Machine Languages, Assembly
Languages, and High-level
Languages
High-level languages
Instructions closer to everyday
English
 English is a natural language. Although
high level programming languages are
closer to natural languages, it is difficult
to get too close due to the ambiguities in
natural languages (a statement in English
can mean different things to different
people – obviously that is unacceptable
for computer programming).
Use mathematical notations
(translated via compilers)
Example:
grossPay = basePay + overTimePay
33
High-level language
 A programming language which use statements
consisting of English-like keywords such as
"FOR", "PRINT" or “IF“, ... etc.
 Each statement corresponds to several machine
language instructions (one-to-many
correspondence).
 Much easier to program than in assembly
language.
 Data are referenced using descriptive names
 Operations can be described using familiar
symbols
 Example:
Cost := Price + Tax
Advantages of High Level
Languages

Higher level languages have a major
advantage over machine and assembly
languages that higher level languages are
easy to learn and use.

It is because that they are similar to the


languages used by us in our day to day life.
Advantages of High Level
Languages
High-level language programs are
portable.
(Although some programs could still
have a few machine-dependent details,
they can be used with little or no
modifications on other types of
machines.)
Program development is faster
Fewer lines of code
Program maintenance is easier
Low vs High level language

Low Level Language:


The Low Level language are those
languages which are near to computer
language.
It is hard to understand by Human and
easy to understand by Computer itself.

Example:
Machine Language (1's and 0's)
Assembly Language (Key words and
symbols)
Advantages and
Disadvantages of Low Level
Languages:

It consists of binary digits and some symbols.


It depends on machine.
Machine language directly executes on
computer.
No need of language translator for machine
language.
Program written in low level language is
called object code.
Example of low level languages
are “MACHINE” and “ASSEMBLY” Language.
disadvantages of Low Level
languages

It is not easy to
understand.
It is difficult to
modify.
High Level Languages:

The high level languages which are close to human
languages.
 they are easy to understand by Humans but difficult for
Computers to understand.
 Computer needs some mechanism to understand them.
Example:

 BASIC
 PASCAL
 FORTRAN
 C/C++
 python
 JAVA etc.

Advantages and disadvantages
of High Level Languages
advantages of high level languages:
they are close to human being
they are easy to understand
They consist of English language like
structure
 It does not depend upon machine
they are easy to modify
disadvantages of high level languages:
they need language translator to translate
into program in to machine language.
They do not execute directly on computer.
Programming Language levels
summary:
•.

•Machine languages are the only languages


understood by computers.
Programming Language levels
summary:Computer Languages
– Machine Language
• Uses binary code
• Machine-dependent
• Not portable
Assembly Language
Uses mnemonics
Machine-dependent
Not usually portable
High-Level Language (HLL)
Uses English-like language
Machine independent
Portable (but must be compiled for different
platforms)
Examples: Pascal, C, C++, Java, Fortran, . . .
Syntax & Semantics in
programming
Syntax:
A language's syntax is he
grammar of the language.
Semantics:
The meanings of words
and symbols used in the
language
Source code or Source program

The form in which a computer


program, written in some
formal programming language,
is written by the programmer.

Can be compiled automatically


into object code or machine
code or executed by an
interpreter.
Compilers & Programs
Compiler
A program that converts
another program from
some source language (or
high-level programming
language / HLL) to machine
language (object code).
Compilation into Assembly L

Source Assembly
Program Compiler Language

Assembly Assembler Machine


Language Language
Compilers & Programs
Object code or Object program
Output from the compiler
Equivalent machine language translation of the
source program
Files usually have extension ‘.obj’

Executable program
Output from linker/loader
Machine language program linked with necessary
libraries & other files
Files usually have extension ‘.exe’
Program Execution
 Steps taken by the CPU to run a program
(instructions are in machine language):
1. Fetch an instruction
2. Decode (interpret) the instruction
3. Retrieve data, if needed
4. Execute (perform) actual processing
5. Store the results, if needed
Program Errors
Syntax Errors:
Errors in grammar of the language
Runtime error:
When there are no syntax errors, but the
program can’t complete execution
 Divide by zero
 Invalid input data

Logical errors:
The program completes execution, but delivers
incorrect results
Incorrect usage of parentheses
Language translator
Language translator is a program that
translates a program of high level language
of computer understands only machine
language.
A program written in high-level or assembly
language cannot be run on a computer
directly.
It must be converted into machine
language before execution
Types of translators

Types of translators
Different types of translators or
language processors are
Compiler
Interpreter
Assembler
1. Compiler

 A compiler is a program that converts the instruction


of a high level language into machine language as a
whole.
 A program written in high-level language is called
source program.
 The compiler converts the source program into
machine code. The machine code program is known as
object program. The object program can executed
many times
 working of compiler
 The compiler checks each statement in the source
program and generates machine instructions.
 Compiler also checks syntax errors in program.
 A source program containing an error cannot be
compiled
2. Interpreter

An interpreter is a program that converts


one statement of a program into machine
at one time.

It executes this statement before


translating the next statement of the
source program.

If there is an error in the statements, the


interpreter stops working and displays an
errors message
advantage and disadvantage of
interpreters
The advantage of interpreters over
compilers is that an error is found
immediately.
So the programmer can correct errors
during program development.
The disadvantage of interpreter is that it is
not very efficient.
The interpreter does not produce an
object program.
It must convert the program each time it is
executed.
Compilation
Source Target
Program Compiler Program

Input Target Program Output

 Compiler translates source into target (a machine


language program)
 Compiler goes away at execution time
 Compiler is itself a machine language program,
presumably created by compiling some other
high-level program
 Machine language, when written in a format
understood by the OS is object code
Interpretation
Source
Program

Interpreter Output

Input

• The interpreter stays around during


execution
• It reads and executes statements one at a
time
Difference between source code
and object code
Source Code Object Code

Source code is in the Object Code is in the


form of Text form. form of Binary Numbers.
Source code is Human Object Code is in

Readable Code. Machine Readable


formats.
Source code is
Object Code is
Generated by Human
Generated by Compiler.
or Programmer. Object Code is
Source code is receive
Generated by Compiler
Compiler as a Input. as a Output.
Compilation vs. Interpretation
Compilation:
Syntax errors caught before running the
program
Better performance
Decisions made once, at compile time
Interpretation:
Better diagnostics (error messages)
More flexibility
Supports late binding (delaying decisions
about program implementation until runtime)
 Can better cope with PLs where type and size of
variables depend on input
Supports creation/modification of program
code on the fly (e.g. Lisp, Prolog)
Example :JAVA
For portability:

Java Compiler

bytecode

ML Interpreter

For flexibility: Just In Time (JIT) compiler


translates bytecode into ML just before
execution
A compiled languages vs
interpreted languages
A compiled language is a programming
language whose implementations are typically
compilers (translators which generate
machine code from source code), and not
interpreters (step-by-step executors of source
code, where no translation takes place).

A compiled language is one where the


program, once compiled, is expressed in the
instructions of the target machine; this
machine code is undecipherable by humans.
interpreted languages
An interpreted language is
one where the instructions are
not directly executed by the
target machine, but instead
read and executed by some
other program (which normally
is written in the language of
the native machine).
Features and Characteristics of Compiled
Languages

 A compiled language is a programming language


whose implementations are typically compilers
(translators which generate machine code from
source code), and not interpreters (step-by-step
executors of source code,).
 A program translated by a compiler tends to be
much faster than an interpreter executing the same
program.

 A closer look at the operations of the back end of a


compiled language reveals:
 intermediate code generation
 code optimisation
 machine code generation
 This results in a more efficient and faster
execution of the code
Characteristics of Interpreted
Languages

 One of the main benefits of interpreted languages is that they


require no compilation. The language is interpreted at run-time
so the instructions are executed immediately. Any errors in an
interpreted program will result in the execution of the code to be
halted.
 Interpreted languages also have a simple syntax which, for the
user:
 makes them easy to learn and use
 assumes minimum programming knowledge or experience
 allows complex tasks to be performed in relatively few
steps
 allows simple creation and editing in a variety of text
editors
 allows the addition of dynamic and interactive activities
to web pages
 Also, interpreted languages are generally portable across
various hardware and network platforms and scripts can
be embedded in standard text documents for added
functionality.
Compiled Languages
advantages

The major advantage of compiled


languages over interpreted languages is
their execution speed.

Because compiled languages are converted
directly into machine code, they run
significantly faster and more efficiently
than interpreted languages, especially
considering the complexity of statements in
some of the more modern scripting
languages which are interpreted.
Examples of pure compiled
languages
Examples of pure compiled
languages include
C,
C++,
Erlang,
Haskell, and more modern
languages such as Rust and
Go.
Disadvs of compiled
languages
 In order to run a program written in a compiled
language, you need to first manually compile it.
Not only is this an extra step in order to run a
program, but while you debug the program, you
would need to recompile the program each time
you want to test your new changes. That can
make debugging very tedious.

 Another detriment of compiled languages is that


they are not platform-independent, as the
compiled machine code is specific to the
machine that is executing it.
Interpreted Languages
ADVANTAGES

In contrast to compiled languages,


interpreted languages do not
require machine code in order to execute
the program; instead, interpreters will run
through a program line by line and execute
each command.
Examples of some common interpreted
languages include PHP,
Perl,
Ruby,
and Python.
Interpreted Languages
ADVANTAGES…

Some of the programming


concepts that interpreted
languages make easier are:
 Platform independence
Smaller executable
program size
disadvantage of interpreted
languages
The main disadvantage
of interpreted languages
is a slower program
execution speed
compared to compiled
languages
Characteristics of Interpreted
Languages

 Unlike a compiler, an interpreter checks syntax and


generates output one source line at a time.
 When an error is encountered, the interpreter
immediately feeds back information on the type of
error and stops interpreting the code.
 This allows the programmer to see instantly the
nature of the error and where it has occurred. He or
she can then make the necessary changes to the
source code and have it re-interpreted.
 As the interpreter executes each line of code at a
time the programmer is able to see the results of
their programs immediately which can also help
with debugging.
CLASS WOrK
 Question 1
(a) Explain what is meant by the term
'compilation'. (4 marks)
(b) Explain why compiled programming
languages execute faster than interpreted
programming languages. (8 marks)
(c) Explain why interpreted programming
languages have greater flexibility than
compiled programming languages. (8
marks)

SUITABILIY OF A PROGRAMMING
LANGUAGE FOR A TASK
Ease of learning
· Ease of understanding
· Speed of development
· Help with enforcement of correct code
· Performance of compiled code
· Supported platform environments
· Portability
· Fit-for-purpose
SUITABILIY OF A
PROGRAMMING LANGUAGE
FOR A TASK

1. Ease of Learning


Clearly, the easier that the
programming language is to learn,
the quicker that programmers
become productive.
For instance Java is a lot easier to
learn than C++;
SUITABILIY OF A
PROGRAMMING LANGUAGE
FOR A TASK
2. Ease of Understanding
Most code is written once and read many
times—usually, to focus on a particular
point (for instance, to fix a bug).

Thus, it is important that the reader quickly


grasp the essence of what’s happening.
SUITABILIY OF A
PROGRAMMING LANGUAGE
FOR A TASK
3.Speed of Development
Language must offer a programmer
development speed because it has
features that allow this to take place.
If you look at speed of development in the
round, you must consider not only how long
it takes you to write code, but also how
long it takes you to find a solution to the
problem at hand and find the bugs.


SUITABILIY OF A
PROGRAMMING LANGUAGE
FOR A TASK
4. Help with Enforcement of Correct
Code
The ideal programming language should
turn logic errors into syntax errors.

A powerful means to this end is type


checking. Most standard languages, such
as Java and COBOL, have good type
checking.
SUITABILIY OF A
PROGRAMMING LANGUAGE
FOR A TASK
5. Supported Platform Environments
By platform environment, we mean not
only the operating-system facilities, but
also the middleware facilities, database
facilities, and system-management
facilities.
A GOOD PROGRAMMING LANGUAGE OUGHT
TO SUPPORT MANY PLATFORMS THAT WILL
BE ASSOCIATED WITH THE SOFTWARE
BEING DEVELOPED USING THE LANGUAGE
SUITABILIY OF A
PROGRAMMING LANGUAGE
FOR A TASK
 6. Portability
 Most languages are dependent on hardware
constraints in some form and the OS environment
in which they ought to run.
 Portability is a characteristic attributed to a
computer program if it can be used in an operating
systems other than the one in which it was created
without requiring major rework.
 Porting is the task of doing any work necessary to
make the computer program run in the new
environment
 Hence a good programming language ought to
provide for porting
SUITABILIY OF A
PROGRAMMING LANGUAGE
FOR A TASK.
7. Fit-for-Purpose
While Java is a good language and is highly
portable, it is unsuitable for some purposes,
such as some game programming and system
programming.
Game programming often requires fast
access to the screen-display hardware. This
can be done by using DirectX, which is
available only in C++ or C.
Hence a programming language is suitable for
a particular task if it fits well in the purpose
Bytecode Languages

Bytecode languages are a type of


programming language that fall under
the categories of both compiled and
interpreted languages because they
employ both compilation and
interpretation to execute code.
Java and the .Net framework are
easily the most common examples of
bytecode languages (dubbed Common
Intermediate Language in .Net).
How Bytecode Languages
are translated?
In a bytecode language, the first
step is to compile the current
program from its human-readable
language into bytecode.
From this point, the bytecode is passed to
a virtual machine which acts as the
interpreter, which then proceeds to
interpret the code as a standard
interpreter would.
What is bytecode?
Bytecode is a form of instruction set
that is designed to be efficiently
executed by an interpreter and is
composed of compact numeric codes,
constants, and memory references.
Bytecode is nothing but the intermediate
representation of Java source code which is
produced by the Java compiler by compiling
that source code.
This byte code is an machine independent
code
Main disadvantage of
bytecode
In bytecode languages, there
is a delay when the program is
first run in order to compile the
code into bytecode, but the
execution speed is increased
considerably compared to
standard interpreted languages
because the bytecode is
optimized for the interpreter.
Main advantage of bytecode
The largest benefit of
bytecode languages is
platform independence
which is typically only available
to interpreted languages, but
the programs have a much
faster execution speed than
interpreted languages.
xristics of BYTE CODE:

It is not an completely a compiled code


but it is an intermediate code somewhere
in the middle which is later interpreted
and executed by JVM.

Bytecode is a machine code for JVM.

But the machine code is platform specific


whereas bytecode is platform independent
that is the main difference between
How java code is transformed
and applied on various
platforms
THE JAVA VIRTUAL MACHINE
When you compile something in Java, the
compiler generates bytecode.

This is native code for the Java Virtual


Machine.

The JVM then translates the bytecode to


native code for your processor/architecture
Summary of the bytecode
concept
Bytecode is a step between your source
code and actual machine code.

The JVM is what takes the bytecode and


translates it into machine code.

You might also like