You are on page 1of 14

## add 1 to counter \$counter = \$counter + 1;

if selection structure

true
sales >= 50 \$total = “Earned
print \$totalbonus!”

false

## unless selection structure

false
sales >= 50 \$counter = did
print “You \$counter + 1;
not earn your
bonus.”
true

false true
\$sales >= 50

print “You did not earn your bonus.” print “Earned bonus!”
print “You did not earn your bonus.” print “Earned bonus!”

## Fig. 3.3 Flowcharting the double-selection if/else control structure.

true
\$product
\$product<=
<=1000 \$product
\$product = 2 * \$product
= 2 * \$product
1000

false

## Fig. 3.4 Flowcharting the while repetition structure.

1 #!/usr/bin/perl Outline
2 # Fig. 3.5: fig03_05.pl
3 # Using the do/while repetition structure
4
The do/while repetition structure
5 \$counter = 1;
repeatedly prints the value of value of
6 variable \$counter while the variable is
7 do { less than or equal to 10.
8 print "\$counter ";
9 } while ( ++\$counter <= 10 );
10
11 print "\n";
After the each execution of the body of
the loop, the control variable \$counter
1 2 3 4 5 6 7 8 9 10 is pre-incremented.

1 #!/usr/bin/perl Outline
2 # Fig. 3.6: fig03_06.pl
3 # Using the do/until repetition structure
4
5 \$counter = 10;
The do/until repetition structure
6
repeatedly prints the value of variable
7 do {
\$counter until the variable is equal
8 print "\$counter ";
to 0.
9 } until ( --\$counter == 0 );
10
11 print "\n";
After the each execution of the body
of the loop, the control variable
10 9 8 7 6 5 4 3 2 1 \$counter is pre-decremented.

print "\$counter ";
print
“\$counter “;

true
--\$counter <=
++counter == 0
10

false

## Fig. 3.7 Flowcharting the do/while repetition structure.

print "\$counter ";
print
“\$counter “;

false
--\$counter == 00
--counter ==

true

## Fig. 3.7 Flowcharting the do/until repetition structure.

1 #!/usr/bin/perl Outline
2 # Fig. 3.10: fig03_10.pl
3 # Average-sales problem with counter-controlled repetition
4
5 # initialization phase
6 \$total = 0; # clear total
7 \$weekCounter = 1; # prepare to loop
8
9 # processing phase
Displays a prompt to the user
10 while ( \$weekCounter <= 10 ) { # loop 10 times
11
for the next sales amount.
The value the user inputs is stored
12 # prompt for input and input a sales value
in variable \$sales. Function
13 print "Enter sales for week \$weekCounter: "; The while structure executes
14 chomp( \$sales = <STDIN> ); chomp immediately removes the
while \$weekCounter is less
15 newline character from the input
than or equal to 10.
16 \$total += \$sales; # add sales to totalvalue.
17 ++\$weekCounter; # increment counter
18 }
20 \$average = \$total / 10; # divide to find average \$total with the new
21 print "\nSales averaged \$average computers per week\n"; \$sales value entered by
Assigns
thethe result of the
user.
averaging calculation to variable
average.
so the condition in the while structure
will eventually become false.

Enter sales for week 1: 56 Outline
Enter sales for week 2: 52
Enter sales for week 3: 64
Enter sales for week 4: 72
Enter sales for week 5: 56
Enter sales for week 6: 58
Enter sales for week 7: 62
Enter sales for week 8: 63
Enter sales for week 9: 48
Enter sales for week 10: 52

## Sales averaged 58.3 computers per week

1 #!/usr/bin/perl Outline
2 # Fig. 3.12: fig03_12.pl
3 # Average-sales problem with sentinel-controlled repetition
4
5 \$total = 0;
6 \$weekCounter = 0;
7
8 print "Enter sales for week or enter quit: "; The until structure executes until
9 chomp( \$sales = <STDIN> ); theReceive input
user enters thefrom user value.
sentinel
10 before entering until
11 until ( \$sales eq 'quit' ) { structure.
12 \$weekCounter++;
13 \$total += \$sales; The if/else structure determines
14 print "Enter sales for week or enter quit: "; The program
values have been
15 chomp( \$sales = <STDIN> ); with the new \$sales value
16 } The next value entered by by
is entered thethe
user.
user before the
entered by the user.
17 end of the until structure’s body.
18 if ( \$weekCounter != 0 ) {
19 \$average = \$total / \$weekCounter;
20 print "\nSales averaged \$average computers per week.\n";
21 }
22 else {
If variable \$weekCounter is 0, a
23 print "\nNo sales figures were entered.\n"; string is output indicating that no
24 } values have been entered.
If values have been entered, the program
calculates and outputs the average of all
values.

Enter sales for week or enter quit: 57 Outline
Enter sales for week or enter quit: 86
Enter sales for week or enter quit: 52
Enter sales for week or enter quit: 48
Enter sales for week or enter quit: quit

## Sales averaged 60.75 computers per week

1 #!/usr/bin/perl Outline
2 # Fig. 3.14: fig03_14.pl
3 # Analysis of sales results
4
5 # initialize loop variables
6 \$metQuota = 0; # employees who met quota
7 \$didNotMeetQuota = 0; # employees who did not meet quota
8 \$employeeCounter = 1; # employee counter
The while structure executes while
9
variable \$employeeCounter is less
10 # process 10 employees; counter-controlled loop
than or equal to 10.
11 while ( \$employeeCounter <= 10 ) {
12 print "Enter quota result, (yes or no): ";
13 chomp( \$result = <STDIN> );
14
15 # conditional operator nested in while
16 \$result eq 'yes' ? ++\$metQuota : ++\$didNotMeetQuota;
17
The conditional operator increments variable
18 \$employeeCounter++;
\$metQuota if the user has entered yes. Variable
19 }
If variableis\$metQuota’s
\$didNotMeetQuota value
incremented if the useris
20
21 # termination phase
has entered no.greater than 8, a string is output to
the user indicating that holiday
22 print "\nMet quota: \$metQuota\n";
bonuses should be raised.
23 print "Failed to meet quota: \$didNotMeetQuota\n";
24
25 if ( \$metQuota > 8 ) {
26 print "Raise holiday bonuses!\n";
27 }
Enter quota result, (yes or no): yes Outline
Enter quota result, (yes or no): yes
Enter quota result, (yes or no): no
Enter quota result, (yes or no): yes
Enter quota result, (yes or no): yes
Enter quota result, (yes or no): yes
Enter quota result, (yes or no): yes
Enter quota result, (yes or no): yes
Enter quota result, (yes or no): yes
Enter quota result, (yes or no): yes

Met quota: 9
Failed to meet quota: 1
Raise holiday bonuses!

## Enter quota result, (yes or no): no

Enter quota result, (yes or no): no
Enter quota result, (yes or no): no
Enter quota result, (yes or no): no
Enter quota result, (yes or no): yes
Enter quota result, (yes or no): yes
Enter quota result, (yes or no): no
Enter quota result, (yes or no): no
Enter quota result, (yes or no): yes
Enter quota result, (yes or no): no

Met quota: 3
Failed to meet quota: 7