Module 4 Resource

  • October 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Module 4 Resource as PDF for free.

More details

  • Words: 1,290
  • Pages: 14
add grade to total

add 1 to counter

 2001 Prentice Hall, Inc. All rights reserved.

$total = $total + $grade;

$counter = $counter + 1;

if selection structure

true sales >= 50

$total = “Earned $totalbonus!” + grade; print

false

unless selection structure

false sales >= 50

$counter = did $counter + 1; print “You not earn your bonus.”

true

 2001 Prentice Hall, Inc. All rights reserved.

false

$sales >= 50

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

Fig. 3.3

true

print “Earned bonus!” print “Earned bonus!”

Flowcharting the double-selection if/else control structure.

 2001 Prentice Hall, Inc. All rights reserved.

true $product $product<= <=1000 1000

$product = 2 * $product $product = 2 * $product

false

Fig. 3.4

Flowcharting the while repetition structure.

 2001 Prentice Hall, Inc. All rights reserved.

Outline

1

#!/usr/bin/perl

2

# Fig. 3.5: fig03_05.pl

3

# Using the do/while repetition structure

4 5

$counter = 1;

6 7 8 9

do {

The do/while repetition structure repeatedly prints the value of value of variable $counter while the variable is less than or equal to 10.

print "$counter "; } while ( ++$counter <= 10 );

10 11 print "\n";

1 2 3 4 5 6 7 8 9 10

 2001 Prentice Hall, Inc. All rights reserved.

After the each execution of the body of the loop, the control variable $counter is pre-incremented.

Outline

1

#!/usr/bin/perl

2

# Fig. 3.6: fig03_06.pl

3

# Using the do/until repetition structure

4 5

$counter = 10;

6 7 8 9

do { print "$counter "; } until ( --$counter == 0 );

The do/until repetition structure repeatedly prints the value of variable $counter until the variable is equal to 0.

10 11 print "\n";

10 9 8 7 6 5 4 3 2 1

 2001 Prentice Hall, Inc. All rights reserved.

After the each execution of the body of the loop, the control variable $counter is pre-decremented.

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

true --$counter <= == 0 ++counter 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

#!/usr/bin/perl # Fig. 3.10: fig03_10.pl # Average-sales problem with counter-controlled repetition # initialization phase $total = 0; $weekCounter = 1;

# clear total # prepare to loop

# processing phase while ( $weekCounter <= 10 ) {

# loop 10 times

# prompt for input and input a sales value print "Enter sales for week $weekCounter: "; chomp( $sales = <STDIN> ); $total += $sales; ++$weekCounter; } $average = $total / 10; print "\nSales averaged $average

 2001 Prentice Hall, Inc. All rights reserved.

Outline

Displays a prompt to the user for the next sales amount. The value the user inputs is stored in variable $sales. Function The while structure executes chomp immediately removes the while $weekCounter is less newline character from the input than or equal to 10. totalvalue.

# add sales to # increment counter

The program updates $total with the new # divide to find average computers per week\n"; $sales value entered by Assigns result of the thethe user. averaging calculation to variable Adds 1 to variable $weekCounter average. so the condition in the while structure will eventually become false.

Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter

sales sales sales sales sales sales sales sales sales sales

for for for for for for for for for for

week week week week week week week week week week

1: 56 2: 52 3: 64 4: 72 5: 56 6: 58 7: 62 8: 63 9: 48 10: 52

Sales averaged 58.3 computers per week

 2001 Prentice Hall, Inc. All rights reserved.

Outline

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

#!/usr/bin/perl # Fig. 3.12: fig03_12.pl # Average-sales problem with sentinel-controlled repetition

Outline

$total = 0; $weekCounter = 0; print "Enter sales for week or enter quit: "; chomp( $sales = <STDIN> ); until ( $sales eq 'quit' ) { $weekCounter++; $total += $sales; print "Enter sales for week or enter quit: "; chomp( $sales = <STDIN> ); The next }

The until structure executes until input user value. theReceive user enters thefrom sentinel before entering until structure.

The if/else structure determines The program $total whether anyupdates values have been with the new $sales value by by thethe user. value entered is entered user before the entered by the user. end of the until structure’s body.

if ( $weekCounter != 0 ) { $average = $total / $weekCounter; print "\nSales averaged $average computers per week.\n"; } If variable $weekCounter is 0, else { string is output indicating that no print "\nNo sales figures were entered.\n"; 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.

a

Enter Enter Enter Enter Enter

sales sales sales sales sales

for for for for for

week week week week week

or or or or or

enter enter enter enter enter

quit: quit: quit: quit: quit:

57 86 52 48 quit

Sales averaged 60.75 computers per week

 2001 Prentice Hall, Inc. All rights reserved.

Outline

Outline

1

#!/usr/bin/perl

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

9 10 # process 10 employees; counter-controlled loop 11 while ( $employeeCounter <= 10 ) { 12

print "Enter quota result, (yes or no): ";

13

chomp( $result = <STDIN> );

The while structure executes while variable $employeeCounter is less than or equal to 10.

14 15

# conditional operator nested in while

16

$result eq 'yes' ? ++$metQuota : ++$didNotMeetQuota;

17 18

$employeeCounter++;

19 } 20 21 # termination phase 22 print "\nMet quota: $metQuota\n";

The conditional operator increments variable $metQuota if the user has entered yes. Variable If variableis$metQuota’s value $didNotMeetQuota incremented if the useris has entered no.greater than 8, a string is output to the user indicating that holiday 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 Enter Enter Enter Enter Enter Enter Enter Enter Enter

quota quota quota quota quota quota quota quota quota quota

result, result, result, result, result, result, result, result, result, result,

(yes (yes (yes (yes (yes (yes (yes (yes (yes (yes

or or or or or or or or or or

no): no): no): no): no): no): no): no): no): no):

yes yes no yes yes yes yes yes yes yes

or or or or or or or or or or

no): no): no): no): no): no): no): no): no): no):

no no no no yes yes no no yes no

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

Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter

quota quota quota quota quota quota quota quota quota quota

result, result, result, result, result, result, result, result, result, result,

(yes (yes (yes (yes (yes (yes (yes (yes (yes (yes

Met quota: 3 Failed to meet quota: 7

 2001 Prentice Hall, Inc. All rights reserved.

Outline

Related Documents

Module 4 Resource
October 2019 15
Module 5 Resource
October 2019 12
Module 6 Resource 2
October 2019 20
Module 3 Resource
October 2019 11
Module 2 Resource
October 2019 14
Module 4
May 2020 20