0% found this document useful (0 votes)
12 views14 pages

Perl Control Structures and Flowcharts

The document describes the use of repetition structures in Perl programming. It contains examples of using while, do/while, do/until loops to repeatedly execute blocks of code until a condition is met. The while loop example prompts the user for sales amounts over 10 weeks and calculates the average. The do/while and do/until examples print the value of a counter variable as it is incremented or decremented each loop iteration. Sentinel value input is also demonstrated to control loop repetition until a special value is entered.

Uploaded by

api-3725802
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views14 pages

Perl Control Structures and Flowcharts

The document describes the use of repetition structures in Perl programming. It contains examples of using while, do/while, do/until loops to repeatedly execute blocks of code until a condition is met. The while loop example prompts the user for sales amounts over 10 weeks and calculates the average. The do/while and do/until examples print the value of a counter variable as it is incremented or decremented each loop iteration. Sentinel value input is also demonstrated to control loop repetition until a special value is entered.

Uploaded by

api-3725802
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

add grade to total $total = $total + $grade;

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

 2001 Prentice Hall, Inc. All rights reserved.


if selection structure

true
sales >= 50 $total = “Earned
print $totalbonus!”
+ grade;

false

unless selection structure

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

 2001 Prentice Hall, Inc. All rights reserved.


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.

 2001 Prentice Hall, Inc. All rights reserved.


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

false

Fig. 3.4 Flowcharting the while repetition structure.

 2001 Prentice Hall, Inc. All rights reserved.


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.

 2001 Prentice Hall, Inc. All rights reserved.


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.

 2001 Prentice Hall, Inc. All rights reserved.


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

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

false

Fig. 3.7 Flowcharting the do/while repetition structure.

 2001 Prentice Hall, Inc. All rights reserved.


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

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

true

Fig. 3.7 Flowcharting the do/until repetition structure.

 2001 Prentice Hall, Inc. All rights reserved.


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 }
19 The program updates
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
Adds 1 to variable $weekCounter
average.
so the condition in the while structure
will eventually become false.

 2001 Prentice Hall, Inc. All rights reserved.


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

 2001 Prentice Hall, Inc. All rights reserved.


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
whether anyupdates $total
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.

 2001 Prentice Hall, Inc. All rights reserved.


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

 2001 Prentice Hall, Inc. All rights reserved.


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 [Link] 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 }
 2001 Prentice Hall, Inc. All rights reserved.
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

 2001 Prentice Hall, Inc. All rights reserved.

You might also like