Chapter 1. Introduction to Programming

In addition to teaching you the basics of computer programming using Just BASIC, this book also seeks to provide you with the background information you will need to figure out your place within the grand scope of the programming world. To help get you started, this chapter provides you with a gentle review of the evolution of computer programming. You will learn about a number of different computer languages and their relative strengths and weaknesses, as well as the types of tasks to which various programming languages are applicable. Along the way, you will also pick up some basic terminology. You will learn about the origins of the BASIC programming language and create and execute your first BASIC application, the Knock Knock Joke game. By the time you are done with this chapter, you will possess the background knowledge you need to not only begin BASIC programming but also to start thinking about which programming language or languages you ultimately want to focus on.

Specifically, you will learn:

  • A brief history of the evolution of computer programming

  • Basic programming concepts and terms

  • About the strengths and weaknesses of various programming languages

  • How to create and execute your first BASIC program

Project Preview: The Knock Knock Joke Game

In this chapter and in each chapter that follows, you will learn how to create a new BASIC application. This chapter’s project, the Knock Knock Joke game, is a simple computer game that tells the player a series of knock knock jokes. The game begins by displaying the opening prompt for the first joke, as shown in Figure 1.1.

The player must respond by entering “Who is there?” in response to the opening prompt.

Figure 1.1. The player must respond by entering “Who is there?” in response to the opening prompt.

To minimize the amount of effort required by the player, the game automatically displays the required response in the text field of the popup dialog. Therefore, to answer the prompt and proceed to the next part of the joke, all that the player has to do is click on the OK button.

After responding to the opening prompt for the first joke, the game displays the second part of the joke, as shown in Figure 1.2.

The player must respond by entering “Disease who?” in order to proceed to the end of the joke.

Figure 1.2. The player must respond by entering “Disease who?” in order to proceed to the end of the joke.

Once the game receives the correct response from the player, it displays the first joke’s punch line, as shown in Figure 1.3.

The punch line for the game’s first joke.

Figure 1.3. The punch line for the game’s first joke.

The player then clicks on OK to dismiss the punch line and proceed to the next joke. All that the player has to do to complete the rest of the game and view its jokes is to continue to click on the OK button. However, if the player instead attempts to retype the correct response and makes a typo in doing so, or if the player clicks on the Cancel button, an error message is displayed similar to that shown in Figure 1.4.

Error messages are displayed if the anticipated response is not received.

Figure 1.4. Error messages are displayed if the anticipated response is not received.

The game automatically ends after the last joke has been told. As you can see, although this is not a particularly challenging game from the player’s standpoint, it does possess a number of features upon which you can build and later create more interesting and complex games. For example, the Knock Knock Joke game demonstrates how to interact with the player via dialog windows and to collect and process input. It also demonstrates how to analyze and process that input.

Getting Started

There are any number of reasons that people decide to learn how to program. For starters, it can be very fun and many people simply enjoy spending time working on challenging tasks and expressing their own unique form of computerized creativity. Some people decide to jump into programming in order to create an application for which they see a need that has not yet been met. For example, a comic book author might decide to create his own custom word processor with features that make comic book development easier. Of course, many people make the leap into programming to earn more money by switching to or starting a new career.

Programming is a lucrative field and there is no shortage of jobs for individuals with solid programming skills. Colleges and universities around the world have developed computer sciences degree programs designed to help fill the never-ending need for new programming talent, and bookstores are packed full of books designed to help programmers at all levels learn more about their craft.

For many, taking the first step into programming is the hardest. That is where this book comes in to play. In preparing to write this book, I set as one of my objectives the goal of writing a book that is designed to assist first-time programmers in wading into the sometimes-murky programming waters. This book assumes that, as a beginner programmer, you have not yet made a decision to learn a specific programming language or that you don’t have a great depth of understanding as to what the many different programming languages today are capable of or what makes them different from one another. In addition to teaching you how to program, this book will provide you with the background that you will require so that, when you have finished with this book, you’ll be able to make a well reasoned and intelligent choice as to the next step you will want to take as you continue your programming career.

Programming Overview

All computer programs, regardless of what programming languages they are written in or what operating system they run on, share certain similarities. They all consist of code statements that provide the computer with instructions. These programs typically accept some type of input, process it in some manner, and then produce output.

The input that is processed might be provided by the program itself or it might be provided by interacting with a user. Data might also come from files stored on the computer or, in the case of Windows computers, from the Windows registry. Input might also come from data passed to the program at execution time. What the program does with the data that it is given depends on what the programmer designed the application to do. For example, a computer game might accept input from the computer keyboard, mouse, or a joystick and then use this data to guide the movement of an animated space ship, which would then be displayed as output on the computer screen as part of an arcade-style game.

Deciding What Programming Language to Learn and Use

In most cases, there is any number of programming languages available for you to choose from in order to solve a given problem. For example, you could write a desktop application for Windows using any of a number of programming languages, including Visual Basic, C++, Java, and so on. Although each of these programming languages has its own particular set of strengths and weaknesses, chances are good that you can create just about any desktop application you would want using any of these languages. In fact, the choice of what language is used is often more a matter of personal preference based on the programmer’s background rather than a technical requirement imposed by the operating system or other external factors. The imagination and experience of the programmer is far more important than the choice of programming language. As a result, in most cases the choice of language is almost immaterial.

Creating Your Own Solutions

For most people, using their computer means running and working with their favorite applications, which may include computer games or Microsoft Office applications like Word and Excel and WinZip. These applications are all examples of software programs written by someone else, typically a commercial software development company like Microsoft. Because commercial software developers want to sell as many copies of their applications as possible, they try to make them appeal to as many people as possible. As a result, compromises are sometimes made such that a given application may meet most but not all of the needs of every consumer.

Microsoft Word is a general-purpose word processor that is great for creating letters, reports, and all sorts of documents. It can even be used to create résumés and perform desktop publishing. However, it takes a little work to set up Word to perform these kinds of tasks, and the number of features it provides may not meet the needs of every user. Because résumé writing and desktop publishing are extremely popular activities, many companies have developed custom applications specifically designed to address these tasks. As you would expect, résumé and desktop publishing applications are specifically designed to accomplish their respective tasks and as such are far more feature rich than Microsoft Word when it comes to résumé writing and desktop publishing.

Now suppose that you want an application that you could use to create your own electronic diary. You might first look for a commercial application to address your needs. If you find such an application, it might not have the specific set of features that you are looking for or it might cost more than you think is reasonable to pay. Another option worth pursuing is to visit one of the many shareware websites on the Internet to see if you can find a shareware or freeware application that somebody else has created that may meet your requirements. If this option does not pan out, then you may have to settle for using your word processor as your diary. Unless, of course, you know how to program—in which case, you can create your own diary application that is perfectly suited to meet your needs. Best of all, if once you are done you think the application is something that other people might be able to use, you might consider giving it away or even distributing it as shareware and seeing if you can make a little money from your hard work.

Mastering the Art of Program Logic

Contrary to most people’s perception, the programming logic that makes computer applications work is not fundamentally different from the logic that people apply to different aspects of their lives every day. Take, for example, a parent that has been asked by her child to teach him how to play a game. In this scenario, the parent might provide instruction like this:

  1. Open the game board and have everybody place their pieces on Go.

  2. The first player then rolls the dice and moves his piece the number of spaces indicated by the dice.

  3. If a double is rolled, the player rolls again; otherwise, the next player rolls.

  4. Player may buy any property he lands on as long as it is not already owned by somebody else and if he has the money to do so.

  5. If a player lands on someone else’s property, rent must be paid.

  6. Each time a player successfully makes it around the board, he is rewarded with $200.

  7. Players are eliminated from the game as they run out of money.

  8. The last player remaining in the game wins.

The logic used to develop a computer program is not fundamentally different from the logic outlined above. What makes writing a computer program a little more difficult is that computers do not understand English or any other human language. Instead, to create a new computer program, you must learn how to outline the logic required to perform a given task using a programming language.

Just as there are many different human languages, there are also many different computer languages. These languages typically have unusual names like Visual Basic, Java, C#, C++, and AppleScript. These languages and many other computer languages like them have their own unique set of strengths and weaknesses and are often better suited than other programming languages to perform certain types of tasks. Visual Basic, for example, only runs on Windows computers and is a great language for creating Windows desktop applications. JavaScript, on the other hand, runs inside web browsers and is a great programming language for adding dynamic content and interactivity to websites.

Regardless of which programming language you decide to learn and which environment you develop applications for (desktop, networking, the Internet), all programming languages share a common set of characteristics and features.

Talking in a Language Computers Understand

Computer languages, like human languages, follow certain sets of rules that define the language and use of specific language keywords. Like human languages, there are thousands of programming languages. In addition, new programming languages are created every year. In the sections that follow, you will learn a little about a great many different programming languages and will come away with an understanding of what each language is used for and where it fits into the grand scheme of computer programming.

Machine Language

Different computers have different types of central processing units or CPUs. Each CPU has its own set of instructions that it understands. These sets of instructions are referred to as machine language or machine code. Machine code is the only language that a computer can understand. Other languages like BASIC are only intermediary languages that must ultimately be converted into machine language before they can be executed.

Machine code languages consist of patterns of bits, which can take on either of two values: 0s or 1s, as demonstrated next. Different bit combinations represent different commands that are recognized by the CPU.

0011 1101 0001 1010
0010 1001 1000 0011
1111 0001 0101 1011

This example is intended to demonstrate that machine languages are enormously difficult programming languages to learn. As such, very few programmers know how to write machine code programs. In fact, some of the most talented programmers in the world have never considered learning to work with machine code.

Programming in machine code is extremely difficult for human beings. As a solution to this problem, assembly language was created in the 1950s. Assembly languages use mnemonic codes to represent specific machine code commands. Unlike machine code, which consists of only 0s and 1s, assembly languages are made up of a combination of simple words and numbers.

Assembly language is considered a low-level programming language. They are linked to specific CPUs. Therefore, the assembly language used on one type of computer will be slightly different from the assembly language used on another type of computer. When executed, assembly programs are translated line for line into corresponding machine language statements by a program known as an assembler.

Assembly language was once widely used in application development. It was used in the creation of operating systems such as MS-DOS and applications like the original Lotus 123 spreadsheet application. Assembly programs were once heavily used on mainframe computers. However, languages such as COBOL and Fortran eventually supplanted it on that platform. Starting in the 1970s, with the advent of the C programming languages, the use of assembly language programs started to fade on personal computers as well.

Today, assembly languages are used only for very specific tasks, such as the development of device drivers where direct hardware access is required or on new computer systems for which high-level languages have not been developed. Regardless, an understanding of assembly language programming is still regarded as an essential part of any computer science degree in most colleges and universities. Still, assembly language programming has proven to be quite difficult for the average programmer. This, combined with a lack of portability between different types of computers, has relegated assembly language programming to a very small number of programmers.

Fortran and COBOL

Despite being a major improvement over machine language coding, assembly language left much to be desired as a general-purpose programming language. Instead, new programming languages were needed to facilitate the development of a new generation of business and scientific applications. In the late 1950s, a pair of new programming languages emerged to address these needs.

Fortran appeared first on the scene in 1957. Fortran, which is an acronym for FORmula TRANslator, was designed to support complex mathematic calculations. Early versions of Fortran provided for the development of applications whose performance was roughly equivalent to that of assembly programs but which were considerably easier to develop. By the early 1960s, several dozen Fortran compilers had been created for various computing platforms, making Fortran the first truly portable programming language.

Fortran applications are still in use today in various technical areas, including weather and climate modeling and computational chemistry and physics. Once an arcane and difficult language to master, Fortran has been updated and modified over the years and now incorporates many features found in other modern programming languages, including updated syntax and object-oriented features. Although still in use today by the scientific and engineering communities, Fortran is generally regarded as a specialist language.

COBOL was introduced in the late 1950s. COBOL, which stands for Common Business-Oriented Language, was designed to support the development of business applications. It is still used to support mainframe application development today. In fact, the vast majority of the world’s business applications are still written in COBOL. The current version of COBOL, COBOL 2002, includes many new programming capabilities, including support for object-oriented programming. Like Fortran, COBOL is a portable programming language that can be written once and ported to other platforms and run, usually with minimal modification.

C

Given that assembly language programming was so difficult and thus assembly programs were extremely difficult to port from one computer to another, and that languages like Fortran and COBOL served specialized purposes, it was clear that an easier to learn and far more portable general programming language was needed. This need was answered when C was introduced in the early 1970s.

C was developed at Bell Telephone Laboratories for use on UNIX operating systems. C is far easier to work with than assembly language and provides direct access to computer hardware. It quickly became the most commonly used programming language for writing system and application software.

In order to run a C program, its source code has to be translated into machine code using a special program known as a compiler. A compiler, like an assembler, is a program that translates a computer program into machine code. C is a relatively small programming language. When compiled, it generates a few machine code statements for each line of C code. C compilers have been developed for just about every computing platform; therefore, C is far easier to port from one computer to another than assembly language programs, often requiring only minimal recoding.

One of C’s main features is its ability to directly access computer hardware. This has proved to be a double-edged sword, in that only the most experienced and accomplished programmers have the programming skills required to safely use the language in this manner. In the hands of less-experienced programmers, C can prove to be a time bomb waiting to go off and crash computers.

C proved to be vastly powerful. As a result, many popular applications have been written using it (and spin-off languages such as C++ and Objective-C), including Microsoft Windows, Linux, and Mac OS X.

In order to program with a programming language that was as portable and efficient as C but safer in terms of removing the language’s ability to access hardware, a whole new generation of programming languages was created, including languages such as C++, Objective-C, Visual Basic, and Java.

C++ and Objective-C

C++ is a general-purpose programming language introduced in the mid-1980s. C++ is object-oriented, meaning that it uses an approach where system and key language resources are viewed as objects that come equipped with everything needed to access and manipulate them. C++ was developed by Bell Telephone Laboratories as an enhancement to C.

Modern implementations of C++ are provided by companies like Microsoft and Borland. Both Microsoft C++ and Borland C++ Builder support rapid application development, allowing programmers to create full-featured software applications in less time than can be done using other programming languages. While Microsoft C++ supports application development on Windows computers, Borland C++ Builder supports application development on both Windows and Linux.

Hint

Hint

Rapid application development or RAD is a programming technique in which programmers begin application development using a drag and drop tool that facilitates the creation of graphical user interfaces. Once an application’s interface has been designed, programmers then add the program code required to finish building the application and make it respond when the user interacts with its interface.

C++ incorporates all aspects of C and includes numerous enhancements. As such, it is regarded as a complicated programming language. Another derivative of C is Objective-C, which was introduced in 1986. Like C++, Objective-C is an object-oriented programming language that is built on top of C. However, unlike C++, Objective-C is relatively small, providing C programming with a faster learning curve than C++. Objective-C applications run on UNIX operating systems, including Mac OS X.

BASIC

Despite the vast improvements introduced by languages like C, C++, and Objective-C, many people have found that learning how to program using C, C++, and Objective-C is very challenging, especially first-time programmers. To help reduce the learning curve required to learn how to program, a new language was introduced in the early 1960s called BASIC. BASIC, which stands for Beginner’s All-purpose Symbolic Instruction Code, was created as a programming language that was designed to teach people how to program. However, it soon became obvious that the language was well suited to more than just teaching basic programming concepts.

One of the fundamental design goals of BASIC was to keep things simple. As a result, BASIC soon became the most popular programming language in the world and was ported over to just about every operating system. BASIC programs are much easier to write and typically take less time to create, test, and debug. BASIC programs are also highly portable.

Throughout the 1970s, numerous versions of BASIC were created and made available on various home and desktop computers, including Atari, Apple, and the original IBM PC. As such, BASIC programming was introduced to countless first-time programmers who used it to create small programs. However, in the 1980s third-party application development companies began to spring up, delivering a host of ready-made applications that soon filled most of the needs of desktop computer owners. As such, individual interest in BASIC programming declined.

BASIC’s fortunes began to change in 1991 when Microsoft released Visual Basic 1.0. Visual Basic was a general-purpose RAD programming language. Microsoft has continued to work on and enhance Visual Basic over the years. The current version, Visual Basic .NET, integrated Visual Basic with Microsoft’s .NET Framework. One particular version of Visual Basic .NET of interest to new programmers is Microsoft Visual Basic 2005 Express Edition, shown in Figure 1.5. Microsoft makes this version of Visual Basic .NET available as a free download for the purpose of attracting new Visual Basic .NET programmers.

Microsoft Visual Basic 2005 Express Edition is specifically tailored to help first-time programmers learn how to develop Windows desktop applications.

Figure 1.5. Microsoft Visual Basic 2005 Express Edition is specifically tailored to help first-time programmers learn how to develop Windows desktop applications.

Hint

Hint

The Microsoft .NET Framework is a collection of resources designed to support the development and execution of Windows applications that run on desktop computers, local area networks, and the Internet.

Over the years, many other companies have jumped into the fray, offering their own unique versions of BASIC. Another major BASIC language developer is REAL Software, which produces the REALbasic programming language, shown in Figure 1.6.

REALbasic supports RAD application development on Windows, Linux, and Mac OS X.

Figure 1.6. REALbasic supports RAD application development on Windows, Linux, and Mac OS X.

Like Visual Basic .NET, REALbasic is an object-oriented programming language that supports RAD development. Where REALbasic differentiates itself is in its support for cross-platform application development, supporting Windows, Linux, and Mac OS X application development. Using REALbasic, programmers can compile the same application source code and generate different copies of the application for each operating system, often with very little, if any, code rewrites.

Hint

Hint

Traditionally, most programming languages have been designed to work with a single operating system. For example, while BASIC is almost universally supported, specific BASIC dialects like Microsoft Visual Basic .NET are restricted to execution on Windows operating systems. However, languages like REALbasic provide cross-platform support, meaning that they are capable for development applications that can be executed on disparate operating systems.

Both Visual Basic .NET and REALbasic support desktop, network, and Internet application development and both support the development of database applications. Visual Basic .NET also supports the development of software applications than can run on small applications, such as Windows CE-enabled handheld computers and cell phones. Both Visual Basic and REALbasic are used to develop business and commercial software applications.

Hint

Hint

To learn more about Visual Basic .NET and REALbasic, as well as numerous other BASIC programming languages, check out Appendix B, “What Next?”

Another major programming language that has garnered a lot of attention and use in recent years is Java, which was developed by Sun Microsystems in the early 1990s and was first released in 1995. Syntactically, Java is based heavily on C and C++ but it lacks these language’s low-level capabilities to access and manipulate system resources. Java programs generally run in one of two forms, either as standalone applications or as applets that can be executed inside web browsers. Standalone applications run like any other software application. Java applets run as programs embedded within web pages that are loaded and executed by a host web browser.

Java is a platform-independent programming language designed to support the development and execution of computer programs that can be run on different operating systems without any code modification. In order to accomplish this goal, Sun designed Java so that the program code developed by the program was compiled into an intermediary format knows as byte-code (also known as p-code). This partially compiled code is then loaded into a virtual machine running on the target operating system. It was the job of the virtual machine to finish compiling the Java program into machine language at execution time. In order for this development model to work, virtual machine programs had to be developed for different operating systems, which Sun developed and made available for free.

Because it runs within a virtual machine, Java programs do not, by default, have a look and feel that mirrors the default appearance of applications native to a particular operating system. While many programmers were critical of the performance of early versions of Java, improvements have since reduced the amount of memory required while simultaneously increasing processing speed, negating such concerns. Another issue with Java is that Microsoft does not ship it as part of the Windows operating system, forcing individuals to take extra steps to download and install it. However, in the age of the Internet where people are used to constantly downloading and installing browser add-ons and system updates, this hurdle has proved easy to overcome.

Hint

Hint

To learn more about Java and to download and install it on your computer, visit www.java.com. You will also find plenty of sample software applications for download as well.

Other Types of Programming Languages

In addition to the general-purpose, business, and scientific-specific languages already discussed, there are many specialized programming languages in use today that are important to know about. These include

  • Scripting Languages

  • Embedded Application Languages

  • Web Development Languages

  • Database Languages

A scripting language is a computer language that is interpreted into machine code at execution time as opposed to being compiled at development time into machine code. Therefore, while an application written in C++ is compiled and converted into machine code just one time at the end of the program processes and can then be executed over and over again, a script is a program that must be re-interpreted and converted to machine code each time it is executed. As a result, scripts take longer to start. In addition, the computers upon which they are executed must have an interpreter installed that is capable of executing them.

Hint

Hint

An interpreter is a program that converts script statements into machine code at runtime.

There are many scripting languages to choose from, including languages like Perl, Python, JavaScript, VBScript, Ruby, Rexx, RBScript, and AppleScript, just to name a few. Scripts are saved as plain text files. As a result, when distributed, the source code for the script is easily accessible by anyone who wants to view and copy or modify it, thus raising concerns regarding a programmer’s ability to control the intellectual property rights of his program. However, the trade off for the lack of speed and visible source code is the speed with which scripts can be developed. To write a script, a programmer typically opens a blank text file, types in its code statements, then saves and executes the script without ever having to compile anything. As a result, a programmer who runs into an error can quickly open the script’s file, fix the error, and run the script again to instantly see if the problem has been fixed.

Hint

Hint

VBScript and RBScript are scripting languages that are based on BASIC. VBScript represents a subset of Visual Basic and RBScript is a derivative of REALbasic. Both of these scripting languages are relatively easy for anyone with a BASIC programming background to learn.

Scripting is prevalent in most programming environments. For example, Windows computer and network administrators develop scripts using languages like VBScript, Python, Ruby, and Perl to automate a host of time-consuming activities, thus freeing up time to work on other tasks. Web developers also embed scripts within web pages using languages like JavaScript to add dynamic content to websites.

Embedded Application Languages

Many applications provide their own embedded programming language in the form of a scripting language. Once such example is Microsoft VBA or Visual Basic for Applications. VBA is shipped as a standard feature in Microsoft Office. It works with Microsoft Word, Excel, PowerPoint, and Access. Microsoft has also added VBA support to other Windows applications such as Microsoft Visio. In addition, third-party Windows application developers have also integrated support for VBA in their applications. For example, Corel has integrated VBA into WordPerfect Office X3 and Corel Designer Technical Suite 12.

A programmer who wants to create a new application that includes features found in Microsoft Excel has a couple of options. One option is to create a new application from scratch. Another option is to create a new application that works in conjunction with Microsoft Excel using VBA. VBA provides programs with the ability to create custom graphical user interfaces and to automate the execution of its host application. In the case of Microsoft Excel, this means that VBA can create a new spreadsheet, populate it with data, perform calculations on that data, and present this data to the user using a customized user interface that handles all the required interaction with Excel, providing the user with a much simpler and more streamlined experience.

Hint

Hint

Microsoft will eventually replace VBA with Visual Studio Tools for Applications. Visual Studio Tools for Applications is a toolkit that provides scripts with access to the .NET Framework. However, this switch appears to be years away and Microsoft includes VBA as part of Microsoft Office 2007.

Web Development Languages

Back in its beginning, information on the Internet was delivered as plain text. While the Internet certainly represented a great leap in communication, it was not until the introduction of HTML, allowing for the development of the world wide web, that things truly became interesting. HTML stands for HyperText Markup Language. HTML provides web developers with the ability to specify how data is displayed. Modern web browsers like Internet Explorer, Opera, and Mozilla Firefox render web pages based on HTML code.

HTML provides the ability to present static information that does not change. As more and more people around the world started surfing the Internet and companies began to look at the Internet as a means of generating revenue, the need for more powerful and robust web page programming languages quickly became evident. In response, a host of new programming languages was created. One of the first was JavaScript. JavaScript is a scripting language that lets you embed small scripts inside HTML pages in order to provide interactive content on your web pages. Another similarly named but completely different programming language, Java, came along allowing small programs, referred to as Java applets, to be run within web browsers. Web developers used these advanced programming languages to build web-based applications that enabled web commerce.

Web-based programming languages provide programmers with access to hundreds of millions of Internet users. Unfortunately, while today HTML is supported by all major web browsers, there are incompatibilities between web browsers and programming languages that create problems for web developers. As a result, there is no guarantee that Internet users will have the proper combination of software installed to allow them to visit and interact with every website, providing web developers with a unique set of challenges.

Database Languages

Modern database programs, such as Microsoft Access and Oracle, are designed to work with a specialized programming language known as SQL or Structured Query Language. SQL is specifically designed to support the creation, modification, and retrieval of data stored in a database.

Hint

Hint

A database is an application that is designed to facilitate the storage and retrieval of large amounts of data.

The advantage of working with specialized database programming languages is that they are optimized for interacting with databases and provide extremely efficient code. Typically, database-specific programming languages allow database programmers to create database applications in less time than general-purpose programming languages. A disadvantage of database programming languages is that their use is tied to their respective database and as such, they do not lend themselves to the development of other types of applications like general programming languages.

Most general programming languages, though not specifically optimized for database application development, do support database access, and as such are sometimes used in place of database-specific languages. Databases are generally associated with specific operating systems. For example, Microsoft Access only runs on Windows. As such, any applications developed in conjunction with Microsoft Access are restricted to Windows. This limits the portability of database programs.

Finding the Right Programming Language

There are many programming languages from which to choose. Each programming language has its own particular set of strengths and weaknesses. Each of these languages is applicable to particular programming environments. There is no one programming language that can be used to develop applications that can run on all platforms and operating systems. As such, there is no one best programming language to learn.

Despite this obvious fact, you will find no shortage of individuals ready to argue that their favorite programming language is the best. The truth is that for just about any programming challenge, there are plenty of programming languages from which to choose. Ultimately, the choice of language is less important than the creativity and talent of the programmer who wields it. As a result, a good BASIC programmer can, for example, develop desktop software every bit as good and useful as a C, C++, or Objective-C programmer.

Getting Started with Just BASIC

Just BASIC is a free BASIC programming language. Compared to other BASIC programming languages like Visual Basic .NET and REALbasic, Just BASIC is relatively simple. It does not require that you first learn how to work with a complicated integrated development environment or Microsoft’s .NET Framework in order to be able to use it to create standalone Windows applications. Instead, Just BASIC provides you with a simple editor and a simple BASIC dialect, making it much easier for the first-time programmer to focus on learning the fundamental elements of programming without getting lost in the complexities of other high-end BASIC programming languages.

I selected Just BASIC as this book’s programming language because of its relative simplicity compared to most other programming languages. Still, Just BASIC packs plenty of punch. It’s a 32-bit Windows programming language that can be used to create desktop applications. In addition to being free, it is backed by a supportive online community of programmers that can help you learn more through online forums.

Hint

Hint

Check out Appendix A, “What’s on the Companion Website?” for a list of online resources where you can go to learn more about Just BASIC, including online forums.

Once you have become comfortable with Just BASIC and are ready, you can make the transition to one of the more advanced BASIC programming languages or perhaps to an entirely different programming language.

Installing Just BASIC

Before you can begin creating new applications using Just BASIC, you must download and install it. Just BASIC is distributed on the Internet for free. To get a copy, go to www.justbasic.com, as shown in Figure 1.7, and click on the Download tab at the top of the screen. This opens the download page where you can click on the Download link to download your copy of Just BASIC. At just 2.4 MB in size, the download does not take long to complete. When prompted, click on Save to download a copy of Just BASIC’s install program to your computer.

Downloading a free copy of Just BASIC from www.justbasic.com.

Figure 1.7. Downloading a free copy of Just BASIC from www.justbasic.com.

To begin the installation process, double-click on the installation program. A dialog appears showing the default installation folder for Just BASIC. You may modify this location or simply click on Start to keep its default location. During the installation process, a number of files are copied to the Just BASIC folder. Once the installation process is complete, you will be prompted to click on OK to close the installation program. Just BASIC will then start, as shown in Figure 1.8.

Just BASIC automatically starts at the end of its installation process.

Figure 1.8. Just BASIC automatically starts at the end of its installation process.

As you can see, two windows appear. The smaller window presents a series of links to online resources. For now, just close this window. The second, larger window is Just BASIC’s program editor and is the tool that you will use to create and test your Just BASIC applications.

Creating and Executing Your First BASIC Program

Now that you have Just BASIC installed on your computer, let’s use it to create a simple computer program. This program will consist of a single program statement that displays a text string message. By developing and executing this program, you will learn the basic mechanics involved in creating any Just BASIC computer program. The steps required to build your first Just BASIC program are outlined here:

  1. If Just BASIC is not already running, start it by clicking on Start, All Programs, Just BASIC v1.01. The Just BASIC Editor appears.

  2. To begin creating a new Just BASIC program, click on the File menu and select the New BASIC Source File option. In response, Just BASIC clears out the code editor pane, as shown in Figure 1.9.

    Just BASIC applications are created by entering BASIC code statements in the code editor pane.

    Figure 1.9. Just BASIC applications are created by entering BASIC code statements in the code editor pane.

  3. Type print “Hello World!” in the code editor pane, as demonstrated in Figure 1.10.

    Code statements are automatically color coded by the Just BASIC code editor.

    Figure 1.10. Code statements are automatically color coded by the Just BASIC code editor.

  4. Run your new Just BASIC program by clicking on the Run menu and selecting the Run option. In response, Just BASIC will compile your new application and run it, as shown in Figure 1.11.

    By default, every Just BASIC application consists of a default window in which text can be displayed.

    Figure 1.11. By default, every Just BASIC application consists of a default window in which text can be displayed.

That is it. You have just created and executed your first Just BASIC application. In doing so, you keyed in one code statement, which used the print command to display a text string in the application’s window. By default, every Just BASIC application automatically displays a default window. In the case of this program, text was displayed on this window using the print command.

Hint

Hint

The instructions that you write when developing a computer program are referred to as statements. A programis a file containing code statements that when executed tell the computer to do something. Within this book, the terms program and application are used interchangeably. When saved to a file, the statements that make up a program are collectively referred to as the program’s source code.

If you want, you can save your work so that you can load and re-execute it again later by clicking on the code editor’s File menu and selecting the Save As option. This displays the Save As dialog, allowing you to specify an application name as well as the location where you want to store your BASIC application.

I suggest that you create a new folder named something like MyBasicApps somewhere on your hard drive and that you use it as a repository for all your Just BASIC applications. This will help you to keep all your application files organized in one easy to find location.

Trick

Trick

The Hello World program is admittedly simple but it does serve as an excellent example of the steps involved in creating and executing a BASIC program. To see how this BASIC program stacks up against the same type of program in other programming languages, visit www.wikibooks.org/wiki/Transwiki:List_of_hello_world_programs. Here you will find code examples of similar Hello World programs written in other programming languages, including Assembly, COBOL, Fortran, Visual Basic .NET, REALbasic, C, C++, Java, and JavaScript, just to name a few.

Back to the Knock Knock Joke Game

Now it is time to turn your attention back to the development of this chapter’s game project, the Knock Knock Joke game. The creation of this script will reinforce your understanding of the mechanics involved in creating and executing Just BASIC programs. Later, down the road, you will be able to apply this experience when developing and executing programs in other programming languages.

As you follow along with the instructions that outline the steps involved in creating this program, don’t worry if you do not understand everything that is going on. It is too early in the book to expect to grasp the meaning and function of the BASIC language statements that make up this game. There will be plenty of time, as you make your way through this book, to learn BASIC.

Designing the Game

Before beginning the development of any new computer program, it is a good idea to spend some time planning the overall design of the application. Taking this approach will not only help to ensure that the end product is a program that looks and works the way you want it to but will also help reduce the possibility of running into problems and errors along the way.

As you saw at the beginning of this chapter, the Knock Knock Joke game executes by displaying prompts in the form of popup dialog windows. The default response expected by the user should be provided for each part of the jokes to minimize the effort required to complete the game. Each of the game’s three jokes should be presented in succession and the game should end once the three jokes have been told. In the event that the user fails to provide the expected responses, the game should display an error message explaining how to correctly formulate the expected response.

As you can see, the Knock Knock Joke game is straightforward. You will create it in six steps, as outlined here:

  1. Create a new BASIC file and disable the display of its default window.

  2. Display the opening line for the first joke and collect the player’s response.

  3. Analyze the player’s response and respond accordingly.

  4. Tell the game’s second joke.

  5. Tell the game’s third joke.

  6. Terminate the program’s execution.

Creating a Just BASIC File Script

The first step in the development of the Knock Knock Joke game is to start Just BASIC and to create a new BASIC file. Begin by starting Just BASIC by clicking on Start > All Programs > Just BASIC v1.01 and then select Just BASIC v1.01. Just BASIC starts by displaying two default windows. Dismiss the World of Just BASIC window by clicking on the close button located in the upper-right corner of the window. Next, create a new BASIC file by clicking on the File menu and selecting New BASIC Source file. In response, Just BASIC clears out the code editor pane, making it ready to accept the code statements that will make the Knock Knock Joke game.

Even though you have not yet added the code required to create the game, save your BASIC file by clicking on the File menu and then selecting Save As. In response, the Save As dialog appears. Enter KnockKnock.bas as the application’s filename, specify the location where you would like to save your new game, and click Save.

At this point, you have created a new empty BASIC file. Let’s document the purpose of the Knock Knock game by entering the following code statements into the BASIC file.

' *************************************************************************
'
' Script Name: KnockKnock.bas (The Knock Knock Joke Game)
' Version:     1.0
' Author:      Jerry Lee Ford, Jr.
' Date:        March 1, 2007
'
' Description: This Just BASIC game displays a series of interactive
'              Knock Knock jokes.
'
'
*************************************************************************

Each of these statements is actually a comment. In most BASIC programming languages, including Just BASIC, comments begin with the ' character or the REM keyword.

Hint

Hint

As the preceding statements demonstrate, comments can be placed on their own line. Comments can also be added to the end of code statements, as demonstrated here:

notice "Click on OK to continue." : rem Display a message in a popup
window
notice "Click on OK to continue." 'Display a message in a popup window

Note that to use rem to add a comment to the end of a code statement you must precede the comment with a colon. The colon character is not required when using the apostrophe character to add a comment to the end of a statement.

You should make liberal use of comments in your program code to provide information about your applications as well as to document the logic implemented by specific code statements. Different programming languages use different characters to represent comments. For example, in C++ the // character is used as the comment character.

Now that you have provided a little documentation about your new application, add the following statement to the bottom of your BASIC file.

nomainwin

This statement executes Just BASIC’s nomainwin command. This command is unique to Just BASIC. Its purpose is to suppress the display of Just BASIC’s default window when a Just BASIC application starts. This statement has been added because the Knock Knock Joke game is designed to display text in popup dialogs instead of Just BASIC’s default window.

Displaying the Opening Prompt

Now it is time to add the code statements that will display the opening prompt for the first knock knock joke, as shown here:

response$ = "Who is there?"

prompt "Knock Knock Joke Game" + chr$(13) + "Knock Knock!"; response$

The first statement assigns a text statement to a variable named response$. The contents of this variable will be used to provide the player with a default response in the popup dialog window generated by the second statement. In BASIC, a string is a series of zero or more characters surrounded by double quotation marks and a variable is a pointer to a location in memory where a value is stored. The second statement uses the prompt command to display the first joke’s opening prompt in a popup dialog window. The player will interact with the game by clicking on the popup dialog window’s OK button in order to submit the required response.

Hint

Hint

You will learn more about strings and variables in Chapter 4, “Working with Variable and Arrays.”

Analyzing Player Input

The next set of statements that you will need to add to your BASIC program are shown below. These statements are responsible for analyzing the response provided by the player and then taking the appropriate actions (e.g., either proceeding to tell the rest of the joke or displaying an error message). Because we have not covered any of these statements yet, I am not going to attempt to explain them in any further detail. For now, just key them in exactly as shown.

if response$ = "" then

  notice "Knock Knock Joke Game" + chr$(13) + _
    "Error: You must respond by entering 'Who is there?'"

else

  response$ = "Disease who?"

  prompt "Knock Knock Joke Game" + chr$(13) + "Disease!"; response$

  if response$ = "" then

    notice "Knock Knock Joke Game" + chr$(13) + _
      "Error: You must respond by entering 'Disease who?'"

  else

    notice "Knock Knock Joke Game" + chr$(13) + _
      "Disease this joke seem funny to you?"

  end if

end if

At this point, the programming logic required to tell the first joke has been defined.

Telling the Second Joke

The code statements required to tell the game’s second joke are listed next. As you can see, except for the text that makes up the second joke, these code statements are identical to the statements that made up the first joke.

response$ = "Who is there?"

prompt "Knock Knock Joke Game" + chr$(13) + "Knock Knock!"; response$

if response$ = "" then

  Notice "Knock Knock Joke Game" + chr$(13) + _
    "Error: You must respond by entering 'Who is there?'"

else

  response$ = "Butter who?"

  prompt "Knock Knock Joke Game" + chr$(13) + "Butter!"; response$

  if response$ = "" then

    notice "Knock Knock Joke Game" + chr$(13) + _
      "Error: You must respond by entering 'Butter who?'"
  else

    notice "Knock Knock Joke Game" + chr$(13) + _
      "You butter move on to the next joke."

  end if

end if

Telling the Last Joke

The code statements that tell the third joke are provided next. Again, except for the text that makes up the actual joke, these statements are identical to the statements that made up the first and second jokes.

response$ = "Who is there?"

prompt "Knock Knock Joke Game" + chr$(13) + "Knock Knock!"; response$

if response$ = "" then

  notice "Knock Knock Joke Game" + chr$(13) + _
    "Error: You must respond by entering 'Who is there?'"

else

  response$ = "Max who?"

  prompt "Knock Knock Joke Game" + chr$(13) + "Max!"; response$

  if response$ = "" then

    notice "Knock Knock Joke Game" + chr$(13) + _
      "Error: You must respond by entering 'Max who?'"

  else

    notice "Knock Knock Joke Game" + chr$(13) + _
      "BASIC, JAVA, C++, it max no difference to me!"

  end if

end if

Terminating the Program

You need to add one last statement to the end of your program before calling it a day. This statement consists of a single word.

end

end is a Just BASIC keyword that identifies the end of your application. Its purpose is to ensure that Just BASIC properly terminates the application before closing any open windows or dialogs. By failing to include this statement in your Just BASIC applications, you run the risk that your applications might keep running even after the application windows are closed, leaving you without an easy way to terminate them. Including end at the appropriate location in Just BASIC applications is considered a good programming practice.

Trick

Trick

If you forget to add the end keyword to one of your Just BASIC applications and find that your application is stuck in a state of limbo with all its application windows closed, you can still terminate it. This is accomplished by right-clicking on the Windows Taskbar and selecting Task Manager, which opens the Windows Task Manager window. You can then select the name of your Just BASIC application from the list of open applications in the Applications property sheet and click on the End Task button.

The Final Result

Okay, that’s it. Assuming you followed along carefully and did not make any typing mistakes, your new game should be ready to run. Given the step by step approach used to show you how to develop the Knock Knock Joke game, I have gone ahead and provided a complete copy of the application’s source code so that you can make sure you keyed in everything correctly and did so in the required order. Also, I have added comments explaining what is going on throughout the source code.

' *************************************************************************
'
' Script Name: KnockKnock.bas (The Knock Knock Joke Game)
' Version:     1.0
' Author:      Jerry Lee Ford, Jr.
' Date:        March 1, 2007
'
' Description: This Just BASIC game displays a series of interactive
'              Knock Knock jokes.
'
' *************************************************************************

'Disable the default display of the main window
nomainwin


' *************************************************************************
' Begin telling the first knock knock joke
' *************************************************************************

'Define the default response for the opening prompt
response$ = "Who is there?"

'Display the opening prompt for the first joke
prompt "Knock Knock Joke Game" + chr$(13) + "Knock Knock!"; response$

'Evaluate the player's response
if response$ = "" then 'No data was entered or the player clicked on Cancel

  'Display an error message
  notice "Knock Knock Joke Game" + chr$(13) + _
    "Error: You must respond by entering 'Who is there?'"

else 'Continue telling the joke

  'Define the default response for the second prompt
  response$ = "Disease who?"

  'Display the follow-up prompt for the first joke
  prompt "Knock Knock Joke Game" + chr$(13) + "Disease!"; response$

  'Evaluate the player's response
  if response$ = "" then 'No data was entered or cancel was clicked

    'Display an error message
    notice "Knock Knock Joke Game" + chr$(13) + _
      "Error: You must respond by entering 'Disease who?'"

  else 'Finish telling the joke

  'Display the joke's punch line
    notice "Knock Knock Joke Game" + chr$(13) + _
      "Disease this joke seem funny to you?"

  end if

end if


' *************************************************************************
' Begin telling the second knock knock joke
' *************************************************************************

'Define the default response for the opening prompt
response$ = "Who is there?"

'Display the opening prompt for the second joke
prompt "Knock Knock Joke Game" + chr$(13) + "Knock Knock!"; response$

'Evaluate the player's response
if response$ = "" then 'No data was entered or the player clicked on Cancel

  'Display an error message
  Notice "Knock Knock Joke Game" + chr$(13) + _
    "Error: You must respond by entering 'Who is there?'"

else 'Continue telling the joke

  'Define the default response for the second prompt
  response$ = "Butter who?"

  'Display the follow-up prompt for the second joke
  prompt "Knock Knock Joke Game" + chr$(13) + "Butter!"; response$

  'Evaluate the player's response
  if response$ = "" then 'No data was entered or cancel was clicked

    'Display an error message
    notice "Knock Knock Joke Game" + chr$(13) + _
      "Error: You must respond by entering 'Butter who?'"

  else 'Finish telling the joke

    'Display the joke's punch line

    notice "Knock Knock Joke Game" + chr$(13) + _
      "You butter move on to the next joke."


  end if

end if


' *************************************************************************
' Begin telling the third knock knock joke
' *************************************************************************

'Define the default response for the opening prompt
response$ = "Who is there?"

'Display the opening prompt for the third joke
prompt "Knock Knock Joke Game" + chr$(13) + "Knock Knock!"; response$

'Evaluate the player's response
if response$ = "" then 'No data was entered or the player clicked on Cancel

  'Display an error message
  notice "Knock Knock Joke Game" + chr$(13) + _
    "Error: You must respond by entering 'Who is there?'"

else 'Continue telling the joke

  'Define the default response for the second prompt
  response$ = "Max who?"

  'Display the follow-up prompt for the second joke
  prompt "Knock Knock Joke Game" + chr$(13) + "Max!"; response$

  'Evaluate the player's response
  if response$ = "" then 'No data was entered or cancel was clicked

    'Display an error message

      notice "Knock Knock Joke Game" + chr$(13) + _
        "Error: You must respond by entering 'Max who?'"

    else 'Finish telling the joke
      'Display the joke's punch line
    notice "Knock Knock Joke Game" + chr$(13) + _
      "BASIC, JAVA, C++, it max no difference to me!"

  end if

end if

end

Go ahead, execute your copy of the Knock Knock Joke game, and put it through its paces. If instead of executing you get an error, then you have made at least one typo somewhere. Hopefully the error message you see will provide you with a clue as to where the error lies. Otherwise, you will need to go back and double-check each statement in the source code in order to find and eliminate all your typing errors.

Summary

In this chapter, you learned background information that will help you as you work your way through this book. This information includes the introduction of a number of key programming terms. In addition to increasing your programming vocabulary, you also learned about different programming languages and how they compare against one another. You learned about popular BASIC programming languages, including Just BASIC. You also learned how to create and execute your first BASIC program.

Now, before you move on to Chapter 2, “Creating Programs with Just BASIC,” I suggest you set aside a few extra minutes to improve the Knock Knock Joke game by addressing the following list of challenges.

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

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