Computer Programming

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

‫بسم هللا الرحمن الرحیم‬

Lecturer: Nawsherwan Wahedy


PROGRAMMING LANGUAGE

A vocabulary and set of grammatical rules for


instructing a computer to perform specific tasks.
Why learn to program?
✓Develops logic and problem-solving skills
✓Improves attention to detail
✓It’s fun!
Programming required knowledge of computer language
syntax
Evolution of programming
languages
First generation : Machine languages

The machine language writes programs using


the machine code of 1s and 0s, which is
directly understood by the computer.

The main problems with using machine code


directly are that it is very easy to make a
mistake, and very hard to find It once you
realize the mistake has been made.
Second generation : Assembly languages

Assembly language is written using mnemonic codes


(abbreviated English words) or short codes that
suggest their meaning and are therefore easier to
remember.
These codes represent operations, addresses that
relate to main memory, and storage registers of the
computer. Typical codes might be
LDA, STO, ADD, NOP, etc.
An example of program code to add and store
two numbers would be:

LDA A, 20 : Load accumulator A with the value 20


ADD A 10 : Add the value 10 to accumulator A
STO B, A : Store contents of accumulator A into storage register B
NOP : No operation (stop here)
Third generation : high-level languages

Codes similar to everyday English


use mathematical notations (translated
via compilers)
Example:
grosspay= base pay + overtime pay
PL hierarchy
What does the computer understand?

Computer only understands machine


language instructions.
Assembler

Instructions written in assembly language must be


translated to machine language instructions :
Assembler does this
One to one translation : One AL instruction is
mapped to one ML instruction.
AL instructions are CPU specific.
Compiler

Instructions written in high-level


language also must be translated to
machine language instructions :
Compiler does this

Generally one to many translation one


HL instruction is mapped to many ML
instruction.
HL instructions are not CPU specific but
compiler is.
Translation from HLL to ML
Interpreter

An interpreter translates high-level instructions


into an intermediate form, which it then
executes. In contrast, a compiler translates
high-level instructions directly into machine
language.
Compiled programs generally run faster than
interpreted programs.
Different types of high-level PL

Weakly typed/strongly typed


Structured
And many other types
Typed languages

Type information was added to programs to improve


efficiency. For ex. An integer addition is performed
more efficiently than floating point addition. Hence
it is more advantageous to declare the value/variable
as integer whenever it is possible.
Weakly typed/strongly typed language

A strongly-typed programming language is one in which


each type of data (such as integer, character, hexadecimal,
packed decimal, and so forth) is predefined as part of the
programming language and all constants or variables
defined for a given program must be described with one of
the data types.
Certain operations may be allowable only with
certain data types.
The language compiler enforces the data typing
and use compliance.
Strongly typed language contd..

Different definitions are given for a language to


be strongly typed and if that condition is not
defined by a particular language it is said to
weakly typed in that context.
For example..

A language is strongly typed if it contains


compile -time checks for type constraint
violations. If all checking is deferred to run time,
it is weakly typed.
A language is strongly typed if it contains compile- or run
time checks for type constraint violations. If no checking is
done, it is weakly typed.

A language is strongly typed if conversions between


different types are forbidden. If such conversions are
allowed, it is weakly typed.
More examples..

A language is strongly typed if conversions


between different types must be indicated
explicitly. If implicit conversions are performed, it
is weakly typed.
A language is strongly typed if there is no
language-level way to disable or evade
the type system. If there are c-style casts
or other type-evasive mechanisms, it is
weakly typed.
Where does C fit in?

For example, under definitions 3,4 and 5 the C


language is weakly typed; — with definitions 1
and 2 it is open for further debate since C does
perform type checks for compound types but not
for scalar or array types.
C++/java?

C++ and Java are stronger typed than C.


Typed languages contd..

An advantage of strong data typing is that it


imposes a rigorous set of rules on a
programmer and thus guarantees a certain
consistency of results.
A disadvantage is that it prevents the programmer
from inventing a data type not anticipated by the
developers of the programming language and it limits
how "creative" one can be in using a given data type.
Structured programming

Structured programming, disciplined approach to


writing programs, Clear, easy to test and debug
and easy to modify Structured programming is
hard and takes time to master
Structured programming
contd..
A technique for organizing and coding computer
programs in which a hierarchy of modules is
used, each having a single entry and a single
exit point, and in which control is passed
downward through the structure without
unconditional branches to higher levels of the
structure.
Three types of control flow are used:
sequential, test, and iteration
Structured programming

Only the following code structures are used to


write programs:
1. Sequence of sequentially executed
statements.
2. Conditional execution of statements ex, (“ if “
statements).
3. Looping.
4. Structured subroutine calls (e.g., ‘Go sub' but
not ‘Goto')
Structure programming contd..

In particular, the following language usage is


forbidden:
 "go to" statements.
 "Break" or "continue" out of the middle of loops.
 Multiple exit points to a
function/procedure/subroutine (ex:
multiple "return" statements).
 Multiple entry points to a
function/procedure/subroutine.
Structured programming contd..

Structured programming is generally a non-issue


when doing modular programming or object
oriented programming as it's assumed that
individual methods are structured.
Structured VS unstructured languages

Structured languages, such as Pascal, Ada and dbase,


force the programmer to write a structured program.
Unstructured languages such as Fortran, Cobol and basic
require discipline on the part of the programmer to write a
structured program.
Structured VS unstructured languages

Unstructured languages define control flow


largely in terms of a GOTO command that
transfers execution to a label in code.
Structured programming languages provide constructs
(often called "if-then-else", "switch", "unless", "while",
"until", and "for") for creating a variety of loops and
conditional branches of execution, although they may
also provide a GOTO to reduce excessive nesting of
cascades of "if" structures, especially for handling
exceptional conditions.
Strictly speaking, in a structured programming
language, any code structure should have only
one entry point and one point of exit; many
languages such as C allow multiple paths to a
structure's exit (such as "continue", "break", and
"return"), which can bring both advantages and
disadvantages in readability.
The key software trend:
object technology

Objects
Reusable software components that model
items in the real world Meaningful software
units.
Date objects, time objects, paycheck objects,
invoice objects, audio objects, video objects, file
objects, record objects, etc.
Any noun can be represented as an object
More understandable, better organized, and
easier to maintain than procedural
programming.
Java source code files (files with a .Java extension)
are compiled into a format called bytecode (files with
a .Class extension), which can then be executed by a
java interpreter. Compiled java code can run on most
computers because java interpreters and runtime
environments, known as java virtual machines (vms),
exist for.
Most operating systems, including UNIX, the
Macintosh OS, and windows. Bytecode can
also be converted directly into machine
language instructions by a just-in-time compiler
(JIT).

You might also like