Вы находитесь на странице: 1из 7

Introduction to Programming

What Is Computer Programming?



`Note` In 2007, these lessons were signiIicantly modiIied. `Note`
This is a link to the new materials.
This is a link to the new first lesson.

Introduction
riting soItware, computer programs, is describing how to do something. In its
simplest Iorm, it is a lot like writing down the steps it takes to do something -
a 5rocess. But, iI what you need to do is not obvious or it involves multiple objects
(each with their own process) writing the program will challenge you like when you
are solving puzzles.
So, writing a computer program can be like composing music, like building a house,
like creating lots oI stuII. It has been argued that in its current state, it is an Art, not
engineering.
An important reason to consider learning a bit about how to program a computer is
that the concepts underlying it will be valuable to you, regardless oI whether or not
you go on to make a career out oI it. One thing that you will learn quickly is that a
computer is very dumb. It does exactly what you tell it to do, which is not necessarily
what you wanted. Programming will help you learn the importance oI clarity oI
expression.
A deep understanding of programming, in particular
the notions of successive decomposition as a mode
of analysis and debugging of trial solutions,
results in significant educational benefits in
many domains of discourse, including those
unrelated to computers and information technology
per se.

(Seymour Papert, in "Mindstorms")

Computers have proven immensely effective as aids
to clear thinking. Muddled and half-baked ideas
have sometimes survived for centuries because
luminaries have deluded themselves as much as
their followers or because lesser lights, fearing
ridicule, couldn't summon up the nerve to admit
that they didn't know what the Master was talking
about. A test as near foolproof as one could get
of whether you understand something as well as
you think is to express it as a computer program
and then see if the program does what it is
supposed to. Computers are not sycophants and
won't make enthusiastic noises to ensure their
promotion or camouflage what they don't know.
What you get is what you said.

(ames P. Hogan in "Mind Matters")
But, most oI all, it can be lots oI 1un!!!

Inside Computers - All Ones and Zeroes
Today, most people don't need to know how a computer works. Most people can
simply turn on a computer or a mobile phone and point at some little graphical image
on the display, click another button, and the computer does something. An example
would be Ior it to get weather inIormation Irom the net and display it. This is all the
average person needs to know about using a computer (or any device that's computer
based).
But, since you are going to learn how to write computer programs, you need to know
a little bit about how a computer works. Your job will be to instruct the computer to
do things. These lists oI instructions that you will write are computer programs, and
the stuII that these instructions manipulate are diIIerent types oI objects.
Basically, computers perIorm operations on objects. A microprocessor, which is the
heart oI a computer, is really primitive but very Iast. It takes groups oI binary
numbers representing parts oI objects and moves them around, adds pairs together,
subtracts one Irom another, compares a pair, etc... - that sort oI stuII.
Computers manipulate numbers, symbolic inIormation (think characters), visual
things (images), sound (heard oI MP3?), and sets oI instructions (the computer's
native language).
As an example, computer displays consist oI a bunch oI colored points called pixels.
A pixel is an object. It has a position which consists oI the row and column it is in.
Its color is speciIied as three numbers - called RGB (Red, Green, Blue) values. Play
with the Iollowing Java applet which lets you see what number values generate which
colors. hat color do you get iI you set red to 170, green to 85, and blue to 255?
hat's the RGB value Ior your Iavorite color?
Color Numbers Applet
So, like pixels are used to compose graphics and images, there are also standard
representations Ior the other things: numbers, characters, sound, and the computer's
instructions. At the lowest level, they everything is a bunch oI ones and zeros!

Binary Representation o1 Stu11
%here are only 10 different kinds of people in the world:
those who know binary and those who don't.

- Anonymous
Computers are Iull oI zillions oI binary numbers, i.e., lots and lots oI 1s and 0s. In
Iact, there are so many and it takes us humans so long Ior us to manipulate them that
we group bunches oI binary numbers together to makes things easier. As an example,
the letter 'A' in a computer has the binary representation: 01000001. But, when we
express this as an octal number, where three binary ones/zeroes are grouped together
to Iorm an octet, 'A' is 101. 101 is much easier to read, write, and manipulate than
01000001.
As you explore how computers work, you'll hear more about numbers expressed in
octal and hexadecimal; these are just more manageable representations oI binary
inIormation. Table 1.1 shows some numeric and symbolic values Ior you to check
out...
Numeric and Symbolic InIormation
ecimal
Number
in
Binary
in
Octal
in
Hexidecimal
1 1 1 1
2 10 2 2
3 11 3 3
4 100 4 4
5 101 5 5
6 110 6 6
7 111 7 7
8 1000 10 8
9 1001 11 9
10 1010 12 A
11 1011 13 B
12 1100 14 C
13 1101 15 D
14 1110 16 E
15 1111 17 F
16 10000 20 10

ASCII
Character
in
Binary
in
Octal
in
ecimal
in
Hexidecimal
space 00100000 040 32 20
! 00100001 041 33 21
( 00101000 050 40 28
) 00101001 051 41 29
0 00110000 060 48 30
1 00110001 061 49 31
2 00110010 062 50 32
3 00110011 063 51 33
A 01000001 101 65 41
B 01000010 102 66 42
C 01000011 103 67 43
D 01000100 104 68 44
a 01100001 141 97 61
b 01100010 142 98 62
c 01100011 143 99 63
d 01100100 144 100 64


Numeric InIormation Symbolic InIormation

%able 1.1


Programming Languages (%he Microprocessor's Language)
So, what is a programming language?
Just like we have many diIIerent languages in use throughout the world Ior us humans
to communicate with, there are diIIerent languages that we use to communicate with a
computer. Computer languages are used to tell the computer what to do, you instruct
it.
II you instructed a computer in its native language (machine language), you would
have to write instructions in the Iorm oI (yes, once again) binary numbers. This is
very, very hard to do. Although the pioneers oI computers did this, no one does this
these days.
One step above machine language is assembler language. In assembler, the operations
that the microprocessor knows how to do are given names. Addresses in memory can
also be given meaningIul names. This is a big step over binary, but still very tedious
to do any large soItware program with. It still has its place Ior little pieces oI soItware
that need to interact directly with the microprocessor and/or those that are executed
many, many, many times. Table 1.2 is an example oI DECsystem-10 assembler
language that:
1. adds two numbers together,
2. adds in a third number iI it is greater than zero, and
3. stores the result in a third location.
OpCode Accumulator Operand
MOVE T1 number1
ADD T1 number2
SKIPLE number3
ADD T1 number3
MOVEM T1 result
%able 1.2

Programming Languages (High-Level Languages)
Most soItware written today is written in high-level languages. There are many and
some are quite old. COBOL, FORTRAN, and Lisp were written in the 1950s!!!
Higher-level languages make it easier to describe the pieces oI the program you are
creating. They abstract away the speciIics oI the microprocessor in your computer.
Most come with large sets oI common stuII you need to do, called libraries.
In this introduction to programming, you will work with two computer languages:
Logo and Java. Logo comes Irom Bolt, Beranek & Newman (BBN) and
Massachusetts Institute oI Technology (MIT). Seymour Papert, a scientist at MIT's
ArtiIicial Intelligence Laboratory, championed the computer programming language
in the 70s. Java is one oI the newest programming languages. It appeared in 1995
just as the Internet was starting to get lots oI attention. Java was invented by James
Gosling, working at Sun Microsystems. It's sort-oI a medium-level language.
ith Logo, you will be able to write computer programs quickly and easily since it is
a very simple language. Once you have some experience with it, you will move to
writing programs in Java.
One oI the advantages oI learning Java is that there is a lot oI soItware already written
which will help you write graphical programs that run on the Internet. You get to take
advantage oI soItware that thousands oI programmers have already written. So, you
are not limited to working with numbers and characters; you get to work with window
objects, canvas objects, string objects and thousands oI others. You will not be
limited to simple operations like adding two numbers; you will use operations like
create a new window, draw a line, display some text in a window, and much, much
more.
Programming Using the English Language
Remember what I said in the Introduction to this lesson?
riting software, com5:ter 5rograms, is a lot like writing down the ste5s it takes to
do something.
BeIore we see what the Logo computer language looks like, let's use the English
language to describe how to do something as a series oI steps. A common exercise
that really gets you thinking about what computer programming can be like is to
describe a process you are Iamiliar with.
Describe how to make a peanut butter and jelly sandwich.
Rather than write my own version oI this exercise, I searched the Internet Ior the
words "computer programming sandwich" using google. One oI the hits returned
washttp://teachers.net/lessons/posts/2166.html. At the link, Deb Sweeney (Tamaqua
Area Middle School, Tamaqua, PA) described the problem as:
bjective: Students will write specific and sequential steps
on how to make a peanut butter and jelly sandwich.

Procedure: Students will write a very detailed and step-by-step
paragraph on how to make a peanut butter and jelly
sandwich for homework. %he next day, the students
will then input (read) their instructions to the
computer (teacher). %he teacher will then "make" the
programs, being sure to do exactly what the students
said...
This lesson is excellent at demonstrating how careIul you need to be, how detailed
you need to be, when writing a computer program.

Hierarchy o1 Computer Languages
Application-SpeciIic Language (4GL)
Examples: Mathematica, SQL
High-Level Language
Examples: Logo, Smalltalk
Low-Level Language
Example: C
Assembler Language
Example: Intel X86
%able 1.3

Exercise
I want to combine exposing you to more details about how a computer works and the
"programming in english" lesson. Let's learn more about binary numbers. Get a piece
oI paper and write the Iollowing binary addition problem on it:
1100
+ 1010
------

Now let's add them together.
1. The Iirst column, 0 0 0; is easy.
2. The second column, 0 1 1; that's easy too.
3. The third column, 1 0 1; this is cake...
4. The Iourth (last) column, 1 1 2; NOPE... e don't have a 2 in binary
numbers. 2 is written as 10, which is a zero with a carry oI 1. Since this is the
last column we're adding, we just write in the 10.
1100
+ 1010
------
10110
For a second problem, let's just swap the ones and zeros. Solve this one as a group...
0011
+ 0101
------

Now your turn. rite down the steps you've used, generalized Ior adding two
binary numbers together. I'll get you started.
1. Start with the rightmost column. II both oI the numbers are zeros, write a zero
underneath them and go on to work on the column to the leIt oI the current
column.
2. II one oI the numbers is a one and the other is a zero, write a one and go on to
work on the column to the leIt oI the current column.
3. ?...

Вам также может понравиться