Uploaded by Anthony Benson

introduction to programming

advertisement
Lecture by: Hugo Johnson (mrj@stmonicascollege.org)
INTRODUCTION TO PROGRAMMING
PART I
WHY BOTHER LEARNING TO PROGRAM A
COMPUTER?”
For the fun
´ To fill a need
´ For a new or second Career
´ As an intellectual challenge
´
PROGRAMMING IS PROBLEM-SOLVING
To tell a computer how to solve one big problem, you usually must tell
the computer how to solve a bunch of little problems that make up the bigger
problem. If you want to make your own video game, for example, you need to
solve some of the following problems:
´
´
´
´
´
Determine how far to move a cartoon figure.
Detect whether the cartoon figure bumps into a wall, falls off a cliff, or runs
into another cartoon figure on-screen.
Make sure that the cartoon figure doesn’t make any illegal moves, such as
walking through a wall.
Draw the terrain surrounding the cartoon figure and make sure that if the
cartoon figure walks behind an object such as a tree, the tree realistically
blocks the figure from sight.
Determine whether bullets that another cartoon figure fires are hitting the
player’s cartoon figure
figure. If so
so, determine the amount of damage,
damage how
it affects the movement of the damaged cartoon figure, and how the
damage appears on-screen.
PROGRAMMING IS PROBLEM SOLVING
The simpler that the problem is that you need to
solve, the more easily you can write a program that
tells the computer how to work. Otherwise the more
p the p
problem,, the bigger
gg the program
p g
and it
complex
takes more time to be written.
PROGRAMMING IS LIKE WRITING ENGLISH
ESSAY OR GIVING DIRECTIONS:
If you can write step‐by step instructions directing someone to your house, you can
write a program.
The hardest part about programming is identifying all the little problems that
make up the big problem that you’re trying to solve. Eg. Giving some simple
directions to someone:
Go south
G
th on Highway
High
I-5.
I5
´ Get off at the Sweetwater Road exit.
´ Turn right at the light.
´ Turn
T
iinto
t th
the second
dd
driveway
i
on th
the left.
l ft
´
PROGRAMMING IS LIKE WRITING ENGLISH
ESSAY OR GIVING DIRECTIONS:
Of course, if you try giving these instructions to a computer, the computer
gets confused and wants to know the following additional information:
´
´
´
´
´
´
Where do I start and exactly how far south do I drive down Highway I5?
How do I recognize the Sweetwater Road exit,
exit and how do I get off at
this exit?
After I turn right at the light, how far to the right do I turn, and do you
mean the traffic light or the street light on the corner?
After I turn into the second driveway on the left, what do I do next?
Park
the car?
? Honk the horn?
?G
Gun the engine and accelerate through your
garage door?
PROGRAMMING IS LIKE WRITING ENGLISH
ESSAY OR GIVING DIRECTIONS:
p y everything
y
g that yyou want the computer
p
Unless yyou specify
to do and
exactly how to do it, the computer just plain doesn’t do what you
want it to do.
N.B. A collection of instructions that tell the computer what to do is
known as a program.
program
p
programming
p
g
g language,
g g , is
The instructions,, written in a specific
known as the source code.
WHY SO MANY DIFFERENT PROGRAMMING
LANGUAGES?
You have many programming languages to choose among because each language
serves a specific purpose, and people are always creating new languages
to solve different types of problems.
Essentially, computers really understand only one language, which consists
of zeroes and ones,
ones also known as machine language.
language A typical program that
you could write in machine language might look something like the following
example:
0010 1010 0001 1101
0011 1100 1010 1111
MACHINE LANGUAGE HAS THE FOLLOWING TWO
MAJOR DRAWBACKS:
You can easily
Y
il type
t
a 0 or 1 b
by mistake,
i t k th
thereby
b
preventing you from giving the computer the
correct instructions.
instructions
´ Machine language takes a long time to write (and
an even llonger ti
time tto understand
d t d what
h t the
th
language is actually telling the computer to do).
´
Because of these two huge problems, few people write programs in
machine language. To make writing a program easier, programmers
quickly invented a simpler programming language known as
g g
assemblyy language.
EXAMPLE OF ASSEMBLY LANGUAGE CODE:
title Nap Program
; This program displays “Take a nap!” on the screen
dosseg
.model small
.stack 100h
.data
d
my_message db ‘Take a nap!’,0dh,0ah,’$’
.code
main proc
mov ax,@data
mov ds,ax
mov ah,9
mov dx,offset my_message
UNDERSTANDING ASSEMBLY LANGUAGE
Because computers can’t read assembly language instructions, programmers
created special programs that translate assembly language into machine language.
These special programs are known as assemblers.
TWO DISTINCT ADVANTAGES OVER MACHINE
LANGUAGE:
´
Assembly language programs are easier to read
than machine language programs.
´
Assembly
A
bl llanguage
g g programs
g
are easier
i tto
write (and modify) than machine language
programs.
ASSEMBLY LANGUAGE HAS THE FOLLOWING
DISADVANTAGES:
´
´
´
Programs
P
g
that
th t you create
t by
b using
i g assembly
bl llanguage
g g
run slower and gobble up more space (both physical
disk space and memory) than equivalent programs that
you may create with machine language.
You can’t
can t easily transfer (or, to use programming lingo,
port) a program that you write in assembly language for
one computer to another computer.
Writing a program in assembly language can prove
extremely tedious, time-consuming, and complicated.
That’s why few people bother to write large programs in
assembly language.
THE HOLY GRAIL OF PROGRAMMING
N.B. In general, the easier the programming language is to read and write, the
slower and larger are the programs it creates. The Holy Grail of computer
programming
i is
i to
t create
t programs th
thatt are easy tto write,
it run as ffastt as possible,
ibl
and take up as little space as possible.
WHAT ARE HIGH LEVEL LANGUAGES?
Is it’s a language that closely resembles English like statements and
mathematic notations. It is independent of the internal machine code of any
particular computer. The program written in High Level Language is called the
Source Code while the translated version is called the Object Code.
EXAMPLE OF SOURCE CODE:
main()
{
printf (“Take a nap!\n”);
}
This language
g g is known as C p
programming
g
g language.
g g The above p
program
g
is equivalent to the assembly language program for taking a nap. Not to
worry about knowing the Syntax for now, it will all come to fruition.
N.B. The Object code is really the raw 0s and 1s that the machine
understands, in other words it’s the machine code.
HIGH LEVEL LANGUAGES ARE SLOWER TO
CREATE
A program that you write in C runs slower and creates larger program
files than does an equivalent assembly language program. That’s because
assembly language is closer to the native language of computers (which is
machine
hi code)
d ) than
th C.
C
So C programs need to first get translated into assembly
language code before finally being converted into machine language code.
KEY ADVANTAGES OF HIGH LEVEL
PROGRAMMING LANGUAGES:
It iis easier
i tto llearn and
d code
d programs
g
b
because
the programmer does not need detailed
knowledge of the computer or its internal
operation.
´ Errors
E
are easier
i tto avoid
id and
dd
detect.
t t
´ Many high level languages are machine
independent, making them portable (transferable
from say a Mac to a PC or even a Smart Phone).
´ Easier to maintain and shared with other
programmers.
´
KEY DIS-ADVANTAGES OF HIGH LEVEL
PROGRAMMING LANGUAGES:
Takes longer to create because of the steps it
has to take: converting from source code to
assembly language code and then to machine
code.
´ Gives the user access to the computer’s
memory, which
hi h h
has di
disastrous
t
effects
ff t if
misused.
´
THERE TWO WAYS TO CONVERT SOURCE CODE
INTO OBJECT CODE:
´
´
Using a Compiler: Translates to Object Code which is
stored for processing at a later time.
Using an Interpreter: Translates and runs the program in
real time,
time object code created while program is being
executed.
DIAGRAM OF SOURCE CODE TRANSITION TO
MACHINE LANGUAGE
WHAT ARE FOURTH GENERATION LANGUAGES?
It is type of Language that consists of more English like commands
Which makes it easier to deploy a program vs its predicecer languages.
A Perfect example would be the language SQL (Structured Query Language)
Used in most Database Management System Software.
OBJECTIVES OF 4GLS:
IIncrease the
th speed
d off program
g
development
d l
t
´ Minimizing the end-user effort to obtain
information from a computer.
´ Decreasing the skill level required of end users, so
they can concentrate on an application rather than
on the intricacies of coding. They can therefore
solve their own problems without the aid of a
professional programmer.
´ Minimize the maintenance by reducing errors and
making programs easy to change.
´
NATURAL LANGUAGES
Its a language that makes computer language as close to human
As possible, allowing computers to interpret real English sentences.
For example, if a user enters the command “Get me sales
Figures for January 2009”, a computer understanding natural languages
Could interpret this and supply the desired information.
Read more on Natural language processing (NLP).
END OF LECTURE
In our next lecture (Part II) we will focus on algorithm
Development and its rules and principles.
Download