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

144 Part III: Advanced Programming with Liberty BASIC

Introducing Structured Programming

You never find one “right” way to write a program, but programmers have created different ways to write programs that are at least well organized. One popular way to write a program is known as structured programming, and its main idea is to organize your program by using only three types of instructions (none of which resembles the GOTO command). If you use only the following three instructions, you ensure that you and other people can easily read and understand your program:

Sequential instructions

Branching instructions

Looping instructions

The following sections describe each of these types of instructions.

Sequential instructions

The simplest way to organize instructions in a program is to place them sequentially, or one after another, as in the following example:

PROMPT “How much stuff did you steal last year”; Amount

TaxesOwed = Amount * .95

PRINT “This is how much tax you owe =”; TaxesOwed

END

Unfortunately, you can’t write every program as one big list of instructions. If the computer needs to make a decision, your program may need to choose between two or more different sets of instructions. Programs that must make a choice are said to branch.

Branching instructions

Branching instructions (such as the IF THEN statement) provide two or more different instructions for the computer to follow, based on a certain condition. (For more information about IF THEN statements and other types of branching statements, see Chapter 9.) The following program, for example, calculates two different taxes owed, depending on whether you’re a politician:

Chapter 11: Writing Large Programs by Using Subprograms 145

PROMPT “How much stuff did you steal last year”; Amount TaxesOwed = Amount * .95

PROMPT “Are you a professional criminal (Y or N)”; Answer$ IF (Answer$ = “N”) THEN

PRINT “This is how much tax you owe =”; TaxesOwed ELSE

PRINT “Lawyers and politicians don’t need to pay taxes.” END

Branching instructions offer two or more alternative sets of instructions for the computer to follow. As a result, branching instructions are harder to read than instructions that you organize sequentially because you must determine which set of instructions the computer may follow at any given time.

Looping instructions

Sometimes the computer may need to repeat certain instructions. Rather than type the same instructions over and over, you can use a loop, such as a FOR-NEXT or a WHILE-WEND loop.

A FOR-NEXT loop repeats a fixed number of times. A WHILE-WEND loop repeats itself while a certain condition remains true. Thus the number of times that a WHILE-WEND loop repeats itself can range from zero to infinity. (See Chapter 10 for more information about looping.)

The following program, for example, asks for a password, checks to see whether the user types the correct password (which is the string “open”), and repeats these instructions until the user types the correct password:

PROMPT “What is the password”; Password$

WHILE Password$ <> “open”

PRINT “Wrong password, moron. Try again.”

PROMPT “What is the password”; Password$

WEND

PRINT “You typed the correct password!”

END

Loops can prove harder to read than sequential instructions and branching instructions because you can’t always tell how many times a loop repeats itself. Essentially, a loop is a shortcut so that you don’t need to type a long series of sequential instructions in your program. (For more information about loops, see Chapter 10.)

146 Part III: Advanced Programming with Liberty BASIC

Putting structured programming into practice

The reason for organizing your program in chunks of sequential, branching, and looping instructions is to make how your program works easier for others to understand. If they can understand how your program works, they can modify and improve on it later.

Just because you write a program, don’t be so sure that you can understand it later. If you write a program consisting of several thousand lines of instructions, you’re likely to forget how certain parts of the program work — especially if you put the program aside and work on another project for awhile. So writing programs that are easy to understand is crucial for your own benefit and for the benefit of any other programmer whose job is to fix or modify programs that you write.

To see how structured programming can make a program easier to read, look at the following program, which consists of sequential, branching, and looping instructions:

‘Sequential instructions

PRINT “This program prints a message, of your” PRINT “choosing, on the screen.”

PROMPT “What message do you want to appear”; Message$ PROMPT “Display message in all UPPERCASE (type U) or lowercase (type l)?”; WhatCase$

‘Branching instructions IF WhatCase$ = “U” THEN

Message$ = UPPER$(Message$)

END IF

IF WhatCase$ = “l” THEN

Message$ = LOWER$(Message$)

END IF

‘Looping instructions FOR I = 1 TO 15

PRINT SPACE$(I + 4); Message$ NEXT

END

Think of sequential, branching, or looping instructions as the building blocks of any program. If you write a program that uses only sequential, branching, and looping instructions, your program will be easier for you (or anyone else) to read and edit at a later date.