Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Lesson 1 and 2 Programming Languages

The document covers the history and evolution of programming languages, detailing first, second, third, and fourth-generation languages, along with their characteristics and benefits. It also discusses various programming paradigms, including imperative, object-oriented, and functional programming, highlighting their unique features. Additionally, it explains the differences between compiled and interpreted languages, emphasizing their respective advantages and disadvantages.

Uploaded by

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

Lesson 1 and 2 Programming Languages

The document covers the history and evolution of programming languages, detailing first, second, third, and fourth-generation languages, along with their characteristics and benefits. It also discusses various programming paradigms, including imperative, object-oriented, and functional programming, highlighting their unique features. Additionally, it explains the differences between compiled and interpreted languages, emphasizing their respective advantages and disadvantages.

Uploaded by

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

LESSON 1 & 2

Programming Languages

TOPICS LEARNING OUTCOMES


1. Describe how the programming
History of programming languages
languages integrate.
Programming paradigms
2. Explain the types and
techniques in programming.
Compiled vs. Interpretative Languages

3. Differentiate the different


Application vs. Scripting Languages
scripting languages.

TOPIC 1: History of Programming Languages Lecture


There are many types of programming languages out there and you might already have heard of a
few of them, for example: C++, VB.NET, Java, Python, Assembly. We will now look at the history of how
these languages came about and what they are still useful for. In all cases keep in mind that the only thing
a computer will execute is machine code or object code when it has been converted from a language to run
on a processor.
The first generation program language is pure machine code, that is just
ones and zeros, e.g.. 0010010010101111101010110. Programmers have to
design their code by
hand then transfer it to a computer by using a punch card, punch tape or flicking
switches. There is no need to translate the code and it will run straight away.
This may sound rather archaic, but there are benefits:

Code can be fast and efficient


Code can make use of specific processor features such as special
registers
And of course drawbacks
Code cannot be ported to other systems and has to be rewritten Code is difficult to
edit and update
Second-generation programming languages are a way of describing Assembly code which you may have
already met.
By using codes resembling English,
programming becomes much LDA for load and STA easier. The
usage of these mnemonic codes such as
for store means the code is easier to read and write. To convert an
assembly code program into object code to run on a computer
requires an Assembler and each line of assembly can be replaced by
the equivalent one line of object (machine) code:
Assembly code has similar benefits to writing in machine code, it is a one to one relationship after all. This
means that assembly code is often used when writing low level fast code for specific hardware. Until
recently machine code was used to program things such as mobile phones, but with the speed and
performance of languages such as C being very close to Assembly, and with C's ability to talk to processor
registers, Assembly's use is declining.
As you can hopefully see there are benefits to using Second-Generation Languages over First-Generation,
plus a few other things that makes Assembly great:
Code can be fast and efficient
Code can make use of specific processor features such as special registers
As it is closer to plain English, it is easier to read and write when compared to machine code
And of course drawbacks
Code cannot be ported to other systems and has to be rewritten
Third generation (High Level Language)
Even though Assembly code is easier to read than machine code, it is still not straight forward to perform
loops and conditionals and writing large programs can be a slow process creating a mish-mash of goto
statements and jumps. Third-
generation programming languages brought many programmer-friendly features to code such as loops,
conditionals, classes etc. This means that one line of third generation code can produce many lines of object
(machine) code, saving a lot of time when writing programs.

Imperative languages - code is executed line by line, in a programmer defined sequence


Third generation (High Level Languages) codes are imperative. Imperative means that code is executed line
by line, in sequence. For example:

dim x as integer

2x=3

3 dim y as integer

Would output:4 y = 8 5
Third generation languages can be platform independent, meaning that code written for 5 x = x + y one
system will work on another. To convert a 3rd generation program into object code requires a Compiler or
an Interpreter.

To summarise:
Hardware independence, can be easily ported to other systems and processors
Time saving programmer friendly, one line of 3rd gen is the equivalent of many lines of 1st and
2nd gen
However
Code produced might not make the best use of processor specific features unlike 1st and 2nd gen

EXTENSION: PROGRAMMING PARADIGMS

There are several types of Third-generation languages that you will cover in more
detail at A2. They include:

• Object Orientated
• Event driven

Fourth-generation languages are designed to reduce programming effort and the time it takes to develop
software, resulting in a reduction in the cost of software development. They are not always successful in
this task, sometimes resulting in inelegant and hard to maintain code. Languages have been designed with
a specific purpose in mind and this might include languages to query databases (SQL), languages to make
reports (Oracle Reports) and languages to construct user interface (XUL). An example of 4th generation
programming type is the declarative language.
--an example of a Structured Query Language (SQL) to select
criminal details from a database
Declarative languages - describe what computation should be performed and not
SELECT name, height, DoB FROM criminals WHERE numScars = 7; how
to perform it. Not imperative!
An example of a declarative language is CSS which you might learn more about when completing any web
design unit.

/*code to change the headings on a page to green and the paragraphs to red and italic*/ h1 { color : #00FF00; }
p { color : #FF0000; font-style : italic }

Exercise: Generations of programming language

1. Describe what is meant by an imperative language:


2. What is the relationship between lines of object code and lines of 2nd generation language code
3. What is the relationship between lines of object code and lines of 3rd generation language code:
4. Give two benefits of using 3rd generation over using assembly. Give one drawback 5. Give the
definition of a declarative language
6. For 2nd and 3rd generation languages give the program translator required to convert the language
generation to object code:

TOPIC 2: PROGRAMMING PARADIGMS l


Lecture

A programming paradigm is a style, or “way,” of programming. Some languages make it easy to write in
some paradigms but not others.

Some Common Paradigms

You should know these:

Paradigms are not meant to be mutually exclusive; a single program can feature
Imperative Programming by defining objects that send messages to each other. Objects have their
own internal (encapsulated) state and public interfaces. Object orientation can be:
Declarative Class-based: Objects get state and behavior based on membership in a class.

Structured Prototype-based: Objects get behavior from a prototype object.

Procedural Programming by defining objects that send messages to each other. Objects have their
own internal (encapsulated) state and public interfaces. Object orientation can be:
Functional Class-based: Objects get state and behavior based on membership in a class.

Function-Level Prototype-based: Objects get behavior from a prototype object.

Object-Oriented Programming by defining objects that send messages to each other. Objects have their
own internal (encapsulated) state and public interfaces. Object orientation can be:
Event-Driven Programming with emitters and listeners of asynchronous actions.
Flow-Driven Programming processes communicating with each other over predefined channels.
Logic (Rule-based): Programming by specifying a set of facts and rules. An engine infers the
answers to questions.
Constraint Programming by specifying a set of constraints. An engine finds the values that meet the
constraints.
Aspect-Oriented Programming cross-cutting concerns applied transparently.

Reflective Programming by manipulating the program elements themselves.

Array Programming with powerful array operators that usually make loops unnecessary.

Logic Programming with emitters and listeners of asynchronous actions.


multiple paradigms!
Imperative Programming

Control flow in imperative programming is explicit: commands show how the computation takes place, step
by step. Each step affects the global state of the computation.
result = []
i = 0 start:
numPeople = length(people)
if i >= numPeople goto finished
p = people[i]
nameLength = length(p.name) if
nameLength <= 5 goto nextOne

upperName = toUpper(p.name)
addToList(result, upperName)
nextOne:
i=i+1
goto start
finished:
return sort(result)
Structured Programming

Structured programming is a kind of imperative programming where control flow is defined by nested
loops, conditionals, and subroutines, rather than via gotos. Variables are generally local to blocks (have
lexical scope).

result = [];
for i = 0; i < length(people); i++ {
p = people[i];
if length(p.name)) > 5 {
addToList(result, toUpper(p.name));
}
}
Early languages emphasizing structured programming: Algol 60, PL/I, Algol 68, Pascal, C, Ada 83, Modula,
Modula-2. Structured programming as a discipline is sometimes though to have been started by a famous
letter by Edsger Dijkstra entitled Go to Statement Considered Harmful.

Object Oriented Programming

OOP is based on the sending of messages to objects. Objects respond to messages by performing
operations, generally called methods. Messages can have arguments. A society of objects, each with their
own local memory and own set of operations has a different feel than the monolithic processor and single
shared memory feel of non object oriented languages.

One of the more visible aspects of the more pure-ish OO languages is that conditionals and loops become
messages themselves, whose arguments are often blocks of executable code. In a Smalltalk-like syntax:
result := List new.
people each: [:p |
p name length greaterThan: 5 ifTrue: [result add (p name upper)]
]
result sort.
^result

This can be shortened to:


^people filter: [:p | p name length greaterThan: 5] map: [:p | p name upper] sort

Many popular languages that call themselves OO languages (e.g., Java, C++), really just take some elements
of OOP and mix them in to imperative-looking code. In the following, we can see that length and toUpper
are methods rather than top-level functions, but the for and if are back to being control structures:
result = [] for p in
people {
if p.name.length > 5 {
result.add(p.name.toUpper); }
}

The first object oriented language was return result.sort; Simula-67; Smalltalk followed
soon after as the
first “pure” object-oriented language. Many languages designed from the 1980s to the present have labeled
themselves object-oriented, notably C++, CLOS (object system of Common Lisp), Eiffel, Modula-3, Ada 95,
Java, C#, Ruby.

Declarative Programming

Control flow in declarative programming is implicit: the programmer states only what the result should look
like, not how to obtain it.
select upper(name)
from people where length(name) > 5
order by name

No loops, no assignments, etc. Whatever engine that interprets this code is just supposed go get the desired
information, and can use whatever approach it wants. (The logic and constraint paradigms are generally
declarative as well.)

Functional Programming
In functional programming , control flow is expressed by combining function calls, rather than by assigning
values to variables:
sort(
fix(λf. λp. if(equals(p,
emptylist),
emptylist,
if(greater(length(name(head(p))), 5),
append(to_upper(name(head(p))), f(tail(p))),
f(tail(people)))))(people))

Yikes! We’ll describe that later. For now, be thankful there’s usually syntactic sugar:

let
fun uppercasedLongNames [] = []
| uppercasedLongNames (p :: ps) = if length(name p) > 5 then (to_upper(name
p))::(uppercasedLongNames ps) else (uppercasedLongNames ps)

in
sort(uppercasedLongNames(people))

Huh? That still isn’t very pretty. Why do people like this stuff? Well the real power of this paradigm comes
from passing functions to functions (and returning functions from functions).

sort(
filter(λs. length s > 5,
map(λp. to_upper(name p), people)))
We can do better by using the cool |> operator. Here x |> f just means f(x). The operator
has very low precedence so you can read things left-to-right:

people |> map (λp. to_upper (name p)) |> filter (λs. length s > 5) |> sort

Let’s keep going! Notice that you wouldn’t write map(λx. square(x)), right? You would write map(square).
We can do something similar above, but we have to use function composition, you know, (f o g)x is f(g(x)),
so:

people |> map (to_upper o name) |> filter (λs. length s > 5) |> sort

With functional programming:

There are no commands, only side-effect free expressions


Code is much shorter, less error-prone, and much easier to prove correct
There is more inherent parallelism, so good compilers can produce faster code

Exercise: Group Activity (5 minutes)

Research the following programming styles and state how they are similar and how
they are different from each other: (a) Stack-based, (b) Concatenative, (c) Point free,
(d) Tacit.

TOPIC 3: COMPILED VS. INTERPRETATIVE


Most programs are written in a high-level language such as C, Perl, or Java. Just as a human language
makes it easy for people to communicate with one another, so computer languages simplify the job of
telling a computer what to do. However, because a computer only understands numbers, talking to one is
like speaking to someone with whom you don't share a language. You need a translator to properly
communicate, and that's what interpreters and compilers do.

The difference between an interpreted and a compiled language lies in the result of the process of
interpreting or compiling. An interpreter produces a result from a program, while a compiler produces a
program written in assembly language. The assembler of architecture then turns the resulting program into
binary code. Assembly language varies for each individual computer, depending upon its architecture.
Consequently, compiled programs can only run on computers that have the same architecture as the
computer on which they were compiled.

A compiled program is not human readable, but instead is in an architecturespecific machine


language. Creating a compiled program requires several steps. First, the programmer, using a development
tool or even a simple text editor, writes the source code in a chosen computer language. If the program is
complex, pieces of it may be spread across several files. The programmer then compiles the program,
sorting and linking the modules and translating it all into machine code that the computer understands.

Because different kinds of computers do not speak each others' machine languages, a compiled
program will only work on the platform it was designed for. For example, a program written for HP-UX
normally will not work on a Mac OS computer or a computer running Solaris. Despite this drawback,
compiled programs are faster than those that must be run through an interpreter. Also, it is often possible
to recompile the program so that it will run on different platforms. Examples of languages that are normally
used to produce compiled programs include C, Fortran, and COBOL.

In an interpreted program, on the other hand, the source code typically is the program. Programs
of this type (often known as scripts) require an interpreter, which parses the commands in the program
and then executes them. Some interpreters, such as the Unix shells (sh, csh, ksh, etc.), read and then
immediately execute each command, while others, such as Perl, analyze the entire script before sending
the corresponding machine language instructions. The advantage of a script is that it is very portable. Any
computer that has the appropriate interpreter installed may run the program more or less unchanged. This
is a disadvantage as well, because the program will not run at all if the interpreter is not available. In
general, interpreted programs are slower than compiled programs, but are easier to debug and revise.
Other examples of interpreted languages include JavaScript and Python.

Intermediate to computer-specific compiled programs and interpreted scripts are programs


designed for runtime environments. Java and Smalltalk programs are executed in this fashion. Constructing
programs for runtime environments is similar to writing traditional compiled programs. The difference is
that instead of compiling the source code into a machine language, it is output into byte code for the
runtime environment's "virtual machine". This virtual machine intercepts the byte code instructions and
translates them into computer-specific commands. The advantage of this approach is that the runtime
environment quickly compiles only the needed pieces of the code (some parts of the program may never
need to be executed). This is called just-in-time compiling. The major disadvantage with runtime
environments is that a program that is not designed well will force the runtime environment to compile
almost all of the code up front and then make redundant calls to the interpreter. This makes the program
slower to load and run.

Compiled Versus Interpreted Languages


Every program is a set of instructions, whether it’s to add two numbers or send a request over the internet.
Compilers and interpreters take human-readable code and convert it to computer-readable machine code.
In a compiled language, the target machine directly translates the program. In an interpreted language, the
source code is not directly translated by the target machine. Instead, a different program, aka the
interpreter, reads and executes the code.

Okay… but what does that actually mean?


So let’s say you have an hummus recipe that you want to make, but it’s in Ancient Greek. There are two
ways you, as a non-Ancient-Greek speaker, could follow its directions.
The first is if someone had translated it into English for you already. You (and anyone else who could speak
English) could get the English version and make hummus. This is the compiled version.
The second is if you had a friend who knows Ancient Greek. Your friend can sit next to you and translate
the Ancient Greek into English, line by line, as you go. In this case, your friend is the interpreter. This is the
interpreted version.

Compiled Languages
Compiled languages are converted directly into machine code that the processor can execute. As a result,
they tend to be faster and more efficient to execute than interpreted languages. They also give the
developer more control over hardware aspects, like memory management and CPU usage.
Compiled languages need a “build” step - they need to be manually compiled first. You need to “rebuild”
the program every time you need to make a change. In our hummus example, the entire translation is
written before it gets to you. If the original author decided he wanted to use a different kind of olive oil,
the entire recipe would need to be translated again and then sent to you.
Examples of pure compiled languages are C, C++, Erlang, Haskell, Rust, and Go.

Interpreted Languages
Interpreters will run through a program line by line and execute each command. Now, if the author decided
he wanted to use a different kind of olive oil, he could scratch the old one out and add the new one. Your
translator friend can then convey that change to you as it happens.
Interpreted languages were once known to be significantly slower than compiled languages. But, with the
development of just-in-time compilation, that gap is shrinking.
Examples of common interpreted languages are PHP, Ruby, Python, and JavaScript.

A Small Caveat
Most programming languages can have both compiled and interpreted implementations. The language
itself is not necessarily compiled or interpreted. However, for simplicity’s sake, they’re typically referred to
as such.
Strictly speaking, the terms interpreted language and compiled language are not well defined because, in
theory, any programming language can be either interpreted or compiled. In modern programming
language implementation it is increasingly popular for a platform to provide both options. e.g. Python can
be executed either as a compiled program, or as an interpreted language in interactive mode.
Most command line tools, CLIs, and shells can theoretically be classified as interpreted languages.

Advantages and Disadvantages


Advantages of Compiled Languages
Programs compiled into native code at compile time usually tend to be faster than those translated at run
time, due to the overhead of the translation process.
Disadvantages of Compiled Languages
The most notable disadvantages are :-

• Additional time needed to complete the entire compilation step before testing, and
• Platform dependence of the generated binary code.

Advantages of Interpreted Languages


An Interpreted language gives implementations some additional flexibility over compiled implementations.
Because interpreters execute the source program code themselves, the code itself is platform independent
(Java’s byte code, for example). Other features include dynamic typing, and smaller executable program
size.
Disadvantages of Interpreted Languages
The most notable disadvantage is typical execution speed compared to compiled languages.
Exercise: COMPILED VS. INTERPRETATIVE

1. What's the difference between compiled and ...


2. Why are Interpreted Languages Slow?
3. What are the pros and cons of interpreted languages?
TOPIC 4: PROGRMMING LANGUAGES VS. SCRIPTING What’s the
difference between Scripting and Programming Languages
Basically, all scripting languages are programming languages. The theoretical difference between the two
is that scripting languages do not require the compilation step and are rather interpreted. For example,
normally, a C program needs to be compiled before running whereas normally, a scripting language like
JavaScript or PHP need not be compiled.

Generally, compiled programs run faster than interpreted programs because they are first converted native
machine code. Also, compilers read and analyze the code only once, and report the errors collectively that
the code might have, but the interpreter will read and analyze the code statements each time it meets
them and halts at that very instance if there is some error. In practice, the distinction between the two is
getting blurred owing to improved computation capabilities of the modern hardware and advanced coding
practices.

Another point to be noted is that while classifying a language as scripting language or programming
language, the environment on which it would execute must be taken into consideration. The reason why
this is important is that we can design an interpreter for C language and use it as a scripting language, and
at the same time, we can design a compiler for JavaScript and use it as a non-scripting(compiled language).
A live example of this is V8, the JavaScript engine of Google Chrome, which compiles the JavaScript code
into machine code, rather than interpreting it.

Some scripting languages traditionally used without an explicit compilation step are
JavaScript,PHP,Python,VBScript.

Some programming languages traditionally used with an explicit compilation step are C, C++.

Applications of Scripting Languages :

PROGRMMING LANGUAGES VS. SCRIPTING

Programming Language Scripting Language

A programming language is an A scripting language is a


organized way of communicating programming language that
with a computer. supports scripts.

Traditional programming is based Scripting prefers high level


on low level languages. language.

The traditional programming Perl, Python, javaScript, and other


l anguages such as C, C++, and Java languages used for scripting are
are compiled. interpreted and do not requires the
compilation step.

General programming leads to Scripting promotes open projects


closed software applications. and is used for web applications.
1. To automate certain tasks in a program
2. Extracting information from a data set
3. Less code intensive as compared to traditional programming languages

Applications of Programming Languages :

1. They typically run inside a parent program like scripts


2. More compatible while integrating code with mathematical models 3. Languages like JAVA can
be compiled and then used on any platform.

You might also like