Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Programming for Dummies 2004.pdf
Скачиваний:
109
Добавлен:
17.08.2013
Размер:
8.05 Mб
Скачать

24

Part I: Programming a Computer

Although, in theory, C programs can run on different computers without modification, the reality is that you almost always must modify a C program slightly or drastically to get it to run on a different computer. Modifying a C program, however, is still much easier than modifying an assembly or machine-language program.

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 Windows 95/98/Me/NT/2000/XP, 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:

C creates larger and slower programs than equivalent assembly or machine-language programs.

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 hyperactive 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, and C#. 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.

High-level programming languages

Because writing machineor assembly-language programs was so difficult and confusing, people developed additional languages that look more like human languages, with names such as FORTRAN, COBOL, BASIC, Pascal, and Ada. By making programming languages look more like ordinary human languages, the creators of these high-level languages hoped to make programs easier to write and modify later on.

One of the first high-level languages was FORTRAN (which stands for FORmula TRANslator). FORTRAN was designed specifically for mathematical calculations. Another early high-level language was COBOL (COmmon Business- Oriented Language), which was designed for business data processing.

Chapter 2: All about Programming Languages

25

Because each language has a specialized purpose, most people aren’t going to use FORTRAN or COBOL to write video games, operating systems, or word processors (although you can still do so if you really want).

Because programming was still too difficult for many people, computer scientists soon created both Pascal and BASIC to teach people programming. BASIC — Beginner’s All-purpose Symbolic Instruction Code — was designed to teach complete novices how to program. Beginners could start learning to program by using C, but the complexities of C can discourage people too soon — sort of like trying to teach a three-year-old how to ride a bicycle by putting him on a motorcycle in the middle of rush-hour traffic.

The main advantage of BASIC is its simplicity. To print the words “Take a nap!” on-screen, you need only the one following command:

PRINT “Take a nap!”

If you compare it with the equivalent C or assembly language source code, BASIC source code enables you to focus on the task that you want to accomplish instead of worrying about the cryptic commands of a specific programming language.

Pascal (named after the French philosopher, Blaise Pascal) is another language designed to help beginners learn how to program. The main difference between BASIC and Pascal is that Pascal encourages you to write wellstructured programs that you can easily read, understand, and modify at

a later date. The following is a Pascal program that displays “Take a nap!” on-screen:

Program Message (Input, Output);

Begin

Writeln (‘Take a nap!’);

End.0

Compared with Pascal, BASIC is much less structured, which makes writing a BASIC program easy but makes reading and understanding large BASIC programs much more difficult. Pascal is more structured and forces you to plan your program before you write, as you do if you first plan a trip before leaving home. This planning may take longer, but your program and your trip will be more organized than if you rush into writing the program right away, which can be as disorganized as showing up in Paris in the middle of the night with no hotel reservations. On the other hand, BASIC enables you to start writing your program right away, which is more likely to lead to a more disorganized program.

BASIC is such a popular language that programmers tried to combine the structured features of Pascal with the simplicity of BASIC to create various dialects of BASIC. Liberty BASIC (see Chapter 5) is one example of a structured version of BASIC.

26

Part I: Programming a Computer

As usual, high-level programming languages such as Pascal, BASIC, FORTRAN, Ada, and COBOL have their own share of problems:

High-level programming languages create larger and slower programs than equivalent C, assembly language, or machine-language programs.

High-level programming languages shield you from accessing all the parts of the computer, preventing you from using the power available in C, assembly language, or machine-language programs. As a result, writing certain types of programs, such as operating systems or disk utility programs (such as the Norton Utilities), is more difficult (but not impossible) in high-level languages.

High-level programming languages more closely resemble human languages, so writing a compiler for a high-level language is more difficult. If your computer doesn’t have a compiler for your favorite high-level language (such as Ada), you can’t write a program for your computer in that particular programming language.

Of course, nobody would use high-level programming languages such as Pascal, BASIC, FORTRAN, Ada, and COBOL unless they offered some advantages over C, assembly language, or machine language. Following are several reasons for using a high-level programming language:

You can write programs much faster in a high-level programming language than you can in assembly language or machine language. (You can write a program in C in roughly the same amount of time as in a highlevel language such as Pascal.)

Learning and mastering a high-level programming language takes less time than learning and mastering machine language, assembly language, or C programming.

Because high-level programming languages shield you from accessing all parts of a computer, they protect you from writing programs that accidentally mess up the computer, causing it to crash.

Reading and modifying a program written in a high-level language is much easier than reading and modifying an equivalent program written in C, assembly language, or machine language.

Programs written in high-level languages can run on a variety of computers. If you write a program in a high-level language, you can (theoretically) port that program to run on a different computer.

Naturally, high-level languages have their own share of problems, which include the following:

Programs written in high-level languages are more complicated to translate into machine code, which means that a program written in Ada or COBOL will likely run much slower and require more memory and disk space than a similar program written in C, assembly, or machine language

Chapter 2: All about Programming Languages

27

Creating a compiler for a high-level language is much harder than creating one for C. As a result, not all computers have compilers available for every high-level language. So if you write a program in Modula-2 or LISP, you may find it nearly impossible to port to another computer or operating system.

Rapid Application Development (RAD) programming languages

Most programming languages were designed back in the days when computer screens displayed nothing but text. The screen didn’t show graphics, mouse pointers, buttons, or windows.

Because computer screens could display only text, languages such as C++, BASIC, and Pascal just had simple commands to display information, such as the following BASIC command:

PRINT “This sentence appears on-screen.”

After computers developed fancy graphical user interfaces with windows, scroll bars, and toolbars, people began demanding programs that included all these fancy graphical features as well. To help programmers create programs with fancy user interfaces, many companies developed special dialects of existing languages, which they dubbed rapid application development (RAD) languages.

RAD languages enable programmers to design the way that they want their program to look (the user interface) and then write source code to make that user interface actually do something useful, such as display information in a window. Figure 2-1 shows such an interface in Real Basic.

Some popular RAD languages are Visual Basic and Real Basic (based on BASIC), Delphi and Kylix (based on Pascal), Visual C# (based on C++), and JBuilder (based on Java).

RAD languages offer the following benefits:

You can write programs with graphical user interfaces much faster by using RAD than you can by using ordinary C++, BASIC, or Pascal. Figure 2-2 shows StoryCraft, a story-creating program that two profes-

sional fiction writers developed to help people create original stories for novels, short stories, plays, or screenplays.

28

Part I: Programming a Computer

Figure 2-1:

Real Basic enables you to create a user interface and then

write BASIC commands to make that user interface do something useful.

Figure 2-2:

StoryCraft was written in Visual Basic to help writers create better stories, novels, and screenplays.