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

 

 

Chapter 5: Getting Your Hands on a Real Language: Liberty BASIC

69

 

 

 

 

 

 

 

 

 

Keystroke Command

What It Does

 

 

 

Backspace

Deletes the character to the left of the cursor

 

 

 

 

 

 

 

 

Ctrl+F

Finds and replaces text in your program

 

 

 

 

 

 

 

 

Ctrl+A

Selects your entire program

 

 

 

 

 

 

 

 

Ctrl+Z

Takes back the effects of the last command you

 

 

 

 

chose (the Undo command)

 

 

 

 

 

 

 

Getting Help Using Liberty BASIC

As do most programs, Liberty BASIC provides an online Help system. To access Help, follow these steps:

1.Choose Help Liberty BASIC Help.

The Help Contents window appears, listing various Help topics, as shown in Figure 5-3.

Figure 5-3:

The Liberty BASIC Help window provides lists various topics that can answer any questions you might have using Liberty BASIC.

70

Part II: Learning Programming with Liberty BASIC

2.Click a Help topic that you want to read.

Depending on the topic you choose, the Help window displays different information about using Liberty BASIC.

3.Click the Close box of the Help window after you finish.

Exiting Liberty BASIC

Eventually, you may need to exit Liberty BASIC so that you can do something else with your computer. To exit Liberty BASIC, follow these steps:

1.Choose File Exit from the menu bar, or click the close box (which appears in the upper right-hand corner) of the Liberty Basic window.

If you haven’t saved the Liberty BASIC program you’re currently displaying, a dialog box asks whether or not you want to save the file.

2.Click Yes to save the current file or click No if you don’t want to save your changes.

Another dialog box appears, asking whether you’re sure you want to exit Liberty BASIC.

3.Click Yes to exit (or No to return to Liberty BASIC).

Liberty BASIC gracefully exits.

Chapter 6

Handling Input and Output

In This Chapter

Getting input and output the primitive way

Getting input and output the modern way

Printing stuff out

Every program takes in data (input), manipulates that data in some way, and spits the data back out in some form (output).

In the old days, programmers gave input to the computer by using a variety of methods, ranging from physically rearranging switches on the computer to using paper tape, punch cards, teletype machines (which resembled typewriters), and finally keyboards and touch-screen monitors. As computers spit back some form of output, it usually appears on paper or on-screen on a monitor.

Despite the fact that today’s computers include pop-up windows, dialog boxes, command buttons, and scroll bars, most of today’s programming languages are still rooted in the past, when programs waited for the user to type a name or a number. Then the program shoved that information up a line and printed the resulting output directly below it.

That’s why most programming languages such as BASIC (and even C/C++) contain built-in commands for reading and displaying data one line at a time on-screen. Naturally, such programs look fairly primitive, especially if you compare them with today’s modern programs, but be patient. As you’re learning programming for the first time, understanding how programs can accept input and spit data back out is much more important (at this time) than worrying about how a program actually looks on-screen.

Inputting and Outputting Data:

The Old-Fashioned Way

Input occurs whenever a program accepts data from an outside source. Some examples of where a program can get input include the following:

72

Part II: Learning Programming with Liberty BASIC

Anything the user types from the keyboard

The movement of the computer mouse

Data that someone previously stores in a file (such as a word processor document or the high score of a video game)

Data that feeds into the computer from an outside source (such as a Web page sent through a modem or an image captured through a scanner)

Output occurs whenever a program displays data back, usually after it manipulates the data in some way. Some common examples of output include the following:

Data that appears on-screen (such as text, pictures, or video images)

Data that prints on paper through a printer

Sound that plays through a computer’s speakers (such as audio files previously downloaded off the Internet)

In BASIC, the simplest way to output data is to use the PRINT command, which simply displays whatever appears inside double quotes. In the following example, the BASIC command does nothing but display the text What are you looking at? on-screen, as shown in Figure 6-1.

PRINT “What are you looking at?”

END

The PRINT command in Liberty BASIC displays text in a window known as the main window. If you don’t want the main window to appear, just use the command NOMAINWIN. Later when you start writing programs that use a graphical user interface (as explained in Chapter 14), you won’t need to display the main window to display data on-screen.

As the PRINT command gives your program the capability to output data, the INPUT command gives your program the capability to accept data from the keyboard. To use the INPUT command, you simply type the word INPUT, following it with a character or phrase that you want to represent whatever the user types at the keyboard, as in the following example:

PRINT “What is the name of your boss?”

INPUT Myboss$

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

END

If you type the preceding example into Liberty BASIC, the program displays the text What is the name of your boss? If you type a name and press Enter, the program responds by displaying the name that you typed, following it with a question mark and the text That sounds like the name of a moron!, as shown in Figure 6-2.

Chapter 6: Handling Input and Output

73

An equivalent C program

Unlike BASIC, other programming languages force you to enclose instructions inside special words or symbols that act as parentheses (so to speak) that wrap around the beginning and ending of your program. In the C/C++ language, for example, the smallest program you can write consists of the word main, which you follow with parentheses and curly brackets, as in the following example:

main ()

{

}

If you write instructions by using C/C++, you must enclose them inside these strange curly brackets. Just to show you how drastically different identical programs can look if you write

them in different programming languages, here’s an equivalent C program that enables the user to type a name (as input) and displays onscreen whatever name the user types, following it with the words, That sounds like the name of a moron!:

main ()

{

char myboss[15];

printf (“What is the name of your boss.\n”);

scanf (“%s”, &myboss); printf (“%s”, myboss); printf (“? That sounds like

the name of a moron!”);

}

Figure 6-1:

The main window is a special window for displaying text from the

PRINT

command.