• Aucun résultat trouvé

Looping Using the until Statement

Dans le document Teach Yourself Perl 5 in 21 days (Page 86-92)

$count = $count + 1;

}

Although this code is correct, it's not easy to see that the statement

$done = 1;

is actually inside an if statement that is inside a while

statement. Larger and more complicated programs

rapidly become unreadable if you do not indent properly.

Looping Using the

until

Statement

Another way to loop in Perl is with the until statement. It is similar in appearance to the while statement, but it works in a slightly different way.

The while statement loops while its conditional expression is true.

The until statement loops until its conditional expression is true (that is, it loops

as long as its conditional expression is false).

Listing 2.7 contains an example of the until statement.

Listing 2.7. A program that uses the until statement.

1: #!/usr/local/bin/perl 2:

3: print ("What is 17 plus 26?\n");

4: $correct_answer = 43; # the correct answer 5: $input_answer = <STDIN>;

6: chop ($input_answer);

7: until ($input_answer == $correct_answer) { 8: print ("Wrong! Keep trying!\n");

9: $input_answer = <STDIN>;

10: chop ($input_answer);

11: }

12: print ("You've got it!\n");

$ program2_7

What is 17 plus 26?

39

Wrong! Keep trying!

43

You've got it!

$

Lines 3 and 4 set up the loop. Line 3 prints the following question on the screen

What is 17 plus 26?

Line 4 assigns the correct answer, 43, to $correct_answer.

Lines 5 and 6 retrieve the first attempt at the answer. Line 5 reads a line of input and

stores it in $input_answer. Line 6 chops off the newline character.

Line 7 tests whether the answer entered is correct by comparing $input_answer with

$correct_answer. If the two are not equal, the Perl interpreter continues with lines 8-10; if they are equal, the interpreter skips to line 12.

Line 8 prints the following on the screen:

Wrong! Keep trying!

Line 9 reads another attempt from the standard input file and stores it in

$input_answer.

Line 10 chops off the newline character. At this point, the Perl interpreter jumps back to line 7 and tests the new attempt.

The interpreter reaches line 12 when the answer is correct. At this point, the following message appears on the screen, and the program terminates:

You've got it!

The syntax for the until statement is

until (expr) {

statement_block }

As in the while statement, expr is a conditional expression, and statement_block is a statement block.

Summary

Today, you learned about scalar variables and how to assign values to them.

Scalar variables and values can be used by the arithmetic operators to perform the basic arithmetic operations of addition, subtraction, multiplication, and division. The chop

library function removes the trailing newline character from a line, which enables you to read scalar values from the standard input file.

A collection of operations and their values is known as an expression. The values operated on by a particular operator are called the operands of the operator. Each operator yields a result, which then can be used in other operations.

An expression can be divided into subexpressions, each of which is evaluated in turn.

Today you were introduced to the idea of a conditional statement. A conditional

statement consists of two components: a conditional expression, which yields a result of either true or false; and a statement block, which is a group of statements that is

executed only when the conditional expression is true.

Some conditional expressions contain the == operator, which returns true if its operands are numerically equal, and returns false if its operands are not.

The following conditional statements were described today:

The if statement, which is executed only if its conditional expression is true

The if-else statement, which chooses between two alternatives

The if-elsif-else statement, which chooses between multiple alternatives

The while statement, which loops while a condition is true

The until statement, which loops until a condition is true

You also learned about nesting conditional statements, as well as about infinite loops and how to avoid them.

Q&A

Q: Which should I use, the while statement or the until statement?

A: It doesn't matter, really; it just depends on which, in your judgment, is easier to read.

Once you learn about the other comparison operators on Day 4, "More

Operators," you'll be able to use the while statement wherever you can use an

until statement, and vice versa.

Q: In Listing 2.7, you read input from the standard input file in two separate places. Is there any way I can reduce this to one?

A: Yes, by using the do statement, which you'll encounter on Day 8, "More Control Structures."

Q: Do I really need both a $done variable and a $count variable in Listing 2.6?

A: No. On Day 4 you'll learn about comparison operators, which enable you to test whether a variable is less than or greater than a particular value. At that point, you won't need the $done variable.

Q: How many elsif parts can I have in an if-elsif-else statement?

A: Effectively, as many as you like. (There is an upper limit, but it's so large that you are not likely ever to reach it.)

Q: How much nesting of conditional statements does Perl allow? Can I put an

if inside a while that is inside an if that is inside an until?

A: Yes. You can nest as many levels deep as you like. Generally, though, you don't want to go too many levels down because your program will become difficult to read.

The logical operators, which you'll learn about on Day 4, make it possible to produce more complicated conditional expressions. They'll eliminate the need for too much nesting.

Workshop

The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.

Quiz

1. Define the following terms:

a. expression b. operand

c. conditional statement d. statement block

e. infinite loop

2. When does a while statement stop looping?

3. When does an until statement stop looping?

4. What does the == operator do?

5. What is the result when the following expression is evaluated?

14 + 6 * 3 - 10 / 2

6. Which of the following are legal scalar variable names?

a. $hello

b. $_test

c. $now_is_the_time_to_come_to_the_aid_of_the_party

d. $fries&gravy

e. $96tears f. $tea_for_2

Exercises

1. Write a Perl program that reads in a number, multiplies it by 2, and prints the result.

2. Write a Perl program that reads in two numbers and does the following:

It prints Error: can't divide by zero if the second number is 0.

If the first number is 0 or the second number is 1, it just prints the first number (because no division is necessary).

In all other cases, it divides the first number by the second number and prints the result.

3. Write a Perl program that uses the while statement to print out the first 10 numbers (1-10) in ascending order.

4. Write a Perl program that uses the until statement to print out the first 10 numbers in descending order (10-1).

5. BUG BUSTER: What is wrong with the following program? (Hint: there might be

more than one bug!)

#!/usr/local/bin/perl

$value = <STDIN>;

if ($value = 17) {

print ("You typed the number 17.\n");

else {

print ("You did not type the number 17.\n");

6. BUG BUSTER: What is wrong with the following program?

#!/usr/local/bin/perl

# program which prints the next five numbers after the

# number typed in

$input = <STDIN>;

chop ($input);

$input = $input + 1; # start with the next number;

$input = $terminate + 5; # we want to loop five times until ($input == $terminate) {

print ("The next number is ", $terminate, "\n");

Chapter 3

Floating-Point Arithmetic and Round-Off Error

Using Octal and Hexadecimal Notation

Interchangeability of Strings and Numeric Values

Initial Values of Scalar Variables

Today's lesson describes everything you need to know about scalar values in Perl.

Today, you learn about the following:

Scalar values

How integers are represented

Floating-point values

The octal and hexadecimal notations

Character strings, and using the double-quote and single-quote characters to

enclose them

Dans le document Teach Yourself Perl 5 in 21 days (Page 86-92)

Documents relatifs