Hello, World

[ LiB ]

Hello, World!

Okay, before we go any further, let's write our first program. This is a common one for first-time programmers to write in any computer programming language, most likely because it is so simple. This program simply displays the text "Hello, World!" on the screen. That's right, no graphics, no special effects, just pure, hardcore text.

Let's go over how to compile the following code. Type what follows into your Blitz Basic compiler or open demo02-01.bb (see Figure 2.1). Next , select Program->Run Program. (The Blitz Basic Compiler may ask you to save the file first; go ahead and do that.) Then watch the magic.

Figure 2.1. The Hello World program in Blitz Basic.


If you decide to type the code into the compiler, make sure that the workspace into which you are typing is blank first. Only the code should be displayed in the main window of the Blitz Basic compiler.

If you don't want to compile the code at all, you can also run this program from the CD. Figure 2.2 shows the executed Hello World program.

Figure 2.2. The executed Hello World program.


 ;demo02-01.bb - Displays text "Hello World" Print "Hello, World!" WaitKey 

Although this program may seem very simple, it is a big hurdle you have just crossed. You just created a file, typed in code, compiled it, and ran it as a program. Congratulations!

Let's analyze this program a bit (although there isn't much to analyze). First of all, the line

 ;demo02-01.bb - Displays text "Hello World" 

is a comment. A comment is any text that is written after a semicolon (;). The comment ends at the end of the line. It does not have to occupy its own line; it can be written after some actual program code. For example,

 Print "This is code" ;This is a comment. 

consists of two parts : a line of code and a comment. Comments are used to help you understand the code; the compiler does not understand or care about information in comments. All comments are automatically ignored by the compiler. Figure 2.3 demonstrates how comments look inside a compiler.

Figure 2.3. Comments in a compiler.


NOTE

TIP

You might be wondering, "If it is my code, why would I need a comment to understand it? I wrote it, so I understand it!" The problem with this assumption is twofold: one, you may decide to share the code with someone after you write the program, and two, you could forget how your program works and spend a lot of time trying to figure out what some parts do. More than once I have forgotten to comment my code, and the results were not good. I had to spend quite some time trying to understand a little bit of code I had written only a few months ear lier. Anyway, the moral of the story is: ALWAYS COMMENT YOUR CODE.

The next line of code is the meat of the program.

 Print "Hello, World!" 

This line prints the text string "Hello, World!" on the screen (a text string is simply a set of characters ) and begins a new line. To see what I mean by new line, add another Print command to the code. You will see that the new text is written below the old text.

Note the quotes around "Hello, World!" . Quotes are necessary around any part of a string. The quotes identify to the program that what is being typed is a set of letters and numbers , not a variable name . If you leave off the quotes, you will get an error.

NOTE

If you type this program into your compiler, you will notice that after running it, your compiler displays a dialog box that says "Program has ended." Although this occurs in the demo version of Blitz Basic, it does not happen in the full version. If you wish to rid any program of the dia- log box, just type End when you wish the program to end. End quits the program without displaying any dia- log boxes. Try it out on demo02- 01.bb by typing End somewhere in the program.

NOTE

Notice the square brackets ([]) on the left and right of the [string$] variable. These brackets mean that the variable is optional and not required. If the variable is required but omitted, you will receive an error and will not be able to compile your code.

I usually like to provide the function declaration for easy reference when calling functions. A function declaration describes any parameters taken in by the function, as well as the function name. The function declaration for Print is

 Print [string$] 

As you can see, the function's name is Print and the only parameter is [string$] . A string is just a series of characters put together; you can think of a sentence as a string. The string would be the entire sentence lined up together, including the spaces and punctuation.

First of all, Print is a function. A function (which will be described in more detail later) comes in two flavors: user-defined and compiler-defined. User -defined functions are written by the programmer himself ( TestKeyboard() from the Chapter 1 game is an example), and compiler-defined functions are embedded in the compiler and are available for use in a program. Print is an example of a compiler-defined function.

See Table 2.1 for a description of the parameter.

Table 2.1. Parameter for Print

Parameter

Description

string$

A text string followed by a new line that will be displayed onscreen. If string$ is omitted, only a new line will be printed.


The final line calls the function WaitKey .

 WaitKey 

This function simply waits for the user to press any key before moving on. If you remove this line from the program, the program will end before the user can read "Hello, World!"

One question remains: What is that dollar sign doing after the word string ? That brings us to variables .

[ LiB ]


Game Programming for Teens
Game Programming for Teens
ISBN: 1598635182
EAN: 2147483647
Year: 2004
Pages: 94
Authors: Maneesh Sethi

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net