Download presentation
Presentation is loading. Please wait.
1
Chapter 5 - Control Structures: Part 2
Outline 5.1 Introduction 5.2 foreach Repetition Structure 5.3 Special Variable $_ 5.4 Loop control Shortcuts: grep and map 5.5 Case Study: Computing Mean, Median and Mode 5.6 Loop Controls: The next Statement 5.7 Loop Controls: The last Statement 5.8 Loop controls: The redo Statement 5.9 Block Labels 5.10 Bare Labels 5.11 Logical Operators 5.12 Using Logical Operators for Flow Control 5.13 Error Functions: die and warn 5.14 Summary of Structured Programming
2
Integers 1 through 10 are stored in @array using the range operator.
1 #!/usr/bin/perl 2 # Fig. 5.1: fig05_01.pl 3 # Using foreach to iterate over an array. 4 = ( ); # create array containing 1-10 6 7 foreach $number ) { # for each element 8 $number **= 2; # square the value 9 } 10 11 print # display the results Integers 1 through 10 are stored using the range operator. The foreach repetition structure allows a programmer to iterate over a list of values, accessing and manipulating each element. When the foreach structure executes, $number is assigned the first element of the list. Changing the value of the control variable in the body of a foreach structure modifies the element that the control variable is currently representing. The value of variable $number is raised to the second power using the exponentiation operator.
3
Standard for structure.
1 #!/usr/bin/perl 2 # Fig. 5.2: fig05_02.pl 3 # Demonstrating different properties of the foreach structure. 4 5 # This structure loops 10 times, not requiring a control variable. 6 foreach ( ) { 7 print "*"; 8 } 9 10 print "\n"; 11 12 # Standard foreach structure. Prints the letters A-G. 13 foreach $letter ( 'A' .. 'G' ) { 14 print "$letter"; 15 } 16 17 print "\n"; 18 19 # Duplicate of the structure above, but with the for keyword. 20 # This structure will behave exactly the same way. 21 for $letter ( 'A' .. 'G' ) { 22 print "$letter"; 23 } 24 25 print "\n"; 26 27 # Standard for structure. 28 # Loops 5 times, printing the multiples of 5 from 0-20. 29 for ( $number = 0; $number <= 20; $number += 5 ) { 30 print "$number "; 31 } The foreach structure loops 10 times, printing an asterisk with each iteration. A standard foreach structure in which $letter is the control variable. The repetition structure iterates over and prints the letters from A through G. The for structure is identical to the foreach structure above. The foreach keyword itself is interchangeable with the keyword for. Standard for structure.
4
A duplicate of the standard for structure with the foreach keyword.
32 33 print "\n"; 34 35 # Duplicate of the structure above with the foreach keyword. 36 # Again, this structure will behave the same as the one above. 37 foreach ( $number = 0; $number <= 20; $number += 5 ) { 38 print "$number "; 39 } 40 41 print "\n"; A duplicate of the standard for structure with the foreach keyword. ********** ABCDEFG
5
1 #!/usr/bin/perl 2 # Fig. 5.3: fig05_03.pl 3 # Using variable $_ with foreach and print. 4 5 # Standard foreach structure, printing the values in a list 6 foreach $name ( 'Amanda ', 'Jeff ', 'Sarah ', 'David ' ) { 7 print "$name"; 8 } 9 10 print "\n"; 11 12 # $_ takes the place of the control variable 13 foreach ( 'Amanda ', 'Jeff ', 'Sarah ', 'David ' ) { 14 print "$_"; 15 } 16 17 print "\n"; 18 19 foreach ( 'Amanda ', 'Jeff ', 'Sarah ', 'David ' ) { 20 print; # print the default variable's ($_) value 21 } 22 23 print "\n"; When no control variable is explicitly stated, the special variable $_ stores the value of the current element of the list during each iteration of the loop. During each iteration of the loop, print implicitly uses the variable $_ to output each name. Amanda Jeff Sarah David
6
1 #!/usr/bin/perl 2 # Fig. 5.4: fig05_04.pl 3 # Demonstrating map and grep. 4 = ( ); 6 7 8 # locate all numbers less than 6 9 foreach ) { 10 $_ ) if $_ < 6; 11 } 12 13 print "Numbers less than 6:\n", 14 15 = grep( $_ < ); 17 print "grep: @smallNumbers2\n\n"; 18 19 # multiply every number by 2 20 foreach ) { 21 $_ * 2 ); 22 } 23 24 print "Double each number:\n", 25 26 = map( $_ * ); 28 print "map: @doubledNumbers2\n"; This foreach repetition structure is equivalent to the grep function call. The call to grep specifies that each element that is less than 6 should be part of the resulting list (which is assigned Function grep searches through a list and creates a new list containing only elements that satisfy a specified condition. The call to map specifies that each element should be multiplied by 2 and then placed in the resulting list (which is assigned This foreach repetition structure is equivalent to the map function call.
7
@numbers: Numbers less than 6: foreach: grep: Double each number: foreach: map:
8
1 #!/usr/bin/perl 2 # Fig. 5.5: fig05_05.pl 3 # Survey data analysis: Determining the mean, median and mode. 4 = ( 8, 9, 4, 7, 8, 5, 6, 4, 9, 9, , 8, 9, 5, 4, 8, 7, 8, 7, 7, , 6, 8, 9, 1, 9, 8, 7, 8, 7, , 8, 9, 8, 9, 4, 9, 6, 8, 4, , 7, 3, 4, 8, 7, 9, 8, 9, 2 ); 10 11 # determine the mean 12 $total = 0; 13 14 foreach ) { 15 $total += $_; 16 } 17 18 $mean = $total 19 print "Survey mean result: $mean\n"; 20 21 # determine the median = sort { $a <=> $b 23 $middle / 2; # middle element subscript 24 The mean is computed by totaling the 50 elements and dividing the result which returns the array’s length. The array of responses is sorted into ascending numerical order by calling Perl’s sort function with the <=> numerical comparison operator. The elements are divided by 2 to determine the latter of the two middle-element subscripts in the array.
9
The foreach structure iterates through each element in @opinions.
25 # for an even number of elements, average the two middle 26 # elements to determine the median; otherwise, use the 27 # middle element 28 if %2 == 0 ) { # even number of elements 29 $median = ( $sorted[ $middle - 1 ] + $sorted[ $middle ] ) / 2; 31 } 32 else { # odd number of elements 33 $median = $sorted[ $middle ]; 34 } 35 36 print "Survey median result: $median\n"; 37 38 # determine the mode 39 $mode = 0; 40 41 foreach ) { $frequency[ $_ ]; # increment the frequency counter 43 44 # if the current frequency is greater than the $mode's 45 # frequency, change $mode to $_ 46 if ( $frequency[ $_ ] > $frequency[ $mode ] ) { $mode = $_; 48 } 49 } 50 51 print "Survey mode result: $mode\n\n"; 52 The if/else structure determines if the number of elements in the array is even or odd. If the number is even, the average of the two middle elements is calculated. The foreach structure iterates through each element If the array contains an odd number of elements, the value of the middle element of the array is determined. This expression accesses the value with index $_ and uses ++ to add 1 to the element’s value. The if structure calculates the current mode by checking if the count for the current response ($_) is greater than the count for the current $mode.
10
53 # display a frequency graph
54 print "Response\tFrequency\n"; 55 print " \t \n"; 56 57 foreach ( ) { 58 print "$_\t\t", "*" x $frequency[ $_ ], "\n"; 59 } Survey mean result: Survey median result: 7 Survey mode result: 8 Response Frequency * * * ****** ** ***** ********** ************* ***********
11
Function sort orders the keys into lexically ascending order.
1 #!/usr/bin/perl 2 # Fig. 5.6: fig05_06.pl 3 # Using foreach loops with hashes. 4 = qw( what word is being used most in this array is what this is used what most is is array what word used is most is array what is this is array what is is array this is most ); 9 10 foreach ) { $hash{ $_ }; 12 } 13 14 # display sorted by key in ascending order 15 print "Word\tFrequency\n"; 16 print "----\t \n"; 17 18 foreach ( sort keys( %hash ) ) { 19 print "$_\t", "*" x $hash{ $_ }, "\n"; 20 } 21 22 # display sorted by frequency in descending order 23 print "\nWord\tFrequency\n"; 24 print "----\t \n"; 25 26 foreach ( sort { $hash{ $b } <=> $hash{ $a } } keys( %hash ) ) { 27 print "$_\t", "*" x $hash{ $_ }, "\n"; 28 } The foreach structure counts the frequency of each string in the array. In this case, the counters are elements in a hash. Function sort orders the keys into lexically ascending order. Function keys returns the set of keys in %hash. The results are displayed, sorted by key, in ascending order. Prints the current value in the list ($_), followed by a tab character to start a new column, and a row of asterisks corresponding to the current value in the list. The sorting order $hash{ $b } <=> $hash{ $a } specifies that the values in the hash for keys $b and $a should be compared and sorted in descending numerical order. The foreach structure iterates over the list returned by the expression sort keys( %hash ).
12
Word Frequency array ***** being * in * is ************ most **** this **** used *** what ****** word **
13
1 #!/usr/bin/perl 2 # Fig. 5.7: fig05_07.pl 3 # Using the next statement in a foreach structure. 4 5 foreach ( ) { 6 7 if ( $_ == 5 ) { $skipped = $_; # store skipped value next; # skip remaining code in loop only if $_ is 5 10 } 11 12 print "$_ "; 13 } 14 15 print "\n\nUsed 'next' to skip the value $skipped.\n"; If $_ is equal to 5, then the the current value of $_ is stored in variable $skipped and the next statement is executed. The next statement skips the remaining statements in the body of that structure and performs the next iteration of the loop. Used 'next' to skip the value 5.
14
The last statement causes immediate exit from the foreach structure.
1 #!/usr/bin/perl 2 # Fig. 5.8: fig05_08.pl 3 # Using the last statement in a foreach structure. 4 5 foreach ( ) { 6 7 if ( $_ == 5 ) { $number = $_; # store current value before loop ends last; # jump to end of foreach structure 10 } 11 12 print "$_ "; 13 } 14 15 print "\n\nUsed 'last' to terminate loop at $number.\n"; When the if structure detects that $_ is 5, $number is assigned the current value of $_ and last executes. The last statement causes immediate exit from the foreach structure. Used 'last' to terminate loop at 5.
15
1 #!/usr/bin/perl 2 # Fig. 5.9: fig05_09.pl 3 # Using the redo statement in a while structure. 4 5 $number = 1; 6 7 while ( $number <= 5 ) { 8 9 if ( $number <= 10 ) { print "$number "; $number; redo; # Continue loop without testing ( $number <= 5 ) 13 } 14 } 15 16 print "\nStopped when \$number became $number.\n"; The if structure in prints $number, increments $number and executes the redo statement as long as $number is less than or equal to 10. When redo executes, program control continues, starting from the {; the loop-continuation condition is not tested. The redo statement returns to the first statement in the body of the loop without evaluating the loop-continuation test. Stopped when $number became 11.
16
A for structure with a label.
1 #!/usr/bin/perl 2 # Fig. 5.10: fig05_10.pl 3 # Using block labels with next. 4 5 LOOP: for ( $number = 1; $number <= 10; ++$number ) { next LOOP if ( $number % 2 == 0 ); print "$number "; # displays only odd numbers } A for structure with a label. When next executes, it forces the next iteration of the block labeled LOOP.
17
1 #!/usr/bin/perl 2 # Fig. 5.11: fig05_11.pl 3 # Using block labels with next in nested looping structures. 4 5 OUTER: foreach $row ( ) { 6 7 INNER: foreach $column ( ) { 8 if ( $row < $column ) { print "\n"; next OUTER; } 13 print "$column "; } } When this statement executes, loop control continues with the OUTER block, ignoring the remaining iterations of INNER. 1 1 2
18
1 #!/usr/bin/perl 2 # Fig. 5.12: fig05_12.pl 3 # Using bare blocks to form a multiple-selection structure. 4 5 print "Enter your guess (1-3): "; 6 $guess = <STDIN>; 7 8 BLOCK: { # start of bare block 9 if ( $guess == 1 ) { print "Right!\n"; last BLOCK; # jump to end of BLOCK 12 } 13 14 if ( $guess == 2 ) { print "Close!\n"; last BLOCK; # jump to end of BLOCK 17 } 18 19 if ( $guess == 3 ) { print "Wrong!\n"; last BLOCK; # jump to end of BLOCK 22 } 23 24 # default case; executes only if $guess is not 1, 2 or 3 25 { print "Please re-enter your guess (1-3): "; $guess = <STDIN>; redo BLOCK; # jump to beginning of BLOCK 29 } 30 } # start of bare block A bare block is a block (zero or more lines) of code enclosed by curly braces, with or without a label, but with no accompanying control-structure keyword. This statement immediately terminates the BLOCK bare block when it executes. So, only the body of the first if structure with a true condition will execute. If none of the if-structure bodies executes, program control continues with the bare block, which acts as a “default case” in this simulated multiple-selection structure. The redo statement returns control to the beginning of the BLOCK bare block.
19
Enter your guess (1-3): 1 Right! Enter your guess (1-3): 77 Please re-enter your guess (1-3): 22 Please re-enter your guess (1-3): 1 Right!
20
Fig. 5.13 Truth table for the && (logical AND) operator.
expression1 expression2 && false true True Fig Truth table for the && (logical AND) operator. expression1 expression2 || false F alse T rue true Fig Truth table for the || (logical OR) operator. Fig Truth table for operator ! (logical negation).
21
1 #!/usr/bin/perl 2 # Fig. 5.16: fig05_16.pl 3 # Using logical operators in flow control. 4 5 print "Please enter a numerator: "; 6 chomp( $numerator = <STDIN> ); 7 8 # bare block; redo continues from beginning of this block 9 { 10 print "Please enter a denominator: "; 11 chomp( $denominator = <STDIN> ); 12 $denominator != 0 or print "Cannot divide by zero\n" and redo; 14 } 15 16 print "\nThe result is ", $numerator / $denominator, "\n"; This statement uses a logical OR expression with operator or to determine if the user input 0 for the denominator. If the value of $denominator is nonzero, the condition on the left of the or evaluates to true, the logical expression short circuits and the remainder of the statement is ignored. On the other hand, if $denominator is equal to zero, the condition on the left of the or evaluates to false, and Perl evaluates the right side of the or operator. Please enter a numerator: 7 Please enter a denominator: 3 The result is
22
Please enter a numerator: 22
Please enter a denominator: 0 Cannot divide by zero Please enter a denominator: 7 The result is
23
1 #!/usr/bin/perl 2 # Fig. 5.17: fig05_17.pl 3 # Using function 'die' to terminate a program. 4 5 print "Please enter a numerator: "; 6 chomp( $numerator = <STDIN> ); 7 8 print "Please enter a denominator: "; 9 chomp( $denominator = <STDIN> ); 10 11 # if condition is false, program prints message and terminates 12 $denominator != 0 or die "Cannot divide by zero"; 13 14 # executes only if $denominator is not 0 15 print "\nThe result is ", $numerator / $denominator, "\n"; Function die allows a programmer to terminate program execution and print a message, assisting in both debugging the program and writing clearer code. In this case, function die is executed if the user enters 0. Please enter a numerator: 7 Please enter a denominator: 3 The result is Please enter a numerator: 22 Please enter a denominator: 0 Cannot divide by zero at fig05_17.pl line 12, <STDIN> line 2.
24
Fig. 5.18 Single-entry/single-exit sequence and selection structures.
unless structure (single selection) if structure if/else structure (double selection) if/elsif/else structure (multiple selection) . Sequence Fig Single-entry/single-exit sequence and selection structures.
25
Fig. 5.19 Single-entry/single-exit repetition structures.
27
Fig. 5.21 Simplest flowchart.
28
. Rule 2 Fig Repeatedly applying rule 2 of Fig to the simplest flowchart.
29
Fig. 5.23 Applying rule 3 of Fig. 5.20 to the simplest flowchart.
30
Fig. 5.24 Stacked, nested and overlapped building blocks.
Stacked building blocks Nested building blocks Overlapping building blocks (Illegal in structured programs) Fig Stacked, nested and overlapped building blocks.
31
Fig. 5.25 An unstructured flowchart.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.