Useful Free Resources

Friday, February 12, 2010

Programming languages --A high level point of view for beginners

Trying to pick up programming indirectly from a text is like
trying to learn judo by reading a pamphlet. In both 
cases, you may glean a theoretical understanding
of the subject, but until you actually practice
your skill, you don’t know how
much you really picked up.


-->
INTRO
Why Learn Computer Programming?
Computer programming is a skill that anyone can pick up, given enough practice, patience, and caffeinated beverages. The answer depends on your ultimate goals, but some common answers to consider are:
  • For fun: Programming a computer can prove fun because you can, for example, design simple programs that display your portfolio on the computer. More complex programs probably may make you earn dollars. A stand-up comedian wrote (in BASIC for his own amusement, using a program known as CA-Realizer) a program known as Comedy Writer. Then he decided to sell the program to others.
  • To fill a need: Many people learn programming with no intention of becoming a full-time, professional programmer. They just want a program that solves a particular problem, but they can’t find a program that does it, so they write the program themselves. A psychologist who specialized in dream interpretation used his knowledge and a program known as ToolBook to create and sell DreamScape, a program that interprets the meaning of dreams. Whatever your interests, you can write a program to solve a specific problem that others may find useful as well.
  • For a new or second career: With computers taking over the world, you’re never unemployed for long if you know how to program a computer. Companies are always looking to create new programs, but you also find a growing market for programmers who can maintain and modify the millions of existing programs that do everything from storing hotel reservations to transferring bank deposits electronically. If you know how to program a computer, you’re in a much better position to earn a lot of money and live wherever you want. You may still want to keep your current job, but programming gives you a new way to expand and share your knowledge. A group of alternative health-care practitioners, for example, wrote IBIS, a program that provides information for treating a variety of aliments by using acupuncture, massage, diet, and homeopathy. They wrote IBIS by using a program known as MetaCard.
  • As an intellectual challenge: Many people find the sheer complexity of computers as fascinating as studying a mathematical puzzle. Not surprisingly, computers tend to attract people of above-average intelligence who enjoy programming a computer to pry into the thought processes of their own minds. To help turn a computer into a thinking tool, one programmer created the Axon Idea Processor by using Prolog, a popular programming language used for researching artificial intelligence. The goal was to create a program to help people manipulate ideas, concepts, and facts so that they can devise a variety of possible solutions while better understanding their own way of thinking in the process. If using a computer normally seems boring, try writing your own program to help you use your brain more effectively.
    If you aren’t doing what you truly enjoy, all the money in the world isn’t going to make your life better. Choose to learn programming because you want to — not because you think that it’s going to make you rich.
How Does a Computer Program Work?
 To make the computer do something useful, you must give it instructions in either of the following two ways:
  • Write a program, which tells a computer what to do, step-by-step, much as you write out a recipe.
  • Buy a program that someone else has already written that tells the computer what to do.
So, to get a computer to do something useful, you (or somebody else) must write a program. A program does nothing more than tell the computer how to
  • accept some type of input, 
  • manipulate that input, and 
  • spit it back out again in some form that humans find useful.

Programming is problem-solving
Essentially, a program tells the computer how to solve a specific problem. The number and variety of programs that people can write for computers is practically endless.

But 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(use the divide and conquer strategy). 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 (such as a car, a spaceship, or a man) on-screen as the user moves a joystick.
  • 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. If so, determine the amount of damage, how it affects the movement of the damaged cartoon figure, and how the damage appears on-screen.
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(Divide et impera).
A program that displays a simple Ping-Pong game with two stick paddles and a ball is much easier to write than a program that displays World War II fighter airplanes firing machine guns and dropping bombs on moving tanks, while  dodging anti-aircraft fire.

Programming isn’t difficult it’s just time- consuming
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. Because computers are completely stupid, you need to tell them how to do everything. If you’re giving a friend instructions to get to your house, for example, you may write down the following information:
  1. Go south on Highway I-5.
  2. Get off at the Sweetwater Road exit.
  3. Turn right at the light.
  4. Turn into the second driveway on the left.
Of course, if you try giving these instructions to a computer, the computer gets confused and wants to know the following additional information:
  1. Where do I start and exactly how far south do I drive down Highway I-5?
  2. How do I recognize the Sweetwater Road exit, and how do I get off at this exit?
  3. 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?
  4. After I turn into the second driveway on the left, what do I do next? Park the car? Honk the horn? Gun the engine and accelerate through your garage door?
You need to tell computers how to do everything, which can make giving them instructions as aggravating and frustrating as telling children what to do. Unless you specify everything that you want the computer to do and exactly how to do it, the computer just plain doesn’t do what you want it to do.
Know that If you ever start  writing a program and feel like giving up before it ever works, you’re not alone.

What Do I Need to Know to Program a Computer?
If you’re the type who finds the idea of making a program (such as a video game) more exciting than actually using it, you already have everything you need to program a computer. If you want to learn computer programming, you need a healthy dose of the following three qualities:
  • Desire: If you want something badly enough, you tend to get it (although you may serve time in prison afterward if you do something illegal to get it). If you have the desire to learn how to program a computer, your desire helps you learn programming, no matter what obstacles may get in your way.
  • Curiosity: A healthy dose of curiosity can encourage you to experiment and continue learning about programming long after you finish reading this sentences. With curiosity behind you, learning to program seems less a chore and more fun. And as long as you’re having fun, you tend to learn and retain more information than does someone without any curiosity whatsoever.
  • Imagination: Computer programming is a skill, but imagination can give your skill direction and guidance. A mediocre programmer with lots of imagination always creates more interesting and useful programs than a great programmer with no imagination. If you don’t know what to do with your programming skill, your talent goes to waste without imagination prodding you onward. 
Desire, curiosity, and imagination are three crucial ingredients that every programmer needs. If you possess these qualities, you can worry about trivial details such as learning a specific programming language (such as C++). Learning to program a computer may (initially) seem an impossible task, but don’t worry. Computer programming is relatively simple to understand; everything just tends to fall apart after you try to put a program into actual use.

Programming Languages
Because computers are functionally brain-dead, people must write instructions for a computer by using a special language, hence, the term programming language.
A collection of instructions that tell the computer what to do is known as a program. The instructions, written in a specific programming language, is known as the source code.
---p19

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 is to create programs that are easy to write, run as fast as possible, and take up as little space as possible.
By using assembly language, programmers sacrifice readability for speed and size.
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 code) than C.
So C programs need to first get translated into assembly language code before finally being converted into machine language code.
This two-step process tends to be less efficient than writing an equivalent assembly language program. C source code, however, is much easier to read,
write, and modify than assembly language source code (and assembly language source code far easier to read, write, and modify than an equivalent machine-language source code).

assembly > C
As is true of translations between human languages, the simpler the language, the easier is the translation. Translating a children’s book from French
into Japanese is much easier than translating a mathematics dissertation
from French into Japanese, mainly because a children’s book uses simple
words, while a mathematics dissertation uses more complicated words.
Similarly, translating C into machine language code is more difficult than
translating assembly language into machine language code.

So the only way that you can run a C program on another computer is if
someone’s already written a C compiler for that other computer.
Because C is a simple language, writing C compilers for different computers is relatively easy, especially if you compare it with the same task for other programming languages, such as Ada or LISP.

Because C compilers are fairly easy to write, you can find C compilers for
almost every computer in the world. Theoretically, you can write a C pro-
gram for the Macintosh, copy it to a computer running Windows XP, recompile it, and run the program with little or no modification.
Given its power and portability, C has quickly become one of the most popular programming languages in the world. The majority of all programs are
written in C although most newer programs are now written in a C derivative
language called C++. Some of the more famous (or infamous) programs that
have been written in C or C++ include operating systems such as MS's Windows
95 / 98 / Me / NT/2000 / XP / vista ecc , Unix, and Linux, as well as major commercial programs such as Quicken, Netscape Navigator, and Microsoft Word.

Although C is popular, it has its share of flaws:
  1. C creates larger and slower programs than equivalent assembly or machine-language programs.
  2. The C language gives programmers access to all parts of a computer, including the capability to manipulate the computer’s memory.
Unfortunately, all this power can prove as dangerous as giving a hyper- active monkey a chainsaw and a hand grenade. If you don’t write your C programs carefully, they can accidentally wreck your computer’s memory, causing your program to crash your computer. 
In a desperate attempt to make C programming more reliable, programmers
developed languages similar to C, such as C++, Java, Perl, Python, C# etc. All
of these C-derived languages add a special feature known as object-orientation,
which encourages programmers to write small programs that they can easily
reuse and modify.
In addition, these other languages try to protect programmers from writing programs that can mess up the computer’s memory, as C programs can do, which decreases the chance of writing a program that crashes an entire computer.

Indeed. The best language for almost any job is likely to be the one you 
already know - if you only have a hammer everything looks like a nail.  
So the moral of the story is that you can make programming a lot easier if you just choose the right programming language to help you solve the right problem.

The Tools of a Computer Programmer
You need the following two crucial tools to write a program:
  • An editor (so that you can write your instructions to the computer).
  • A compiler which converts your instructions into machine language so that the computer knows what you want it to do. Instead of using a compiler, many programming languages use an interpreter. The main difference between the two is that an interpreter converts your instructions into machine language and stores them in memory each time you run the program, whereas a compiler converts your instructions into machine language once and saves those instructions in a executable file (often called an EXE in Windows world).

You may want to use the following additional tools in writing a program:
  • A debugger (which helps identify problems --called also bugs-- in your program).
  • A Help file authoring program (so that your program can provide Help on-screen instead of needing to supply the user with a decent manual).
  • An installation program (to copy your program to the user’s computer).

If you buy a specific programming language, such as Visual Basic, Delphi, or Real Basic, you usually get an editor, compiler, and a debugger, which means you just need to buy a separate Help file authoring program and an installation program.


Writing Programs in an Editor
As you write a program, you must type your instructions in a text (or ASCII) file. Although you can use a word processor to create a text file, a word processor offers fancy formatting features (such as changing fonts or underlining text), which you don’t need to write a program. An ASCII file consists of nothing but characters that you can type from a keyboard. ASCII stands for American Standard Code for Information Interchange, which is simply a universal file format that any computer can use.
 A program consists of one or more instructions that tell the computer what to do. The instructions that make up a program are known as the program’s source code.
Rather than struggle with a word processor, programmers created special programs for writing, editing, and printing the source code of a program. Almost no one writes a program correctly the first time, so the majority of a programmer’s time is spent editing and debugging the source code. As a result, the program that enables you to write, edit, and print a program is known as an text editor(or editor).

An editor looks like a word processor but may offer special features to make programming easier, such as :
  • automatically formatting your source code,
  • offering shortcuts for editing your source code,
  • or providing pop-up Help as you’re typing program commands(in the programming language you use).
Anytime that you need to write or modify the source code of a program, you must use an editor. 

Using a Compiler or an Interpreter
 After you type your instructions in an editor by using a programming language such as C++ or Java, guess what? The computer doesn’t have the slightest idea what you just created. 
Computers understand only machine language, so you need to use another special program to convert your source code (the instructions that you write in C++ or Java) into machine language.

You can use either of the following two types of programs to convert source code into machine language:
  • A compiler
  • An interpreter
 Compilers
  1. A compiler takes your source code, 
  2. converts the entire thing into machine language, and 
  3. then stores these equivalent machine language instructions in a separate file, often known as an executable file
The process is like having a translator study an entire novel written in Spanish and then translate it into Arabic. 
Whenever a compiler converts source code into machine language, it’s called compiling a program.
After you compile a program, you can just give away copies of the executable (machine-language) version of your program without giving away your source code version. 
As a result, most commercial programs (such as Microsoft PowerPoint and Quicken) are compiled. 
After you use a compiler to convert source code into machine language, you never need to use the compiler again (unless you make changes to your source code).
A compiler creates machine language for a specific microprocessor, such as the PowerPC (which the Apple Macintosh uses) or the Intel Pentium family of microprocessors (including clone microprocessors, such as the AMD Athlon). 

If you write a program in BASIC and want to run it on a Macintosh and a Windows computer, you need to compile your program twice:
  • once for the Macintosh and
  • once for the Windows environment.
Not all compilers are equal, although two different compilers may convert the same language into machine language. Given identical C++ source code, for example, one C++ compiler may create a program that runs quickly, whereas a second C++ compiler may create a smaller file that runs much slower.

Interpreters
A second, but less popular, way to convert source code into machine language is to use an interpreter. 
An interpreter converts each line of your source code into machine language, one line at a time. The process is like giving a speech in English and having someone translate your sentences, one at a time, into another language (such as French).
Unlike what a compiler does, an interpreter converts source code into machine language but stores the machine-language instructions in the computer’s memory. Every time that you turn off the computer, you lose the machine-language version of your program. To run the program again, you must feed the source code into the interpreter again.
If anyone wants to run your program, that person needs both an interpreter and the source code for your program. Because your source code enables everyone to see how you wrote your program (and gives others a chance to copy or modify your program without your permission), very few commercial programs use an interpreter.
Most Web-page programming languages use interpreters, such as JavaScript and VBScript
Because different computers can view Web pages, you can’t compile programs that you write in JavaScript or VBScript into machine language. Instead, your computer’s browser uses an interpreter to run a JavaScript or VBScript program.
In the old days, when computers were slow and lacking in sufficient memory and storage space, interpreters were popular because they gave you instant feedback. The moment you typed an instruction into the computer, the interpreter told you whether that instruction would work and even showed you the results. 
With an interpreter, you could write and test your program at the same time. Now, computers are so fast that programmers find using a compiler easier than using an interpreter.
P-code: A combination of compiler and interpreter
Getting a program to run on different types of computers is often a big pain in the neck. Both Macintosh and Windows programs, for example, use pull-down menus and dialog boxes. You need to write one set of commands to create pull-down menus on the Macintosh, however, and a second set of commands to create the identical menus in Windows.

Because one program almost never runs on multiple computers without extensive modification, programmers combined the features of a compiler with an interpreter to create something called p-code
Instead of compiling source code directly into machine language, p-code compiles source code into a special intermediate file format. To run a program compiled into p-code, you use an interpreter. This two-step process means that after you compile your program into p-code, you can run your p-code interpreted program on any computer that has the right p-code interpreter.
Java is the most popular programming language that uses p-code. After you compile a Java program into p-code, you can copy that p-code to a Macintosh, a Windows computer, or a Linux computer. As long as that computer uses a Java p-code interpreter, you can run the Java program on that computer without modification.
Best of all, programs that you compile into p-code can run without the original source code, which means that you can protect your source code and still give your program away to others.
Just in case you’re wondering, Liberty BASIC, which comes with this book, takes BASIC instructions and saves them in a separate file that uses p-code. 
If you distribute any compiled programs that you create using Liberty BASIC, you need to also distribute a special run-time file that can translate your Liberty BASIC p-code on another computer.
Naturally, p-code has its own disadvantages. 
  1. Programs that you create by using p-code tend to run much slower than programs that you compile directly into machine language. 
  2. Although p-code programs can run without a copy of the original source code that you use to create them, you can also decompile p-code programs.
Decompiling a p-code program can reveal the original source code that the programmer used to create the program. So if you write a program in Java and compile it into p-code, a rival can decompile your p-code program and see your original Java source code. Your rival then ends up with a nearly identical copy of your source code, essentially stealing your program. 
You can actually decompile any program, including programs that you compile into machine language. 
But unlike with decompiling p-code programs,  decompiling a machine-language version of a program never gets you the original high-level language source code that the programmer used to write the program.  
If you compile a program into machine language, the original source code can be written in C++, COBOL, FORTRAN, BASIC, Ada, LISP, Pascal, or any other programming language in the world. 
Because the decompiler has no idea what language the original source code was written in, it can only decompile a machine-language version of a program into equivalent assembly language. 
After you decompile a program into assembly language source code, you can rewrite or modify that source code. Decompiling effectively allows you to steal the ideas of others.

So what do I use?
  • If you want to write programs to sell, use a compiler, which protects your original source code.
  • If you want to write a program to run on your Web page, you can use either an interpreter or p-code.
  • If you want to write a program that can run on different types of computers, p-code may prove your only choice. As a safer but more cumbersome alternative, you can also use multiple compilers and modify your program to run on each different computer. 
The language that you choose can determine whether you can use
  • a compiler,
  • an interpreter,
  • or p-code.
You often convert Java programs into p-code, for example, although you can compile them directly into machine language. On the other hand, you usually compile C++ and rarely interpret or convert it into p-code.


Squashing Bugs with a Debugger 
Few computer programs work 100% correctly, which explains why computers crash, lose airline reservations, or just act erratically at times. Mathematically, writing a program that works 100% correctly every time is impossible because testing a program for all possible types of computers, hardware, and additional software that may interfere with the way your program runs is impossible.
A problem that keeps a program from working correctly is known as a bug. 
In the early days, computers used mechanical relays and vacuum tubes instead of circuit boards and microprocessors. One day, the computer failed to work correctly. The scientists examined their program; it should have worked. So they next examined the computer itself and found that a moth had gotten smashed in a mechanical relay, preventing it from closing and thus keeping the computer from working correctly. From that point on, problems in computers have been known as bugs. 
Because writing a program that works 100% correctly all the time is virtually impossible, 
  1. operating systems (such as Windows XP) unavoidably contain bugs that keep them from working correctly. 
  2. When you convert your source code into machine language, you must use a compiler or interpreter, which is another program that contains its share of bugs. 
  3. Finally, your own program may contain bugs of its own.
With so many places for bugs to creep in, you shouldn’t be surprised that bugs infest computers like cockroaches infest a cheap apartment complex.

Although you can do little about bugs in other people’s programs (except not buy the programs), you can reduce (but not completely eliminate) bugs in your own program by using a debugger. 
A debugger is a special program (which may also contain bugs) that can help you track down and wipe out bugs in programs that you write.

A debugger provides several ways to track down bugs in your program:
  • Stepping: The debugger runs your program, line-by-line, so that you can see exactly which line may contain the bug. This process is like rereading written instructions to get to another person’s house if you’re lost. By going over these instructions, one by one, you can find out where you made a wrong turn.
  • Breakpoints: Rather than force you to step through an entire program, line-by-line, breakpoints enable you to specify where you want to start examining your program line-by-line. So if you were lost, instead of rereading the instructions to get to another person’s house from start to finish, you skip those instructions that you know you followed correctly and examine only the remaining instructions that you aren’t sure that you followed correctly. Similarly, by using breakpoints, you can selectively examine only parts of your program, line-by-line, and skip over the parts that you know already work.
  • Watching: Watching enables you to see your program storing data in memory and to determine what that data may be. If your program stores incorrect data (such as saving a name instead of a telephone number), you know exactly where in your program the bug is occurring. Each time you examine a line in your program, the debugger shows you how that particular line affects the value you’re watching. As soon as you see the value change, the debugger shows you exactly which line in your program caused that change. This process is like having someone to tell you to drive 10 miles south down a certain road after turning right. The moment that you exceed 10 miles, a watchpoint alerts you so that you know exactly where you almost made a mistake and got lost.
A debugger essentially shows you exactly how a computer is going to interpret the instructions in your program
Of course, if you fix one bug, you may introduce several new ones. That’s why writing bug-free programs is impossible.

Writing a Help File
Nobody has trouble using a doorknob, a toaster, or a microwave oven, but people still complain that computers and VCRs are too hard to use. The problem with VCRs lies in the cryptic controls that aren’t easy to figure out just by looking at them.

Similarly, the problem with computer programs is that programs are too complex to use at first glance. If you can make a program that’s actually easy to use, people can actually use it.
Because computer programs are still being designed for programmers by other programmers, computers still mystify the average user. To help the poor befuddled user, most programs now offer Help files. 
A Help file provides instructions and explanations on-screen. 
Theoretically, if the user experiences trouble with the program, he can browse through the Help file, find an explanation or step-by-step instructions, and continue using the program.
Although Help files still can’t substitute for designing a program that’s easy to use in the first place, most programs offer Help files anyway. 
To keep your program modern and up-to-date, include a Help file with your program. 
To create a Help file, you can use a special Help file-authoring program, which simplifies the process of creating and organizing topics for your Help file.

Creating an Installation Program
After you 
  1. write your program, 
  2. test it, 
  3. debug it, and 
  4. write a Help file for it, 
the final step is to give or sell copies of your program to others. Although you can copy your program onto a floppy disk or CD/DVD and force buyers to manually copy your program to their hard drive, doing so can cause problems. Users may not copy all the files correctly, for example. And forcing users to manually copy your program to their hard drive may prove such a nuisance that most people don’t bother even trying to use your program.

To make copying a program to a user’s hard drive as easy as possible, most commercial programs include a special installation program. 
Users run this installation program, which automatically copies a program and all necessary files to the appropriate location on the user’s hard drive. 
By making the installation of a program practically foolproof, software publishers make sure that users install the programs correctly.
So the final step to distributing your program to others is to use a special installation program, which can smash all your program files into a single file that can automatically install itself on another computer.

Installation programs offer the following features for distributing programs to others:
  • File compression: Most programs are fairly large, which means that they can’t fit on a single floppy disk. Rather than force users to insert a series of floppy disks or compact discs (CDs) into their computer to install your program, an installation program smashes your files so that they can fit on as few floppy disks or compact discs as possible.
  • Display graphics and play sounds: Installing a program usually takes a few minutes while the computer copies files from the floppy or CD to its hard drive. Rather than force the user to stare into space, an installation program can display advertisements or messages to make the installation process mildly interesting. Other uses for graphics include displaying an hourglass icon or a status bar that shows how far the installation is complete, such as 54% complete. That way, users know how much longer they need to wait.
  • Simplify the copying process: Most important, an installation program simplifies copying your program to the user’s hard drive, making the entire process accurate, fast, and foolproof.
The first impression that people get from your program is through its installation process, so an installation program helps give your program a professional appearance. 
Of course, you need to make sure that your installation program installs your program correctly or the first impression that users get from your program will be a highly negative one.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.