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

134 Part II: Learning Programming with Liberty BASIC

Looping a Fixed Number of Times

A conditional loop, such as the WHILE-WEND loop, stops repeating itself only if a certain condition becomes true or false. So the number of times that a loop repeats can vary from one moment to the next.

Sometimes, however, you may need to loop a specific number of times. Although you can still use a conditional loop, you may prefer to use another type of loop known as the FOR-NEXT loop, which in Liberty BASIC looks as follows:

FOR counter = start TO end ‘ One or more instructions NEXT

This loop tells the computer to repeat a fixed number of times the instructions between the FOR and NEXT commands. The start and end values determine the number of repeats. A typical FOR-NEXT loop looks as follows:

FOR I = 1 TO 10

PRINT “The square of “; I; “ is “; I * I

NEXT

END

If you run this program, here’s what happens:

1.The first line creates the variable I and tells the computer to keep repeating the command 10 times.

2.The second line tells the computer to print The square of 1 is 1. Each time that this line runs, the value of I is different, so the final output from the program looks as follows:

The square of 1 is 1

The square of 2 is 4

The square of 3 is 9

The square of 4 is 16

The square of 5 is 25

The square of 6 is 36

The square of 7 is 49

The square of 8 is 64

The square of 9 is 81

The square of 10 is 100

3.The third line tells the computer to go back to the second line.

4.The fourth line tells the computer that the program is at an end.

Chapter 10: Repeating Yourself with Loops 135

If the start value is less than the end value, the FOR-NEXT loop doesn’t run at all, as in the following example:

FOR counter = 8 TO 2

‘ This doesn’t work at all NEXT

Counting with different numbers

Most FOR-NEXT loops count from 1 to another fixed value, such as 10. The FOR-NEXT, however, loop can count from any number to any other number, as in the following example:

FOR I = 8 TO 14

PRINT “The value of I =”; I

NEXT

END

This loop repeats seven times and prints the following:

The value of I = 8

The value of I = 9

The value of I = 10

The value of I = 11

The value of I = 12

The value of I = 13

The value of I = 14

You can also use negative numbers for the start and end values, as follows:

FOR counter = -5 TO 3

‘ One or more instructions NEXT

Unless you have a good reason for choosing different numbers to start and end, a good idea is to always start at number 1. Using 1 as the starting point simply makes determining how many times the FOR-NEXT loop repeats easier for you or someone else.

Counting in increments

A FOR-NEXT loop counts by one. In the following example, the loop runs four times:

FOR counter = 1 TO 4

‘ One or more instructions NEXT

136 Part II: Learning Programming with Liberty BASIC

If you like, you can use the following STEP command to make the FOR-NEXT loop count in any increments other than one:

FOR counter = 1 TO 4 STEP increment ‘ One or more instructions

NEXT

If, for example, you want to count by twos, you can use the following FOR-

NEXT loop:

FOR I = 1 TO 8 STEP 2

PRINT “The value of I = “; I

NEXT

If you run this program, the FOR-NEXT loop doesn’t repeat eight times, but rather four times and prints this:

The value of I = 1

The value of I = 3

The value of I = 5

The value of I = 7

You can make a FOR-NEXT loop count backward by using a negative number for the increment value, which is the only time that you can have the start value greater than the end value, as shown in the following example:

FOR I = 6 TO -8 STEP -3

PRINT “The value of I = “; I

NEXT

If you run this program, the FOR-NEXT loop repeats five times, printing the following results:

The value of I = 6

The value of I = 3

The value of I = 0

The value of I = -3

The value of I = -6

Chapter 10: Repeating Yourself with Loops 137

How the FOR-NEXT loop looks in a C program

Because many people eventually graduate to programming in C, here’s a glimpse at how a FOR-NEXT loop looks in a C program:

main ()

{

int counter;

for (counter = 1; counter <= 5; counter++) {

printf (“The square of %d is %d\n”, counter, counter * counter);

}

}

This FOR-NEXT loop tells the computer, “Create an integer variable, counter, and set its value to 1 (counter = 1). Then keep incrementing the value of counter by 1 (counter++) until the Boolean expression (counter <= 5) becomes false. Then stop looping.” (This FOR-NEXT loop repeats itself exactly five times.)

If the C version of the FOR-NEXT loop looks a bit cryptic, that’s because it is. Now you know why learning the fundamentals of programming is much easier if you learn BASIC rather than C.

Exiting a FOR-NEXT loop prematurely

The FOR-NEXT loop repeats a fixed number of times. However, there may be times when you want to exit out of the FOR-NEXT prematurely. To do this, you can use the magic EXIT FOR command as follows:

FOR I = 1 to 10

PRINT “This is I = “; I

IF I = 5 THEN EXIT FOR

NEXT

END

Normally this FOR-NEXT loop would repeat ten (10) times but the IF I = 5 THEN EXIT FOR command tells the program to stop after the program has looped only five (5) times.

138 Part II: Learning Programming with Liberty BASIC

Part III

Advanced Programming with Liberty BASIC

In this part . . .

In this part you find all the fancy things you can do with Liberty BASIC — create graphics, make sounds, save data on a floppy disk or hard drive, create a real Windows user interface, and much more. After you understand these more advanced features of Liberty BASIC, you can create useful and interesting programs that rival the features that

you find in commercial-quality software.

To maximize your programming skills, make sure that you type and run the many Liberty BASIC programs that sprinkled throughout each chapter. And keep your eyes open for all the different ways to write the same program in both Liberty BASIC and other languages such as C/C++, Pascal, or Java. The more that you use Liberty BASIC to help you master general programming principles now, the easier you can migrate to another language such as C/C++ later.

Chapter 11

Writing Large Programs

by Using Subprograms

In This Chapter

Understanding structured programming

Dividing a program into modules

Creating subroutines

Creating functions

Programming is not so much a hard science as it is a creative art form. The ultimate goal of programming is to write the smallest possible pro-

gram that uses the least amount of computer resources (memory, hard drive space, and so on) while accomplishing as much as possible.

Although some people are naturally talented at writing small, tight, fast code (to use the lingo of the programming community), most people need guidelines to help them write programs.

Small programs are easy to write and read. But if you’re writing a large program that consists of several thousand lines of instructions, guess what? You’d better organize your program carefully from the start, or you may waste lots of time writing a large program that doesn’t work.

Breaking the Bad Programming

Habits of the Past

In the old days, people wrote programs without any advance planning, which is like trying to write a novel by sitting down at a word processor and typing continuously until you’re done. You can possibly write a decent novel that way, but you’re more likely to create an unreadable mess.

142 Part III: Advanced Programming with Liberty BASIC

Similarly, programmers used to write programs by typing commands into the computer just to get something to work. After they had a simple program that worked, they started adding new instructions into the program.

Unfortunately, programmers often didn’t add new instructions with any planning or organization. Some programmers added new instructions in the beginning of a program; others put new instructions at the end; and still others sprinkled new instructions throughout the program’s existing instructions so that determining where the new instructions began and the old ones ended was nearly impossible.

One reason programmers used to write programs that were hard to read is because they could use a special programming command, the GOTO command. The GOTO command tells the computer to jump to another part of the program. GOTO LabelOne, for example, tells the computer to suddenly jump to the part of the program that LabelOne identifies.

The GOTO command encouraged programmers to write programs that told the computer to jump from one place in the program to another to find instructions. Programs that use the GOTO command are like a novel in which page 2 tells you to jump to page 349, page 349 tells you to jump to page 34, page 34 tells you to jump to page 125, and so on.

Because bouncing from one part of a program to another can prove as confusing as trying to untangle strands of spaghetti, writing programs in this manner became known as spaghetti coding.

Consider, for example, the following Liberty BASIC program:

GOTO [LabelOne] [LabelFour]

PROMPT “How much money do you have”; MyCash GOTO [LabelThree]

[LabelTwo] END [LabelOne]

GOTO [LabelFour] [LabelThree]

PRINT “You owe me = “; MyCash * .95 GOTO [LabelTwo]

Trying to figure out what this program does is confusing because the instructions are jumbled. But the following breakdown shows you how the program works:

Chapter 11: Writing Large Programs by Using Subprograms 143

1.The first line tells the computer to jump to the part of the program that the label [LabelOne] (which happens to be on the seventh line of the program) identifies.

2.The computer jumps to the seventh line in the program, which contains the label [LabelOne].

3.The eighth line in the program tells the computer to jump to the part of the program that the label [LabelFour] (which happens to be the second line of the program) identifies.

4.The computer jumps to the second line in the program, which contains the label [LabelFour].

5.The third line displays a Prompt dialog box that asks, How much money do you have? on-screen and waits for the user to type a number, which the program stores in the variable MyCash.

6.The fourth line tells the computer to jump to the part of the program that the label [LabelThree] (which happens to be the ninth line in the program) identifies.

7.The computer jumps to the ninth line in the program, which contains the label [LabelThree].

8.The tenth line prints, You owe me = and follows it with the value of MyCash multiplied by .95.

9.The eleventh line tells the computer to jump to the part of the program that the label [LabelTwo] (which happens to be the fifth line of the program) identifies.

10.The computer jumps to the fifth line of the program, which contains the label [LabelTwo].

11.The sixth line tells the computer that the program is at an end.

The preceding GOTO program is equivalent to the following simple program:

PROMPT “How much money do you have:”; MyCash

PRINT “You owe me =”; MyCash * .95

END

In the early days of computer programming, structures such as IF THEN and SELECT CASE statements didn’t exist. (See Chapter 9 for more information about IF THEN statements.) For many years, the GOTO command was the only way programmers had to tell the computer to skip over certain instructions or follow a different set of instructions. Although you can still use the GOTO command, you should avoid using GOTO commands whenever possible.