Chapter I.3. Types of Programming Languages

After you understand how to plan, organize, and create a program through one or more methodologies (such as structured programming, event-driven programming, or object-oriented programming), you're ready to start utilizing a particular programming language.

Just as your native spoken language can shape the way you think and speak, so can your first computer programming language influence the way you think, design, and write a computer program.

You can choose from literally thousands of different programming languages with obscure names, like Icon, Algol 60, APL, Forth, and Scheme. Although you can understand programming by using any programming language, it's probably best to know at least one of the more popular programming languages, such as BASIC or C.

Knowing a popular programming language, like BASIC or C, simply gives you more opportunities to work. More people use BASIC and C, which means more jobs are available for programmers who know BASIC or C. Just as knowing English, Chinese, Spanish, or Arabic allows you to travel and speak with more people around the world (compared to knowing Swahili, Mayan, or Eskimo), so can knowing BASIC or C give you more opportunities to work and write programs anywhere you go.

Note

Sometimes there's a good reason to know an obscure programming language. One of the oldest programming languages, COBOL, was heavily used by businesses back when computers filled entire rooms and cost tens of thousands of dollars. Because many COBOL programs are still running today, COBOL programmers can actually make a nice living because so few programmers know COBOL. So knowing an obscure language might limit your opportunities, but at the same time, if someone needs a programmer who knows a particular programming language, you could be the only one they could hire (and have to pay big bucks as a result).

Choosing Your First Language

So should you start studying BASIC or C as your first programming language? Yes and no, depending on who you ask.

C the BASIC choices

Here's why you might consider studying either BASIC or C as your first programming language.

  • Because BASIC and C are two of the most popular languages used, you need to know one (or both) of them eventually, so you might as well start understanding them right from the beginning.

  • BASIC was originally designed to teach programming, so understanding BASIC can be easy, fun, and fast.

  • By studying BASIC, you can focus on understanding programming principles without getting lost in the technical details of how a computer or operating system works, or learn confusing commands or cryptic syntax that can make programming harder than necessary.

Understanding C can be important because so many programming languages (such as Java, C++, C#, and Perl) are based on the C language. So after you know C, you'll have no trouble understanding Java, C++, or practically any other programming language based on C.

With so many people using C (or languages like Java or C++, which are derived from C), any C programmer can find plenty of work, so knowing C is practically a necessity for anyone who wants to make a living as a programmer. Because you'll probably need to know C eventually, you might as well start with C from the beginning.

Having second thoughts

Ultimately, there's no "perfect" programming language to know and use because every programming language is designed to solve one problem extremely well (and hopefully prove useful enough to solve other problems, too). BASIC was designed to be easy to understand. C was designed to create efficient and powerful programs.

BASICally disrespected

From a financial point of view, BASIC programmers usually earn less than C programmers, even if they're doing the exact same job. Part of the reason for this is that BASIC suffers from the perception that it's a toy language — unsuitable for commercial use. Although that was true at one time, the BASIC language has evolved to the point where it can do practically anything C or other languages can do.

Although you can use BASIC to create anything from satellite navigation systems to Wall Street financial trading programs, BASIC programmers will probably always get paid less, so you'll need to know another language like C anyway, just to get paid more. Because BASIC programmers tend to get paid less than other programmers, many programmers feel that they might as well skip BASIC and just figure out C instead.

C, it's kinda hard

There's nothing wrong with studying C as your first programming language. The biggest problem with C is that it's not an intuitive language for beginners to learn and understand. With its cryptic syntax and odd use of symbols in place of actual words, understanding C means trying to figure out how to program while also wrestling with trying to figure out C, which essentially doubles the effort needed to know how to program a computer.

As a result, many beginners get frustrated with C as their first programming language and wind up more confused than ever. Imagine how many people would want to drive a car if it meant knowing how to refine their own gasoline and build their own engine. Understanding C isn't quite as difficult as refining gasoline or building a combustion engine, but it can seem that way, especially when you're programming for the first time and you start with the C language.

So if you're going to begin programming, you should definitely understand C eventually, but not necessarily as a first language. You don't need to know BASIC, but BASIC can definitely help you understand how programming works. If you figure out BASIC first and then figure out C, you'll know two of the most popular languages on the planet.

The more programming languages you know, the better you can understand the benefits and drawbacks of all programming languages, including favorites like BASIC and C. Ultimately, the best programming language to discover first is the language that makes programming easiest and fun for you, whether that language is as simple as BASIC, as arcane as assembly language, or as obscure as SNOBOL.

To help you understand the goals of different programming languages and the methods they use to achieve these goals, consider the different categories of programming languages.

Teaching Languages

The goal of teaching languages is to teach novices how to program for the first time, so teaching languages tend to be simple to learn and easy to understand. (Common sense might tell you that all programming languages should be simple to learn and easy to understand, but they're not. Languages, like C and C++, are designed for maximum computer efficiency whereas programmer efficiency is secondary. That's why C and C++ programs can be so hard to read and understand.)

Three popular teaching languages include BASIC, Logo, and Pascal. Besides making programming easy and fun, teaching languages also try to help you understand general programming principles that you can apply no matter what programming language you might later wish to know.

Getting back to BASIC

Back in 1963, programming was difficult because you had to know how the computer worked before you could write a program. As a result, only scientists, engineers, and mathematicians tended to program a computer.

Programming back in the early days of computers meant typing commands on a series of punch cards and feeding the punch cards into the computer. Then you had to return an hour later (or sometimes even overnight) to see the results of your program. If you made just one mistake, your program wouldn't run. Then you'd have to correct that one mistake and try to run your program again. If the computer found a second mistake, it wouldn't run the program either, and you'd have to try again. Needless to say, programming like this was tedious, slow, and frustrating.

At Dartmouth University, two professors, John Kemeny and Thomas Kurtz, decided that non-scientists could also benefit from computer programming by writing programs for themselves. (Scientists, engineers, and mathematicians tended only to write programs for themselves too, but how many ordinary people could use a program that would calculate a quadratic equation?)

Note

Both John Kemeny and Thomas Kurtz have created an "official" version of BASIC called True BASIC (www.truebasic.com). Despite this official version of BASIC, many other companies, such as Microsoft, have created variations, or dialects, of BASIC. These BASIC dialects — like Visual Basic and REALbasic — are actually more commonly used than the official True BASIC version.

Principles

Because programming was so difficult, both John Kemeny and Thomas Kurtz decided to create a new programming language specifically designed to teach novices how to program. The goals of their new programming language, dubbed BASIC (Beginner's All-purpose Symbolic Instruction Code), consists of several principles:

  • Be easy for beginners to understand and use.

  • Be a general-purpose programming language.

  • Be interactive.

  • Shield the user from understanding the computer hardware and operating system.

Descriptive commands

To make the language easy to understand, BASIC uses descriptive commands, which is something most programming languages don't use. For example, this assembly language program prints the words Hello, world! on-screen:

title   Hello World Program
dosseg
.model small
.stack 100h

.data
hello_message db 'Hello, world!',0dh,0ah,'$'

.code
main  proc
      mov    ax,@data
      mov    ds,ax

      mov    ah,9
      mov    dx,offset hello_message
      int    21h

      mov    ax,4C00h
      int    21h
main  endp
end   main

Looking at the preceding assembly language program, isn't what the preceding commands are telling the computer simple and intuitive? (If you answered, "Yes," you're either a computer genius or completely delusional.)

Now look at the command you need to use to print Hello, world! in BASIC:

PRINT "Hello, world!"

That's it — a single line. To print Hello, world! on-screen, you can either write a 17-line assembly language program (and risk making a mistake on all 17 different lines), or you can write a one-line BASIC program that's immediately obvious and intuitive.

Versatility

Both professors Kemeny and Kurtz didn't just want to create a programming language that was easy to understand, but they also wanted to create a programming language that could be put to practical use as well. That way, novices could understand BASIC and then use BASIC to write actual programs. After all, there's no point in using BASIC to teach programming only to force programmers to later use a completely different language if they wanted to write a useful program.

So BASIC not only provides descriptive commands like PRINT, but it also provides enough commands so you can write practically any type of program. The only type of program you can't create easily in BASIC are programs that require you to understand and access the actual hardware of the computer or operating systems.

Warning

One reason C programmers look down on BASIC as a toy language is because BASIC doesn't let you access the hardware or operating system like C does. But one reason why C programs fail is precisely because programmers incorrectly manipulate the computer hardware or operating system. By shielding you from the details of the computer or operating system, BASIC reduces the chance that your program accidentally crashes the computer.

Shielding you from having to know the technical details of the computer hardware or operating system is actually another goal of BASIC. By using BASIC, you can focus on solving a problem and not get distracted by the technical details of the computer.

Another goal of BASIC is to provide the programmer with instant feedback. Most programming languages force you to write an entire program before you can run it. BASIC, on the other hand, was originally designed to let you type in a single BASIC command at a time, and the computer tells you right away whether the command works.

Such instant feedback made BASIC programming much easier and faster than traditional programming languages. In addition, such rapid feedback helped novices quickly figure out what they were doing wrong so they could correct their mistakes right away.

Turtle graphics with Logo

BASIC introduced the idea of giving programmers instant feedback every time they typed in another BASIC command. If you typed in the following BASIC command:

PRINT 2+4

The BASIC interpreter would display the following on-screen:

6

Although giving programmers instant feedback made figuring out programming easier, a computer scientist named Seymour Papert felt that programming should be accessible to everyone, including children. Because few children are excited about adding 2+4 together and seeing the number 6 appear, Seymour Papert thought that a better form of instant feedback for children would involve seeing the results of your program visually.

So in 1967, Seymour invented a new programming language — Logo — which was designed especially for teaching children. The main idea behind Logo is that rather than create a program to solve abstract problems, like mathematical equations, Logo shows children how to draw pictures on-screen.

With Logo, you give commands to an imaginary robot commonly called a turtle. To write a program (and draw pictures), you have to give the "turtle" commands that tell it which way to move, when to start drawing pictures (known as turtle graphics), and when to move without drawing pictures. The following Logo program tells the turtle to draw a square, as shown in Figure 3-1.

PENDOWN
FORWARD 100
LEFT 90
FORWARD 100
LEFT 90
FORWARD 100
LEFT 90
FORWARD 100
LEFT 90
Programming in Logo means giving commands to a "turtle" and telling it where to move to draw pictures.

Figure I.3-1. Programming in Logo means giving commands to a "turtle" and telling it where to move to draw pictures.

By drawing pictures in Logo, even novice programmers could pick up the principles of programming without even realizing it. Logo has succeeded as an educational tool for both teaching programming and teaching students how to think logically and solve problems. Logo has proven popular for graphics, animation, music, mathematics, and even robotics.

Unlike BASIC though, Logo has remained primarily an educational tool with few programmers using Logo to develop commercial applications. As a result, you're hard pressed to find jobs available for Logo programmers; so Logo is a great language to find out a different way to program a computer, but it's not likely to translate into marketable programming skills any time in the near future. If you study Logo just to explore new ways of thinking, you won't be disappointed.

Note

Logo relies primarily on an interpreter instead of a compiler because an interpreter can give you instant feedback for each command you type into the computer.

The philosophy of Pascal

Unlike BASIC or Logo, which was designed to make programming interactive and fun, Pascal was designed to force programmers to design and organize a program using structured programming techniques (see Book I, Chapter 2).

Early versions of BASIC made it easy to write spaghetti programs because a BASIC program consisted essentially of one glob of commands lumped together inside a single file. The larger your BASIC program got, the more confusing it became to read and understand.

Pascal tackled this problem by encouraging programmers to divide a large program into smaller subprograms. Each subprogram would solve a single problem and act like a Lego building block. Slap enough subprograms together and you could create a much larger and more complicated program.

Although Pascal was initially designed to teach good programming habits, Pascal eventually became popular for writing many commercial programs. Early versions of the Macintosh operating system were even written in Pascal. Computer scientists eventually added so many features to Pascal that Pascal's power could nearly rival that of the C language.

Note

For a brief period of time during the 1980s, Pascal was arguably more popular than BASIC. One of the most popular Pascal compilers of all time was Borland Software's Turbo Pascal, which later morphed into the event-driven, object-oriented programming language that's now known as Delphi (www.turboexplorer.com).

Pascal's greatest strength was that it was almost as easy to understand as BASIC and almost as fast, efficient, and powerful as C. Reading a Pascal program was much easier than reading a similar C program, as the following programs demonstrate.

Program Hello_World;

Begin
  Writeln ('Hello World!'),
End.

Without even knowing much about programming, you can figure out what the preceding Pascal program does, just because Pascal commands consist of descriptive words. Now take a look at the equivalent C program, which is fairly easy to understand, but littered with cryptic characters and symbols:

#include <stdio.h>

main()
{
  printf ("Hello World!
");
}

Although Pascal combined the best features from BASIC and C, Pascal's popularity also plummeted because of these similarities, too.

BASIC started adopting structured programming techniques, which made BASIC easier to understand than Pascal but just as easy to write organized programs like Pascal. On the other extreme, C started adopting similar structured programming techniques as well, so you had the power of C with nearly all the advantages of structured programming emphasized by Pascal. Given a choice between using Pascal, which was almost as easy to use as BASIC and almost as powerful as C, or using BASIC (which was easier to figure out than Pascal) or C (which was more powerful than Pascal), programmers found few reasons to use Pascal any more.

Despite Pascal's fading popularity, you can still understand Pascal as a first language and then find a job writing and modifying Pascal programs. However, Pascal programmers aren't in as much demand as C programmers, so study Pascal to better understand good programming principles and then apply those skills to writing programs in other languages.

Note

The inventor of Pascal, Niklaus Wirth, eventually created an improved version of Pascal dubbed Modula-2. The U.S. Department of Defense created a similar programming language, called Ada, which was also based on Pascal. Like Modula-2, Ada was designed to encourage structured programming and allow programmers to create and manage large programs by storing subprograms in separate files called modules (in Modula-2) and packages (in Ada).

Other teaching languages

Although BASIC, Logo, and Pascal may be the more popular teaching languages available, plenty of other people have created their own languages to teach people how to program a computer. Because these languages are developed by individuals or companies, they're proprietary languages — a single company or individual controls the language. (In comparison, languages like BASIC, Logo, and Pascal all have official "standards," although most companies that offer BASIC, Logo, or Pascal compilers often deviate from the official "standard" anyway.)

The main disadvantage of proprietary languages is that you probably won't find much work writing programs in any little-known, proprietary language. The advantage of proprietary languages is that they can often be much better at solving specific types of problems. In this case, proprietary teaching languages can make understanding programming much easier and more exciting than BASIC, Logo, or Pascal.

KPL (Kid's Programming Language)

One of the newest proprietary teaching languages is KPL (Kid's Programming Language), which you can download for free from www.kidsprogramminglanguage.com. The idea behind KPL is that kids like playing video games, so why not create a programming language that can teach kids how to program their own video games?

Creating video games in languages like BASIC, Logo, or Pascal is difficult, but creating video games in KPL is easy, as shown in Figure 3-2. In the process of creating real video games that they can play, kids also learn to read, type, do math, and understand the principles of computer programming.

KPL teaches kids how to program by letting them create their own video games.

Figure I.3-2. KPL teaches kids how to program by letting them create their own video games.

If the idea of using a "kid's" programming language to figure out programming bothers you, grab a copy of KPL's more sophisticated version, Phrogram (www.phrogram.com). Like KPL, Phrogram is free. (If you want to compile your video games into programs that you can give or sell to others, buy the commercial version of Phrogram.)

Both KPL and Phrogram use a proprietary language that combines the readability of Pascal with the object-oriented features of more advanced languages, as shown in the following example:

Program HelloWorld
    Method Main()
        PrintLine ("Hello, World!")
    End Method
End Program

The goal of KPL and Phrogram is to make programming simple, easy, and fun by focusing on graphics, sound, and animation so you can create video games. After you know how to program with KPL or Phrogram, you can migrate to more popular languages later.

Alice

The latest programming technique for creating, organizing, and maintaining large programs is object-oriented programming. Unfortunately, figuring out object-oriented programming can be difficult, especially for beginners who already have enough trouble just figuring out how to program a computer.

So to help beginners understand object-oriented programming, Carnegie Mellon University has created a free programming language dubbed Alice (www.alice.org). To make programming fun and teach object-oriented principles at the same time, Alice lets beginners write simple programs to animate characters on the screen, as shown in Figure 3-3.

When you write an Alice program, your commands create an animated character on-screen. Then you need to write additional commands to tell that animated character how to move to create a simple story. In the process of telling the animated character how to move, you wind up discovering both how to program and how to use object-oriented principles while having fun in the process.

Like most teaching programming languages, Alice programming uses plain English commands, like move forward or play sound. By using simple commands, Alice lets you focus on understanding the principles of object-oriented programming without getting bogged down in understanding the peculiar syntax of a specific programming language.

An Alice program creates an animated character on-screen and moves it around.

Figure I.3-3. An Alice program creates an animated character on-screen and moves it around.

Lego Mindstorms

Kids love building things with Lego building bricks, so to encourage kids to build actual working robots, Lego has released their Lego robot-building kit, Mindstorms NXT. Not only can you build a working robot with Lego bricks, but you can also program it using the Lego NXT-G programming language.

To write a program in NXT-G, you don't have to type a thing. Instead, you create a program by arranging icons that represent different type of actions your robot can do, such as move forward or respond to a light. After writing your program on your computer, you load that program into your Lego robot and watch it go.

By using Lego Mindstorms NXT, anyone can figure out both programming skills and robot-making skills. Unlike KPL, Logo, or Alice, Lego Mindstorms NXT lets you see your working program in action as a walking, rolling, or crawling Lego robot.

Programming a killer robot

Studying how to program by controlling a Lego robot can be fun, but to combine the thrill of controlling a robot with the joy of playing a video game, computer scientists have also created games that let you write a simple program for controlling a battling robot, as shown in Figure 3-4.

Using a battle robot to study programming can make programming more exciting.

Figure I.3-4. Using a battle robot to study programming can make programming more exciting.

Instead of writing a program just to control a robot, these games force you to write a program to move a robot on-screen, search for other robots nearby, and then attack those other robots with a cannon.

After you finish your program, you can see the results by watching your robot battle another robot in a gladiator-style battle. Write a "good" program, and your robot can defeat another robot. Write a "bad" program, and your robot gets blasted into chunks of (virtual) charred metal.

To program a "battling robot," use a simplified version of a popular programming language, such as Java or C++. That way you not only figure out the basics of a popular programming language, but you can also start writing "real" programs that actually do something interesting right from the start. Table 3-1 lists some popular "battling robot" programming games.

Table I.3-1. Popular "Battling Robot" Programming Games

Program Name

Language Used

Where to Find It

Robocode

Java

http://robocode.source-forge.net

C++ Robots

C++

http://www.gamerz.net/c++robots

CRobots-3D

C

http://antru.ru/crobots3d

"Curly Bracket" Languages

If you know BASIC or Pascal, you can rush out and start writing programs without ever needing to know another programming language again for the rest of your life. However, it's far more common for programmers to know two or more programming languages. Typically, the one language that most programmers know is one that belongs to a family of related languages known as the "curly bracket" family.

The "curly bracket" language family gets its name because they use curly brackets to define the start and ending of a block of commands, such as

#include <stdio.h>

void main()
{
    printf("Notice how the curly brackets
");
    printf("identify the beginning and end
");
    printf("of your commands?
");
}

Note

Rather than use curly brackets, many other programming languages use descriptive words, like Begin or End, to identify the start and end of a block of code.

The most popular "curly bracket" language is C. The C language is popular for several reasons:

  • Power

  • Efficiency

  • Portability

The power of C

The C language is a curious combination of assembly language and high-level languages, like BASIC. Like assembly language, C provides commands for directly manipulating every part of the computer, including memory, hard disks, and printers. Like a high-level language, C lets you focus on the logic of your program without worrying about the technical details of the computer so you get the best of both assembly language and high-level languages.

Because C programs are nearly (note the emphasis on the word "nearly") as easy to write and understand as high-level languages but still give you the power of accessing the computer's hardware like assembly language, C is often used for creating large, complicated programs (such as operating systems and word processors) along with more exotic programs (like antivirus utilities or disk diagnostic programs).

Warning

With great power comes great responsibility, and C is no exception. Because C programs can access every part of the computer's hardware, C programs can fail dramatically by crashing other programs, including the entire operating system.

The efficiency of C

A C compiler tends to create smaller, faster, more efficient programs than compilers for other programming languages. The reason is that the C language is much simpler and thus easier to translate into equivalent machine language commands.

What makes the C language simpler is its small number of commands or keywords. Keywords are special commands used in every programming language. The more keywords a programming language uses, the fewer commands you need to make the computer do something. The fewer keywords a programming language offers, the more commands you need to make the computer do something.

Tip

Think of keywords like words in a human language. The fewer words you know, the more limited your communication is. If a little kid only knows the word hot, he can only express himself in a limited manner, such as describing something as very hot, a little hot, or not so hot. However, if a kid knows a lot of different words, he can express himself much better. Rather than use two or more words to describe something as very hot, a little hot, or not so hot, a kid with a richer vocabulary could describe the same items as scalding, warm, or cool.

A programming language with a lot of keywords means you can write a program with fewer commands. That's great from the programmer's point of view, but inefficient from the computer's point of view.

The more keywords used in a language, the more work the compiler needs to do to translate all these keywords into machine language. As a result, programs written in languages that use a lot of keywords tend to run much slower than programs written in C.

A C program compiles to smaller, more efficient machine language commands because instead of offering a large number of keywords, the C language offers just a handful of keywords. This makes it easy for a compiler to translate the limited number of keywords into machine language.

However, as a programmer, you need to use C's limited number of keywords to create subprograms that mimic the built-in commands of other programming languages. Because this can be impractical, the C language often includes libraries of subprograms that mimic the built-in commands of other programming languages.

The bottom line is that C programs tend to run faster and more efficiently than equivalent programs written in other programming languages. So if you need speed, efficiency, and access to the computer hardware, the C language is the most popular choice.

The portability of C

By using much fewer commands than most programming languages, the C language makes it easy to create compilers that can translate a C program into machine language. Because it's so easy to create C compilers, compared to creating compilers for other programming languages, you can find a C compiler for nearly every computer and operating system.

Theoretically, this means it's possible to take a C program, written on Windows, copy it to another computer and operating system, and run that program on a different operating system, like Linux or Mac OS X, with little or no modifications. When you can copy and run a program on multiple computers and operating systems, the program (and the language it's written in) is portable.

So not only does C create small, fast, and efficient programs, but C also allows you to copy and run your program on different operating systems and computers. Given all these advantages, the C language has few equivalent rivals.

Adding object-oriented programming with C++

Although the C programming language is popular, it's not perfect. When object-oriented programming became popular for designing and maintaining large programs, computer scientists created an object-oriented version of C called C++.

Because more people are writing and organizing large programs with object-oriented programming, more programs are being written in C++. Some people study C so they can understand the peculiarities of the C language. When they feel comfortable with C, they start studying C++ and object-oriented programming.

Other people just skip C and start studying C++ right away. The theory is that as a professional programmer, you'll probably wind up writing and modifying C++ programs anyway, so you might as well study C++ from the start. After you know C++, you pretty much know enough to teach yourself how to write and modify C programs, too.

True portability with Java

Although C and C++ programs are supposed to be portable — you can copy and run them on other computers — they're not really. Sometimes, you have to make minor changes to get a C/C++ program to run on another computer, but more often, you have to make major changes.

So that's why Sun Microsystems created the Java programming language. Like C++, Java is also based on the C language, but includes several features to make Java programs safer than C or C++ programs. Specifically, Java isolates the programmer from directly accessing the computer's memory. This reduces the power of Java somewhat, but translates into safer programs that (hopefully) won't crash as often as C/C++ programs do.

Perhaps the most important feature of Java is its portability. Rather than attempt to compile a Java program into machine language for different types of processors, Java compiles Java programs into an intermediate file format called bytecode or pseudocode (also called p-code).

To run a Java program that's compiled into bytecode, you need a free program, or a Java virtual machine (VM). As long as a computer has a Java VM, it can run a Java compiled bytecode program.

Note

Like most promises made by computer scientists, Java programs aren't always portable. It's perfectly possible to write a Java program correctly, compile it to bytecode format, and make the program run perfectly on a specific computer and operating system. But copy that same bytecode program to another computer, and suddenly, the Java program doesn't run correctly. The problem can occur when the Java VM, on the other computer, has errors in it. So although Java programs are more portable than C/C++ programs, they still aren't 100 percent portable.

Besides creating full-fledged programs, like word processors or spreadsheets, Java can also create smaller programs, or applets, which can be used to create interactive Web pages.

So if you're looking for a programming language that makes programming safer and more portable, consider Java. Java programmers are in demand almost as much as C/C++ programmers, and the similarities between Java and C/C++ make it relatively easy to understand after you know C. (Or you can study Java first and then study C/C++ later.)

Safer programming with C#

Microsoft took one look at C/C++ and decided they could create an improved language, which they dubbed C# (pronounced C-sharp).

C# advantages

C# has a couple advantages over languages such as C, C++, and even Java.

Object oriented

One main advantage of C# over C++ is that C# is a true object-oriented programming language, so you have to use object-oriented programming to write a program in C#.

Forcing you to use only object-oriented programming techniques might seem like a drawback until you realize that C++ is a hybrid language that lets you choose whether to use object-oriented programming. Although C++ gives you, the programmer, more flexibility, C++ programs can also be a mish-mash of structured programming mingled in with object-oriented programming.

Trying to decipher such a mix of programming techniques can be confusing. By forcing all programmers to use object-oriented programming (and isolate their structured programming techniques only inside objects), C# programs can be much easier to understand and modify.

Type safe

A second advantage of C# is type-safe language. Basically, if a C# program stores data, such as a whole number (such as 3 or 49, but not 5.48), the C# compiler checks to make sure no other part of the program accidentally changes that whole number into a decimal.

With languages that aren't type-safe, the compiler lets a program change data types, such as storing a decimal or negative number where the program expects a whole number. Obviously, if your program is expecting a whole number but instead receives a decimal number, the program may get confused and crash.

.NET compatibility

Because Microsoft invented C#, they also invented a special program — the .NET framework. The idea behind the .NET framework is that instead of compiling a C# program into machine language, you compile a C# program into p-code, which is similar to the bytecode intermediate file format of Java.

Note

Theoretically, you can run a C# program on any computer that has the .NET framework on it. Realistically, the only computers that have the .NET framework are computers running the Windows operating system. So if you write a C# program, you're essentially locked into writing programs that run only under Windows. A band of hardy programmers are trying to rewrite the .NET framework to run under the Linux operating system, but this doesn't have the official blessing of Microsoft, so C# programs are guaranteed to work only on Windows.

The .NET framework allows you to both

  • Run C# programs on any computer with the .NET framework.

  • Write programs in multiple languages that all link together through the .NET framework, as shown in Figure 3-5.

By letting you write a program with different languages, the .NET framework lets you use each language's strengths without being forced to put up with that language's weaknesses.

Note

The only programming languages you can use with the .NET framework are languages specifically designed to work with the .NET framework. So if you want to write a program using a combination of C# and BASIC, you have to find a BASIC compiler that works with the .NET framework, such as Microsoft's own Visual Basic language.

A final advantage of the .NET framework is that it lets you use event-driven programming to create your user interface and then write event handlers in any .NET language, such as C#.

The .NET framework can tie programs, written in multiple languages, into a single program.

Figure I.3-5. The .NET framework can tie programs, written in multiple languages, into a single program.

Because C# is similar to C, C++, and Java, you can study C# first and then study the other languages (or vice versa). The main drawback with C# is that you can only use C# to write Windows programs. If you want to write programs for Linux, Mac OS X, or other operating systems, you can't use C#.

For that reason, many programmers prefer to first understand C or C++, and then understand C#. If you don't mind being limited to writing only Windows programs, go ahead and study C#.

Choosing a curly bracket language

If you plan to write programs professionally, you'll probably need to know a curly bracket language. If you know C, C++, Java, or C#, you can pick up any of the other curly bracket languages fairly easily:

  • Knowing C can be great because it's the basis for all the other languages.

    While figuring out C, you can get used to its cryptic syntax without having to worry about understanding object-oriented programming at the same time.

  • Begin with C++ if you want to get started using the (currently) most popular programming language in the world.

    While figuring out C++, you can ignore its object-oriented features. After you feel comfortable with writing C++ programs, you can gradually start developing object-oriented programming techniques as well.

If you want to write programs that can run on different computers, use Java:

  • Java forces you to know object-oriented programming right from the start (like C#), so knowing Java means you can figure out object-oriented programming at the same time. Because Java isn't as confusing as C or C++, understanding Java first is likely much easier than understanding C or C++.

  • If you're using only a Windows computer, consider trying C#.

The C# language is quickly becoming the standard language for writing Windows programs, so if that's what you want to do, figuring out C# is your best bet. As long as you know at least one curly bracket language, you know one of the most popular programming languages in the world.

Artificial Intelligence Languages

Programming languages, such as C and BASIC, are often considered procedural or functional languages because they divide a large program into separate procedures or functions that tell the computer how to solve a problem step by step.

Although telling the computer what to do step by step might seem like the most logical way to program a computer, another way to program a computer is by using a declarative language. Instead of describing how to solve a problem, declarative programming languages describe

  • Facts: Information about the problem

  • Rules: Relationships between this information

By using facts and rules, programs written in declarative languages can literally figure out an answer on their own without being told explicitly how to do it.

Warning

Ultimately, every program, including those written in declarative languages, must get translated into machine language. That means every program must eventually tell the computer how to solve a problem step by step. Declarative languages simply free you from having to describe these steps to the computer.

The most popular declarative programming language is Prolog (short for Programming in Logic). A typical Prolog fact might look like this:

father("Sally", "Jesse").

The preceding fact tells the computer that Jesse is the father of Sally. Now if you want to know who the father of Sally might be, you could ask the following:

?- father("Sally", X).

Using the fact that earlier stated that the father of Sally was Jesse, the preceding Prolog command would simply return:

X = "Jesse".

At this point, Prolog simply uses a pre-defined fact to come up with an answer. Notice that even in this simple example, no instructions told the Prolog program how to use the fact that Jesse is the father of Sally.

A list of facts by themselves can be made more useful by including rules that define relationships between facts. Consider the following Prolog program that defines two facts and one rule:

father("Jesse", "Frank").
father("Sally", "Jesse").

grandFather(Person, GrandFather) :-
    father(Person, Father),
    father(Father, GrandFather).

The two facts tell the computer that Frank is the father of Jesse, and Jesse is the father of Sally. The grandfather rule tells the computer that someone is a grandfather if they're the father of someone's father.

Suppose you typed the following Prolog command:

?- grandFather("Sally", Y).

The Prolog program tells the computer to use its known facts and rules to deduce an answer, which is:

Y = "Frank".

In other words, Frank is the grandfather of Sally. (Frank is the father of Jesse, and Jesse is the father of Sally.)

Just from this simple example, you can see how different a Prolog program works compared to a program written in BASIC or C. Instead of telling the computer how to solve a problem, declarative programming languages let you state the facts and the rules for manipulating those facts so the computer can figure out how to solve the problem.

Note

A Prolog program can actually create additional facts (and delete old facts) while it's running, so it can appear to think. That's why Prolog is commonly used in the field of artificial intelligence. The whole idea behind artificial intelligence is to make computers smarter and literally think for themselves. (That's because computer scientists have pretty much given up hope that people will ever get smarter or begin to think for themselves.)

Warning

Just as knowing two or more human languages can help you better understand how people communicate, so can knowing two or more different programming languages help you better understand how programming can work. The key is to figure out two different programming languages, like C++ and Prolog. Knowing two similar programming languages, like C++ and C#, doesn't show you much of a difference.

One of the most popular programming languages favored by the artificial intelligence community is LISP (which stands for LISt Processing). The basic idea behind LISP is that everything is a list that can be manipulated by the computer. For example, a typical LISP command might look like this:

(print "Hello world")

This LISP command is a list that displays the following on-screen:

"Hello world"

The enclosing parentheses define the start and end of a list. A different way to print "Hello world" on-screen would be to use this LISP command:

(list "Hello world")

The preceding command would print the following:

("Hello world")

In this case, the list command tells the computer to treat "Hello world" as a list, so it encloses it in parentheses. Now consider what happens if you insert a command (list) inside another command (list):

(list (print "Hello world"))

This is how the preceding LISP command works:

  1. The innermost command (list) runs first, which is the (print "Hello world") list. This displays the following on-screen:

    "Hello world"
  2. From the computer's point of view, the original LISP command now looks like this:

    (list "Hello world")

    This command now displays the following on-screen:

    ("Hello world")

So the command

(list (print "Hello world"))

prints the following:

"Hello world"
("Hello world")

In the previous example, LISP treats the (print "Hello world") list first as a command (to print "Hello world" on-screen) and then as data to feed into the list command to display the list ("Hello world") on-screen.

With traditional programming languages, like C or BASIC, commands and data are separate where data may change but commands never change. With LISP, a list can be both a command and data. That makes it possible for a program to change its lists (treated either as data or as a command), essentially allowing a program to modify itself while running, which can mimic the learning and thinking process of a human being.

As you can see, both LISP and Prolog offer radically different ways to program a computer compared to C or BASIC. Just as languages, like C and BASIC, free you from the tedium of manipulating registers and memory addresses to program a computer, so do LISP and Prolog free you from the tedium of writing explicit step-by-step instructions to program a computer.

Note

Although the idea that a LISP program can modify its own commands might seem like science fiction, LISP is actually the second oldest programming language still in use today. (FORTRAN is the oldest programming language still in popular use.) LISP was invented in 1958, and although it's been used primarily as a research tool, people have created commercial programs using LISP.

Scripting Languages

Languages, such as C and C++, are often dubbed system programming languages because they can create programs that access and manipulate the hardware of a computer, such as an operating system (Linux and Windows) or a utility program (an antivirus or anti-spyware program). However, using systems programming languages, like C++, for everything can get clumsy.

Rather than write an entirely new program from scratch using a systems programming language, more people are likely to use an existing program and customize it in some way. Programming languages that customize existing programs are typically called scripting languages.

Scripting languages work with one or more existing programs and act as "glue" that connects different parts of an existing program together. For example, the Microsoft Office suite consists of a word processor (Microsoft Word), a spreadsheet (Microsoft Excel), and a database (Microsoft Access). By using the scripting language that comes with Microsoft Office, you can write a program that can automatically yank information from an Access database, create a chart from that information in an Excel spreadsheet, and then copy both the data and its accompanying chart into a Word document for printing.

Trying to yank information from a database, create a chart with it, and print the data and chart using a systems programming language, like C++ or Pascal, would mean creating everything from scratch including a database, a spreadsheet, and a word processor. By using a scripting language, you use existing components and simply "glue" them together. The existing components do all the work while the scripting language just passes the data from one component to another.

Because scripting languages work with existing programs, they differ from traditional programming languages (like C++ or BASIC) in two important ways.

First, because scripting languages work with one or more existing programs, scripting languages are usually interpreted rather than compiled. Therefore, if someone else wants to run your program, written in a scripting language, they need the source code to your program along with all the programs your scripting program needs, such as Microsoft Word and Microsoft Access. As a result, scripting languages are used less to create commercial applications and more to create custom solutions.

Second, to make scripting languages easy to understand and use, even for non-programmers, most scripting languages are typeless languages. (Systems programming languages, like C++ and Pascal, are strongly-typed or type-safe languages.)

Strongly-typed languages force you to define the type of data your program can use at any given time. So if your program asks the user to type a name, a strongly-typed language makes sure that the user doesn't type in a number by mistake. This protects a program from accidentally trying to manipulate the wrong type of data, which could crash the program as a result.

In comparison, typeless languages don't care what type of data the program stores at any given time. This makes writing programs much easier because your program assumes if it's going to yank data from a particular program, such as Microsoft Excel, the data is probably going to be the right "type" anyway, so type-checking would just be restrictive and tedious.

Scripting languages are typically used in four different ways:

  • To automate repetitive tasks

  • To customize the behavior of one or more programs

  • To transfer data between two or more programs

  • To create standalone programs

Automating a program

At the simplest level, scripting languages (also called macro languages) can automate repetitive tasks that essentially record your keystrokes so you can play them back at a later time. For example, if you regularly type the term Campylobacteriosis (a disease caused by the Campylobacter bacteria), you have two choices:

  • Type that phrase manually and hope that you spell it correctly each time.

  • Type this phrase just once (the easier solution), record your keystrokes, and use those captured keystrokes to create a scripting language program that you can save and run in the future.

Figure 3-6 shows a scripting language, VBA (Visual Basic for Applications), that's captured keystrokes and saved them in a VBA scripting language program.

Recording keystrokes automatically creates the equivalent VBA code in Microsoft Word.

Figure I.3-6. Recording keystrokes automatically creates the equivalent VBA code in Microsoft Word.

Customizing a program

Besides letting you automate a program, scripting languages also let you customize a program, which can make the program easier. For example, you might have a spreadsheet that calculates your company's invoices. However, to use this spreadsheet, you need to know the specific place in the spreadsheet to type new invoice information. Type this information in the wrong place, and the spreadsheet doesn't work right.

To avoid this problem, you can write a program in a scripting language that can display a window with boxes to type in new invoice information. Then the scripting language program automatically plugs that new information in the correct place in the spreadsheet every time.

For even more power, a scripting language can combine automation with customization to make programs perform tasks on their own. If you use the Mac OS X operating system, you can use its built-in scripting language — AppleScript — to control your Macintosh.

For example, you can write an AppleScript program that tells your computer to download files over the Internet at a specific time each day, move files from one folder to another while renaming those files in the process, or retrieve waiting e-mail messages and sort them into categories. Figure 3-7 shows an AppleScript program that can retrieve stock quotes off the Internet.

AppleScript lets you customize and automate the Mac OS X operating system.

Figure I.3-7. AppleScript lets you customize and automate the Mac OS X operating system.

Transferring data among multiple programs

Built-in scripting languages can help you automate or customize a program, but what if you use a program that doesn't include a scripting language? Or what if you need to transfer data between two or more programs, but neither program uses the same scripting language? In these cases, you'll need to use a scripting language that isn't tied to any particular program, such as Perl, Python, Ruby, or JavaScript.

When scripting languages link two or more programs together, the scripting language programs are often referred to as glue. So if you have a Web page that lets users type in their names, addresses, and credit card numbers, and a database program that stores customer information, you could use a scripting program to glue the Web page to the database. The user would type information into the Web page, and the scripting language would then yank this data off the Web page and shove it into the database.

By gluing programs together, scripting languages let you combine existing programs to create custom applications. Because scripting languages are interpreted rather than compiled, they can run on any computer with the proper language interpreter. So whether you use Windows, Linux, or Mac OS X, you can still use the same scripting language (and programs) on different computers.

Creating separate applications

Because scripting languages are so easy to understand and use, some scripting languages can create standalone programs. One of the most popular scripting languages used for developing standalone programs is Visual Basic.

Unlike scripting languages, like Perl or Python, which link separate programs together, Visual Basic links program components together. To create a Visual Basic program, you design the program's user interface consisting of pull-down menus, buttons, and check boxes. Then you write Visual Basic commands to link all these separate user interface components together to create a working program.

Note

Like most scripting languages, Visual Basic isn't compiled to machine code like traditional programming languages, such as C++. Instead, Visual Basic programs get stored in an intermediate (p-code) file. To run a Visual Basic program, include a special run-time file, which interprets the p-code file and actually runs the program.

Note

Another popular scripting language used to create standalone applications is Revolution, which uses a scripting language similar to AppleScript. Like Visual Basic, Revolution doesn't compile your programs into machine code. Instead, Revolution creates an intermediate file (p-code) that attaches directly to a run-time interpreter. Because the Revolution run-time interpreter can run on different operating systems, you can create a Revolution program that can run on different operating systems (such as Windows, Linux, and Mac OS X).

Database Programming Languages

Programming languages, such as C++, are general-purpose languages because they can literally be used to create any type of program from operating systems and word processors to antivirus utilities and video games. However, in the business world, the most common type of custom programs needed are those that store and retrieve data, such as invoices, inventory, customer information, and so on.

Although it's possible to write a database program in C++, that essentially doubles the amount of work you need to do.

  • Write your program's user interface and commands for manipulating data.

  • Write commands to store and retrieve data, essentially creating a database program from scratch.

Rather than rewrite (and test) your own database program, it's much easier just to customize an existing database program instead. Many database programs include their own programming language. By using a database programming language, you just have to customize the appearance of the database program by designing a user interface along with commands for manipulating data. The database program does all the work of storing, retrieving, and rearranging the actual data so you can focus on what your program should do with data and not with the technical details for how it should store data.

The dBASE programming language

One of the earliest database programming languages for personal computers was dBASE. By using the dBASE language, you could create a user interface, write commands for storing data, and write additional commands for retrieving and manipulating that data.

The following dBASE code creates a simple menu and waits for the user to type in a choice by choosing a number from 1-6.

SET TALK OFF
USE CUSTOMER.DAT

STORE 0 TO choice
STORE " No. Name    Title   Address" TO head
DO WHILE choice <> 6
ERASE
@ 3,1 SAY "
   ======================================================"
@ 4,26 SAY "Main Menu"

@ 5,1 SAY "
   ======================================================"
@ 8,26 SAY "1) Display Customer File"
@ 10,26 SAY "2) Display Record"
@ 12,26 SAY "3) Input Record"
@ 14,26 SAY "4) Delete Record"
@ 16,26 SAY "5) Print Customer File"
@ 18,26 SAY "6) Exit"
STORE 0 TO choice

Although the dBASE code may initially look cryptic, you can see how the dBASE language works like traditional programming languages. The SAY command simply prints information on-screen much like the BASIC PRINT command or the C printf command.

Note

At one time, dBASE was so popular that competing companies released similar dBASE-like languages with names like FoxPro, dBXL, and Clipper. The original dBASE language and all of its various dialects are referred to under the generic name of xBASE.

Early versions of the dBASE language were interpreted but later versions were compiled, allowing programmers to write complete database programs and sell them to others. The main difference between dBASE and languages such as C++ is that traditional programming languages can be used to create anything whereas dBASE is meant just for creating programs that need to store and retrieve data.

Note

Three popular database programming languages are

  • Clarion (www.softvelocity.com)

  • 4th Dimension (www.4d.com)

  • Alpha Five (www.alphasoftware.com)

Warning

One major drawback of database programming languages is that not as many programmers know them compared to traditional languages, like C++ and BASIC. Therefore, if someone writes a custom database application with dBASE, Clarion, 4th Dimension, or Alpha Five, nobody else can modify that program unless they also know dBASE, Clarion, 4th Dimension, or Alpha Five.

Adding database access to traditional languages

Because so many businesses needed programs that manipulated data (hotel reservations, hospital patient tracking, inventory management, and so on), programmers often had to teach themselves a database programming language, such as dBASE, rather than use their favorite programming languages, like C++ or BASIC.

To let programmers continue using their favorite (and familiar) programming languages, many companies started adding database access to their language compilers. Two popular languages that offer database access features include Microsoft's Visual Basic and Borland's Delphi (based on the Pascal programming language).

By adding database features to a familiar language, programmers could continue using their favorite programming language without having to know a completely different database programming language. In addition, programmers could use their favorite language to add more sophisticated features that might be impossible for a database programming language to create, such as the ability to display animation.

Customizing database programs

Perhaps the simplest way to create database applications is to use a database program that includes its own scripting language. By writing simple programs (scripts), you can glue the different parts of your database together, such as a script that transfers data from a database file to a user interface.

One of the most popular Windows database programs, Microsoft Access, offers the VBA (Visual Basic for Applications) scripting language. Of course, Microsoft Access runs only on the Windows operating system, so if you need to create database applications that run on both Windows and Mac OS X, you can choose FileMaker (www.filemaker.com).

Like Microsoft Access, FileMaker offers a scripting language: ScriptMaker. Best of all, you can create standalone versions of your FileMaker databases and sell them to anyone who uses Windows or Mac OS X.

Note

Many specialized database programs, such as medical office management programs or multilevel marketing programs, have been created using FileMaker.

Comparing Programming Languages

With so many different programming languages available, the real question isn't "Which programming language should I study and use?" Instead, the real question you should ask is "How can I become a better programmer and choose the best language for solving a particular problem?"

Programming languages just offer different ways to express your ideas, and depending on what you need to accomplish, sometimes a language like C++ is best and sometimes another language like LISP might be better. The goal is to choose the best language for the job.

Unfortunately, it's impossible to know and master every programming language, so it's usually best to focus on mastering two or three languages instead. The more you know about using a particular language, the faster and more efficient you can write programs in that language.

Warning

A mediocre programmer using a programming language designed for a particular job is likely more efficient than an expert programmer using an inappropriate language for that same job. Assembly language might create the fastest and most efficient programs, but if you need to write a program quickly and you don't care about efficiency, a scripting language like Perl might be much easier and faster. Sometimes, a program that gets the job done now is preferable to a program that works ten times as fast but takes a million times longer to write.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset