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

76

Part II: Learning Programming with Liberty BASIC

NOMAINWIN

PROMPT “What is the name of your boss?”; name$

NOTICE name$ + “? That sounds like the name of a moron!”

END

Sending Data to the Printer

One of the most popular ways to output data is to print it out on paper, also known as a hard copy. In Liberty BASIC, the commands for sending data to a printer are LPRINT and DUMP, as shown in the following example:

LPRINT “Save the dolphins! Kill the tuna!”

DUMP

END

The LPRINT command sends data to your default printer. The DUMP command simply tells your printer to start printing right away.

You can skip the DUMP command if you want, but then the LPRINT command may not start printing immediately.

The LPRINT command starts printing in the upper-left corner of the page. If you want to start printing your data in a different location on a page, you may need to add extra lines or spaces to change the vertical and horizontal position where your text starts printing.

To change the vertical position, just use the LPRINT command by itself, as follows:

LPRINT

LPRINT “Save the dolphins! Kill the tuna!”

DUMP

END

In the preceding example, the first LPRINT command prints a blank line, and the second LPRINT command prints the text Save the dolphins! Kill the tuna!

To change the horizontal position, use the SPACE$(x) command where the letter x represents how many spaces you want to insert. If, for example, you want to insert five spaces before printing any text, you use the SPACE$(5) command, as in the following example:

Chapter 6: Handling Input and Output

77

LPRINT “Save the dolphins! Kill the tuna!”

LPRINT SPACE$(5); “Save the dolphins! Kill the tuna!”

DUMP

END

The preceding program would make your printer print the following message:

Save the dolphins! Kill the tuna!

Save the dolphins! Kill the tuna!

When using the SPACE$(x) command, you need to use the semicolon (;), following it with the data that you want to print, which you surround with double quotation marks.

This chapter provides a brief explanation for getting input and displaying output, just so you can understand the way a computer program gets data and spits it back out again for the user to see. For more details about creating a more modern user interface that offers windows, dialog boxes, and menus, skip to Chapter 14.

78

Part II: Learning Programming with Liberty BASIC

Chapter 7

Variables, Constants,

and Comments

In This Chapter

Using variables

Creating and using constants

Adding comments to your code

When a program accepts input, the first thing the computer needs to do is find a place to store any data that it gets from the user. Because

computers are like one giant brain, they simply store data in memory.

Of course, if you stuff enough data into a computer, it’s likely to lose track of all the data in its memory (much like a person might do). So to help the computer find data that it already stored, computer programs use something called variables.

A variable simply acts like a storage bin. You can stuff any type of data into a variable, such as numbers or words, and then retrieve them back out again so you can stuff different data into that variable all over again. Although variables can only store one chunk of data at a time, they can be reused over and over again to store different data. The contents of a variable may vary at any given time, hence the name variable.

Besides storing data in variables, programs also use things known as constants and comments. Constants represent a fixed value that a program may need, and comments are explanations that programmers use to explain how a program works.

Although the idea of using variables, constants, and comments may seem mysterious to you, relax. You’ll see their purpose as soon as you start writing your own programs.