Learning BASIC

January 30th, 2008 yudi Posted in Learn The Language No Comments »

 

If you’re interested in learning to program, you may wonder, “Why not jump right in and start learning C++?” There’s nothing wrong with learning C++ right away, but the cryptic nature of C++ can be as confusing to a novice programmer as reading and writing Chinese or Arabic can be to a native English speaker. If you try to learn C++ right away, the technical details of learning the language can often interfere with understanding the general principles of programming.

That’s why this site introduces programming concepts, using BASIC for a couple of reasons:

  • BASIC was specially designed to teach beginners how to program, so it’s one of the easiest languages for anyone to learn. Most BASIC commands are similar to English words, which makes learning easier in much the same way that native English speakers find that learning to read and write French or Spanish is easier than learning to read and write Arabic and Chinese.

  • BASIC is powerful, so after you learn BASIC, you can apply your BASIC programming skills right away to create commercial-quality programs.

Liberty BASIC uses a simple BASIC dialect so you can start learning programming right away. After you understand how programming in the BASIC language works, you can graduate to REALbasic, which offers a more advanced BASIC dialect that includes object-oriented programming features, commonly found in more powerful programming languages like C++.

Liberty BASIC is a shareware program, so if you find it useful, consider paying for it. REALbasic is a trial version that you can use for a limited time. When this trial period is over, you have to buy a copy of REALbasic if you want to continue using it.

 

Getting to know Liberty BASIC

Liberty BASIC closely follows the goals of the original BASIC programming language, which emphasizes simplicity. The entire Liberty BASIC program to display It works! on-screen consists of a single line:

 


PRINT “It works!”
Image from book

Technical StuffThe philosophy of BASIC

In the early days of computers, programming was slow. First you had to write your commands and store them in a file. Then you had to compile them into a program. Finally, you had to run your program to test whether it worked. If it didn’t work, you had to edit your commands, compile them again, and run the new program once more.

The problem was that most compilers were slow. For a large program, it was customary to compile a program overnight and then return the next morning to see whether it worked. Obviously, such a slow process made programming tedious and frustrating. So the whole idea behind BASIC was to make programming fun by giving you instant feedback on whether your program would work.

Instead of using a compiler, the first BASIC language used an interpreter so programmers could type a BASIC command into the computer and immediately see whether it would work. Not only did BASIC give instant feedback so people could program faster, but it also used more English-like words for its commands to make programming easier, especially for beginners. In fact, BASIC is actually an acronym that stands for Beginner’s All-purpose Symbolic Instruction Code.

Although BASIC compilers are now just as fast as BASIC interpreters, the central goal of BASIC remains the same: Make programming easy for beginners while still being powerful enough to create commercial-quality programs.

Image from book
 

Remember In BASIC, you can type commands in all uppercase (PRINT), lowercase (print), or a combination of the two (Print).

Almost anyone can figure out how the previous Liberty BASIC program works without knowing anything about programming. Novices often start learning to program in BASIC because it makes programming easy and simple. When you understand the fundamental principles of programming, you can always graduate to a more powerful (and complicated) language like C++, or you can just continue using BASIC.

 

Getting to know REALbasic

REALbasic is an object-oriented version of BASIC that includes rapid application development (RAD) features that let you design a user interface by drawing buttons, menus, and other types of controls on a window.

To write a REALbasic program, you typically design the user interface and then write BASIC code to make user interface actually respond to the user and do something useful. REALbasic programs consist of two parts: a user interface and the BASIC code that makes the user interface work. So to display It works! on-screen, you could create a user interface with a single button on it.

When you run this program, it displays the user interface and waits until the user clicks the button. The moment the user clicks the button, the following BASIC code runs:

 


Sub Action()                                      ?1
  MsgBox “It works!”                              ?2
End Sub                                           ?3

Although still fairly easy to understand, the REALbasic program consists of three lines that work as follows:

  • ?1 The Sub keyword is short for subprogram and identifies the BASIC code as a self-contained chunk of code that’s part of a larger program (hence the prefix sub- in subprogram). The Action keyword tells the computer to run the commands in this subprogram when the user clicks the button displayed on the user interface .

  • ?2 The MsgBox command tells the computer to display a small window, called a message box, on-screen with the words It works! shown inside.

  • ?3 The End Sub keywords define the end of the subprogram.

AddThis Social Bookmark Button

Learning C++

January 30th, 2008 yudi Posted in Learn The Language No Comments »

C++ is currently one of the most widely used languages in the world today, so gaining some familiarity with C++ and related programming languages (such as C, C#, and Java) is important.

Although this site won’t cover everything about C++, it shows you enough to understand how C++ programs work. After you understand C++, you can apply your knowledge to learning more about C++ or similar languages such as C# or Java.

If you have Windows, you can install the gcc compiler, and an accompanying editor and debugger called Dev-C++, off this book’s CD. If you have Mac OS X, you can install a free C++ compiler on your Macintosh that runs under a collection of tools called Xcode. (You can copy and install Xcode from the original discs that came with your Macintosh, or you can download and install the latest version of Xcode from Apple’s Web site at http://developer.apple.com/tools/xcode.)

Remember The gcc compiler is free. Xcode also uses the gcc compiler but includes an editor to help you write C++ programs on a Macintosh. The gcc compiler (http://gcc.gnu.org) is open source. If you find the gcc compiler useful, consider donating either your time or money to helping make the gcc compiler even better than before.

A C++ program to display It works! on-screen might look like this:

 


#include <iostream>                               ?1
#include <stdio.h>                                ?2
int main()                                        ?3
{                                                 ?4
 cout << “It works!n”;                           ?5
 cout << “nPress ENTER to continue…” << endl;  ?6
 getchar();                                       ?7
 return 0;                                        ?8
}                                                 ?9
Image from book

Technical StuffThe design of the C++ language

Most programming languages, like BASIC, provide a large collection of keywords, or commands, that you can combine in different ways to tell a computer what to do. The more keywords or commands a language provides, the more powerful the programs you can create.

Unfortunately, when a language offers a huge library of keywords, the language compiler must be able to recognize all these keywords, whether or not programmers ever use the keywords. As a result, creating compilers for such languages can be difficult.

The C++ language takes a different approach. Rather than bog down the compiler with a huge library of keywords, the C++ language contains a much smaller library of keywords. This makes it easy to write C++ compilers for different computers, which in turn means more computers can run a C++ program.

Of course, having a small library of keywords means you have only a handful of keywords to give the computer instructions. So C++ solves this problem by including a library of commands, created from this handful of C++ keywords, and stored in separate files. To use a command stored in a C++ library, you just have to tell the C++ compiler which library file you want to use. Two of the most common library files that C++ programs use are the iostream.h and the stdio.h files, which give you commands for accepting data and printing data to the screen.

Because C++ is designed for efficiency (from the computer’s point of view), it tends to be cryptic (from a human point of view).

Image from book
 

On the CD This C++ program is stored in the C++ Sample Programs folder as Chapter 5 -It Works.cpp.

If this C++ program looks a bit weird, it is. Here’s how each line in the C++ program works:

  • The #include command tells C++ to include the library file called iostream.h.
    Remember Some C++ compilers let you abbreviate the filename by dropping the .h file extension, so you’d use #include <iostream> rather than #include <iostream.h> in this example.

  • The second #include command tells C++ to include the library file called stdio.h.

  • The int main() defines the start of your program. Every C++ program is called a main program. The int keyword identifies your main program to represent an integer value, which in this sample program means absolutely nothing and serves only to confuse beginners. (Now you know why learning C++ is much harder than learning BASIC.) The empty parentheses () tells the computer that the program doesn’t need any additional data to run.

  • The left curly bracket { defines the start of your program.

  • The cout command tells the computer to print the phrase It works! on-screen. The \n symbols tell the computer to move the cursor to a new line after it prints It works! on the screen. The semicolon ; defines the end of the command.

  • The cout command prints Press ENTER to continue on-screen. The \n symbols tell the computer to move the cursor to a new line after it prints It works! on the screen. The semicolon ; defines the end of the command.

  • The getchar() waits for the user to press a key.

  • The return 0 command assigns the number 0 to the main program. Some C++ compilers require this, others don’t.

  • The right curly bracket } defines the end of your program.

C++ programs almost always look more cryptic than similar BASIC programs (especially the simple one-line Liberty BASIC program). As a general rule, C++ makes easy things hard and hard things easy. As you can see from the previous C++ program, you have to write a lot of commands just to print It works! on-screen.

Remember C++ is a powerful but unforgiving language. Make a single mistake, and your C++ program could crash a computer. Despite this problem, C++ still remains the most popular language to use, which explains why programs like Micro-soft Windows, Photoshop, and Quicken are all written in C++ (or a combination of C and C++ code). (This also explains why programs like Microsoft Windows tend to crash a lot and constantly need patching to make sure they work properly.)

AddThis Social Bookmark Button

Learning Revolution

January 30th, 2008 yudi Posted in Learn The Language No Comments »

Revolution is a unique scripting language that uses English-like commands to program a computer. As a result, it’s easy to understand while also demonstrating that not all programming languages need to work like BAISC or look as cryptic as C++.

Perhaps the biggest drawback of traditional programming languages like BASIC and C++ is that you must tell the computer what to do and then how to do it, step by step. Scripting languages like Revolution allow you just to tell the computer what you want it to do without getting bogged down in the details for how to do it. Revolution offers three advantages over traditional programming languages:

  •  Revolution programs are often shorter than equivalent programs written in other languages such as BASIC or C++.

    Image from book

    The design of the Revolution language

    In 1987, Apple developed a unique program called HyperCard, which it gave away for free with every Macintosh computer. HyperCard combined the features of a paint program with a database program and included its own programming language called HyperTalk. Hyper-Talk was designed to be easy to write and understand by using plain English commands. For many people, HyperCard and HyperTalk made programming fun and accessible for the first time.

    Unfortunately, Apple couldn’t make money by giving HyperCard away for free, so it eventually stopped improving the program. Around this time, an independent programmer decided to develop an equivalent HyperCard clone, called MetaCard, which would run on Windows, Macintosh, and UNIX (including Linux) computers.

    Eventually, a company in Scotland bought the rights to MetaCard and made it even easier to use. This new program was dubbed Revolution and its programming language, based on the original HyperTalk language, was dubbed Transcript. (The AppleScript language, used on current Macintosh computers, bears a striking similarity to the original HyperTalk language, so if you understand AppleScript, you already know much of the Revolution language, too.)

    So the basic design philosophy of Revolution is to make programming easy by hiding the details of programming and letting you focus on writing descriptive English-like commands to tell the computer what to do.

    Image from book
     
  •  Revolution uses English-like commands to make programs much easier to understand and modify.

  •  Because you don’t have to worry about how the computer does something, you can concentrate more on the design of your program.

Programmers familiar with traditional languages like BASIC or C++ might find Revolution initially confusing, but the fundamental programming principles apply to Revolution, as well. By browsing through Revolution program examples, you can see that there is more than one way to write a program.

Revolution is a trial version that you can use for a limited time. After this trial period is over, you have to buy a copy of Revolution if you want to continue using it.

To create a program to display It works! on-screen, a Revolution program might look like this:


on mouseUp                                        ?1
    put “It works!” into message                  ?2
end mouseUp                                       ?3

On the CD This Revolution program is stored in the Revolution Sample Programs folder as Chapter 5 - It Works.rev.

Like REALbasic, Revolution programs often rely on a user interface and then provide Revolution commands to make the user interface work. The previous code runs only when the user clicks a button in a Revolution program.

Here’s how each line in the Revolution program works:

  • ?1 The on mouseUp command tells the computer that this is the start of a program that runs whenever the user releases the mouse button in the process of clicking.

  • ?2 The put command tells Revolution to display or “put” the phrase It works! on-screen.

  • ?3 The end mouseUp command defines the end of your program.

AddThis Social Bookmark Button




eXTReMe Tracker