acontecimientos sociales

Páginas: 37 (9157 palabras) Publicado: 25 de julio de 2014
Loops and Decisions
Most of the programs so far have been very simply structured – they've done one statement after
another in turn. If we were to represent statements as boxes, our programs would look like this:

Statement

Statement

Statement

This sort of diagram is called a flow chart, and programmers have used them for a long time to help
design their programs. They're considereda bit passé these days, but they're still useful. The path Perl
(or any other language) takes by following the arrows is called the flow of execution of the program.
Boxes denote statements (or a single group of statements), and diamonds denote tests. There are also a
whole host of other symbols for magnetic tapes, drum storage, and all sorts of wonderful devices, now
happily lost in themists of time.

Chapter 4
In the last chapter, we introduced the for loop to our growing repertoire of programming tools. Instead
of taking a straight line through our program, perl did a block of statements again and again for each
element of the list. The for loop is a control structure – it controls the flow of execution. Instead of
going in a straight line, we can now go around in a loop:for each
element

Do something

Not only that, but we can choose our path through the program depending on certain things, like the
comparisons we looked at in Chapter 2. For instance, we'll do something if two strings are equal:

Strings are
equal?
Yes

No

Do this

Do that

We'll take a look at the other sorts of control structures we have in Perl, for example, structuresthat do
things if or unless something is true. We'll see structures that do things while something is true or until it
is true. Structures that loop for a certain number of times, or for each element in a list. Each of the
words in bold is a Perl keyword, and we'll examine each of them in this chapter.

Deciding If...
Let's first extend our previous currency conversion program a little, usingwhat we learned about hashes
from the previous chapter.

114

Loops and Decisions
Try It Out : Multiple Currency Converter
We'll use a hash to store the exchange rates for several countries. Our program will ask for a currency to
convert from, then a currency to convert to, and the amount of currency we would like to convert:
#!/usr/bin/perl
# convert1.plx
use warnings;
use strict;
my($value, $from, $to, $rate, %rates);
%rates = (
pounds
=> 1,
dollars
=> 1.6,
marks
=> 3.0,
"french francs" => 10.0,
yen
=> 174.8,
"swiss francs" => 2.43,
drachma
=> 492.3,
euro
=> 1.5
);
print "Enter your starting currency: ";
$from = ;
print "Enter your target currency: ";
$to = ;
print "Enter your amount: ";
$value = ;
chomp($from,$to,$value);
$rate = $rates{$to} /$rates{$from};
print "$value $from is ",$value*$rate," $to.\n";

Here's a sample run of this program:
> perl convert1.plx
Enter your starting currency: dollars
Enter your target currency: euro
Enter your amount: 200
200 dollars is 187.5 euro.
>
Let's first see how this all works, and then we'll see what's wrong with it.

How It Works
The first thing we do is to declare all our variables.You don't have to do this at the start of the program,
but it helps us organize:
my ($value, $from, $to, $rate, %rates);

115

Chapter 4
Note that you do need to put brackets when you're declaring more than one variable at once. Once
again it's a question of precedence – my has a lower precedence than a comma. Now we can set out our
rates table:
%rates = (
pounds
dollars
marks"french francs"
yen
"swiss francs"
drachma
euro
);

=>
=>
=>
=>
=>
=>
=>
=>

1,
1.6,
3.0,
10.0,
174.8,
2.43,
492.3,
1.5

Using as we did in the last chapter to read a line from the console, we'll ask for two currencies
and the amount:
print "Enter your starting currency: ";
$from = ;
print "Enter your target currency: ";
$to = ;
print "Enter your amount: ";
$value = ;...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Acontecimiento social
  • Acontecimientos sociales
  • acontecimientos sociales
  • acontecimiento social
  • acontecimiento social
  • Acontecimientos Sociale
  • Acontecimientos sociales
  • Acontecimientos Sociales

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS