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

Chapter 9: Making Decisions with Control Statements 125

Make sure that you don’t use the same value on two separate CASE statements, as happens in the following example:

PROMPT Type a number. “; Answer

SELECT CASE Answer

CASE 1, 2

NOTICE “This always prints if you type a 2.”

CASE 2, 3

NOTICE “This never prints if you type a 2.”

END SELECT

In the preceding SELECT CASE statement, the program prints This always prints if you type a 2 if the user types 2, but the program never prints the instructions under the second CASE statement (CASE 2, 3). That’s because the first CASE statement runs first, preventing the second CASE statement from getting a chance to run at all.

Checking a relational operator

Sometimes, checking for an exact value or a range of values may still prove too limiting. You may, for example, compare a variable to another value by using one of those friendly symbols known as relational operators. A relational operator enables the SELECT CASE statement to determine whether a variable is greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=), or not equal to (<>) a specific value.

To use a relational operator, you must use a slightly different version of the CASE statement as shown in the following example:

INPUT “How many cats do you own”; Answer

SELECT CASE

CASE (Answer <= 5)

PRINT “You need more cats.”

CASE (Answer > 5)

PRINT “Are you out of your mind?”

END SELECT

END

If the user types any number equal to or less than 5, the program prints, You need more cats. If the user types any number greater than 5, the program prints, Are you out of your mind?

126 Part II: Learning Programming with Liberty BASIC

Beware of the SELECT CASE statement in C/C++ and Java

In BASIC and many other programming languages such as Pascal, the SELECT CASE statement runs only one set of instructions the moment that it finds a match, such as printing

You’re still officially a teenager. in the Liberty BASIC example in the section, “Working with SELECT CASE Statements,” earlier in this chapter, if the user types the number 19.

C/C++ and Java programs, however, behave much differently. If you use these languages, you must specifically tell the computer to stop following instructions in a SELECT CASE statement (technically known as a switch statement in C/C++ and Java) by using the command break.

Consider, for example, the following C program:

#include <stdio.h> main ()

{

char akey;

printf (“Type a lower case letter “);

scanf(“ “);

scanf (“%c”, &akey); switch (akey) {

case ‘a’: printf (“You pressed the A key.\n”);

case ‘b’: printf (“You pressed the B key.\n”);

}

}

If you run this program and press the A key, this C program prints the following:

You pressed the A key.

You pressed the B key.

In C/C++ and Java, the computer follows every set of instructions in the switch statement from the first match that it finds to the end. To make sure that a C/C++ or Java program stops following any instructions in a switch statement, you must insert the break command as follows:

#include <stdio.h> main ()

{

char akey;

printf (“Type a lower case letter “);

scanf(“ “);

scanf (“%c”, &akey); switch (akey) {

case ‘a’: printf (“You pressed the A key.\n”);

break;

case ‘b’: printf (“You pressed the B key.\n”);

}

}

If you eventually plan to program in C/C++ or Java, you need to remember this subtle difference or you may find your C/C++ or Java programs acting different from similar BASIC programs.

The two crucial differences when using relational operators in a SELECT-CASE statement is:

Chapter 9: Making Decisions with Control Statements 127

The SELECT CASE variable (which is “Answer” in the above example), does not appear directly after the SELECT CASE command.

The relational expression (such as “Answer <= 5”) appears directly after each CASE statement.

Make sure that your relational operators don’t overlap another part of the SELECT CASE statement, as happens in the following example:

SELECT CASE

CASE (Answer < 10)

PRINT “This always prints.”

CASE (Answer < 12)

PRINT “This prints only if the user types 11.”

END SELECT

In this SELECT CASE statement, the program prints This always prints if the user types any number less than 10, but the program prints the instructions under the second CASE statement (CASE IS < 12) only if the user types 11.

128 Part II: Learning Programming with Liberty BASIC

Chapter 10

Repeating Yourself with Loops

In This Chapter

Looping with the WHILE-WEND commands

Looping a fixed number of times with the FOR-NEXT loop

In general, programmers try to get the computer to do as much as possible so that they can do as little as possible. Ideally, you want to write the

smallest programs possible, not only because small programs are easier to debug and modify, but also because smaller programs require less typing.

One way that programmers write as little as possible is by using programming structures known as loops. The idea behind a loop is to make the computer repeat one or more instructions. Consider, for example, the following program that prints the numbers 1 through 5 on-screen:

PRINT 1

PRINT 2

PRINT 3

PRINT 4

PRINT 5

END

If you want to expand this program to print out five million numbers, guess what? You must type five million instructions. Because you really don’t want to type that many instructions, you can use loops to make the computer repeat the same instructions multiple times. The computer does the hard work. Consider the following, which in programming lingo is called a FOR-NEXT loop:

FOR I = 1 TO 5

PRINT I

NEXT I

END

130 Part II: Learning Programming with Liberty BASIC

This program just prints out the following:

1

2

3

4

5

As you can see, when you use a loop, you can write shorter programs.

If you run this program, it does exactly the same thing as the first BASIC program. The loop version of this program, however, can print out five numbers or five million numbers (by changing the number 5 in the program to 5000000). Loops make the computer do more without forcing you to type additional instructions.

Although loops can help create shorter programs, the tradeoff is that loops are also harder to read and understand than a straightforward list of instructions. If you create a loop, make sure that you write a comment in the program to explain exactly what you expect the loop to do. (See Chapter 7 for more information about comments.)

A loop forces the computer to run the same instructions over and over, but eventually the computer needs to know when to stop. To tell the computer when to stop looping, you use a condition that represents either true or false.

In the world of mathematics and programming, anything that represents a true or false value is known as a Boolean expression; see Chapter 9. Some examples of Boolean expressions are 4 > 9.48 (which in Boolean arithmetic represents as a false value).

Using the WHILE-WEND Loop

Loops are handy for repeating one or more instructions. Of course, whenever you create a loop, you need to make the loop stop eventually.

One of the most common problems with loops is something known as an endless loop, which means that the computer follows a set of instructions but never stops. If a program gets caught in an endless loop, the program may appear to freeze on-screen. Usually, the only way to get out of an endless loop is to turn the computer off and then on again.

One way to make a loop eventually stop is to check if a certain condition is true. To do this in Liberty BASIC, you can create a special loop called a WHILE-WEND loop that looks as follows:

Chapter 10: Repeating Yourself with Loops 131

WHILE (Boolean expression is true) ‘ One or more instructions

WEND

To repeat one or more instructions, you just sandwich them between the WHILE and WEND commands, as shown in the following example:

I = 1

WHILE I < 5

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

I = I + 1

WEND

END

This program does the following:

1.The first line creates the variable I and sets its value to 1.

2.The second line tells the computer that a loop is starting and that the computer is to run the instructions between the WHILE and WEND commands as long as the Boolean expression I < 5 is true.

3.The third line tells the computer to print The square of 1 is 1 the first time that the WHILE-WEND loops runs and The square of 2 is 4 the second time that the WHILE-WEND loop runs, and so on.

4.The fourth line tells the computer to add one to the value of the variable I, so now I represents the number 1 + 1, or 2.

5.The fifth line tells the computer to check whether the Boolean expression I < 5 is true. If it’s true, the program skips to the sixth line. If it’s not true (if I represents the number 1, 2, 3, or 4), the program returns to the top of the loop on the second line. The computer repeats the loop four times to print out the following:

The square of 1 is 1

The square of 2 is 4

The square of 3 is 9

The square of 4 is 16

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

Exiting a WHILE-WEND loop prematurely

Normally the only way to exit out of a WHILE-WEND loop is to wait until a certain condition becomes true. However, Liberty BASIC (like many other programming languages) also allows you to exit out of a loop prematurely using the magic EXIT WHILE command as follows: