Chapter 5 - Control Structures: Part 2

Slides:



Advertisements
Similar presentations
1 JavaScript: Control Structures II. 2 whileCounter.html 1 2
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 – Control Structures Outline 3.1 Introduction 3.2 Algorithms 3.3 Pseudocode 3.4Control Structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 9 - JavaScript: Control Statements II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
Chapter 05 (Part III) Control Statements: Part II.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
Fortran: Control Structures Session Three ICoCSIS.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - JavaScript: Control Structures II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
CPTG286K Programming - Perl Chapter 9: Misc. Control Structures Chapter 10: Filehandles & File tests.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Chapter 9 - JavaScript: Control Statements II
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 3: Decisions and Loops
Chapter 5- Control Structures: Part 2
Python: Control Structures
Control Statements: Part 2
JavaScript: Control Statements I
Control Structures: Part 2
Chapter 4 - Program Control
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
CSC113: Computer Programming (Theory = 03, Lab = 01)
Ch 7: JavaScript Control Statements I.
JavaScript: Control Statements.
JavaScript: Control Statements II
Arithmetic operations, decisions and looping
Outline Altering flow of control Boolean expressions
7 Arrays.
Chapter 8 JavaScript: Control Statements, Part 2
Structured Program
Chapter 4 - Program Control
Control Structures: for & while Loops
3 Control Statements:.
Chapter 3 – Control Structures
Chapter 6 Control Statements: Part 2
2.6 The if/else Selection Structure
Chapter 3: Selection Structures: Making Decisions
Chapter 5 – Control Structures: Part 2
Topics Introduction to Repetition Structures
Chapter 4 - Program Control
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

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

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 5 @array = ( 1 .. 10 ); # create array containing 1-10 6 7 foreach $number ( @array ) { # for each element in @array 8 $number **= 2; # square the value 9 } 10 11 print "@array\n"; # display the results Integers 1 through 10 are stored in @array 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 in @array that the control variable is currently representing. The value of variable $number is raised to the second power using the exponentiation operator. 1 4 9 16 25 36 49 64 81 100

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 ( 1 .. 10 ) { 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.

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 0 5 10 15 20

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

1 #!/usr/bin/perl 2 # Fig. 5.4: fig05_04.pl 3 # Demonstrating map and grep. 4 5 @numbers = ( 1 .. 10 ); 6 print "\@numbers: @numbers\n\n"; 7 8 # locate all numbers less than 6 9 foreach ( @numbers ) { 10 push( @smallNumbers, $_ ) if $_ < 6; 11 } 12 13 print "Numbers less than 6:\n", 14 "foreach: @smallNumbers\n"; 15 16 @smallNumbers2 = grep( $_ < 6, @numbers ); 17 print "grep: @smallNumbers2\n\n"; 18 19 # multiply every number by 2 20 foreach ( @numbers ) { 21 push( @doubledNumbers, $_ * 2 ); 22 } 23 24 print "Double each number:\n", 25 "foreach: @doubledNumbers\n"; 26 27 @doubledNumbers2 = map( $_ * 2, @numbers ); 28 print "map: @doubledNumbers2\n"; This foreach repetition structure is equivalent to the grep function call. The call to grep specifies that each element of @numbers that is less than 6 should be part of the resulting list (which is assigned to @smallNumbers2). 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 of @numbers should be multiplied by 2 and then placed in the resulting list (which is assigned to @doubleNumbers2). This foreach repetition structure is equivalent to the map function call.

@numbers: 1 2 3 4 5 6 7 8 9 10   Numbers less than 6: foreach: 1 2 3 4 5 grep: 1 2 3 4 5 Double each number: foreach: 2 4 6 8 10 12 14 16 18 20 map: 2 4 6 8 10 12 14 16 18 20

1 #!/usr/bin/perl 2 # Fig. 5.5: fig05_05.pl 3 # Survey data analysis: Determining the mean, median and mode. 4 5 @opinions = ( 8, 9, 4, 7, 8, 5, 6, 4, 9, 9, 6 7, 8, 9, 5, 4, 8, 7, 8, 7, 7, 7 6, 6, 8, 9, 1, 9, 8, 7, 8, 7, 8 7, 8, 9, 8, 9, 4, 9, 6, 8, 4, 9 6, 7, 3, 4, 8, 7, 9, 8, 9, 2 ); 10 11 # determine the mean 12 $total = 0; 13 14 foreach ( @opinions ) { 15 $total += $_; 16 } 17 18 $mean = $total / @opinions; 19 print "Survey mean result: $mean\n"; 20 21 # determine the median 22 @sorted = sort { $a <=> $b } @opinions; 23 $middle = @sorted / 2; # middle element subscript 24 The mean is computed by totaling the 50 elements in @opinions and dividing the result by @opinions, 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.

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 ( @sorted %2 == 0 ) { # even number of elements 29 $median = 30 ( $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 ( @opinions ) { 42 ++$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 ] ) { 47 $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 in @opinions. 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 in @frequency 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.

53 # display a frequency graph 54 print "Response\tFrequency\n"; 55 print "--------\t---------\n"; 56 57 foreach ( 1 .. 9 ) { 58 print "$_\t\t", "*" x $frequency[ $_ ], "\n"; 59 } Survey mean result: 6.86 Survey median result: 7 Survey mode result: 8   Response Frequency -------- --------- 1 * 2 * 3 * 4 ****** 5 ** 6 ***** 7 ********** 8 ************* 9 ***********

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 5 @opinions = qw( what word is being used most in this array is 6 what this is used what most is is array what 7 word used is most is array what is this is array 8 what is is array this is most ); 9 10 foreach ( @opinions ) { 11 ++$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 ).

Word Frequency ---- --------- array ***** being * in * is ************ most **** this **** used *** what ****** word **  

1 #!/usr/bin/perl 2 # Fig. 5.7: fig05_07.pl 3 # Using the next statement in a foreach structure. 4 5 foreach ( 1 .. 10 ) { 6 7 if ( $_ == 5 ) { 8 $skipped = $_; # store skipped value 9 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. 1 2 3 4 6 7 8 9 10   Used 'next' to skip the value 5.

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 ( 1 .. 10 ) { 6 7 if ( $_ == 5 ) { 8 $number = $_; # store current value before loop ends 9 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. 1 2 3 4   Used 'last' to terminate loop at 5.

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 ) { 10 print "$number "; 11 ++$number; 12 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. 1 2 3 4 5 6 7 8 9 10 Stopped when $number became 11.

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 ) { 6 next LOOP if ( $number % 2 == 0 ); 7 print "$number "; # displays only odd numbers 8 } A for structure with a label. When next executes, it forces the next iteration of the block labeled LOOP. 1 3 5 7 9

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 ( 1 .. 10 ) { 6 7 INNER: foreach $column ( 1 .. 10 ) { 8 9 if ( $row < $column ) { 10 print "\n"; 11 next OUTER; 12 } 13 14 print "$column "; 15 } 16 } When this statement executes, loop control continues with the OUTER block, ignoring the remaining iterations of INNER. 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10

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 ) { 10 print "Right!\n"; 11 last BLOCK; # jump to end of BLOCK 12 } 13 14 if ( $guess == 2 ) { 15 print "Close!\n"; 16 last BLOCK; # jump to end of BLOCK 17 } 18 19 if ( $guess == 3 ) { 20 print "Wrong!\n"; 21 last BLOCK; # jump to end of BLOCK 22 } 23 24 # default case; executes only if $guess is not 1, 2 or 3 25 { 26 print "Please re-enter your guess (1-3): "; 27 $guess = <STDIN>; 28 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.

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!

Fig. 5.13 Truth table for the && (logical AND) operator. expression1 expression2 && false true True Fig. 5.13 Truth table for the && (logical AND) operator. expression1 expression2 || false F alse T rue true Fig. 5.14 Truth table for the || (logical OR) operator. Fig. 5.15 Truth table for operator ! (logical negation).

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 13 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 2.33333333333333

Please enter a numerator: 22 Please enter a denominator: 0 Cannot divide by zero Please enter a denominator: 7   The result is 3.14285714285714

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 2.33333333333333 Please enter a numerator: 22 Please enter a denominator: 0 Cannot divide by zero at fig05_17.pl line 12, <STDIN> line 2.

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. 5.18 Single-entry/single-exit sequence and selection structures.

Fig. 5.19 Single-entry/single-exit repetition structures.

Fig. 5.21 Simplest flowchart.

. Rule 2 Fig. 5.22 Repeatedly applying rule 2 of Fig. 5.20 to the simplest flowchart.

Fig. 5.23 Applying rule 3 of Fig. 5.20 to the simplest flowchart.

Fig. 5.24 Stacked, nested and overlapped building blocks. Stacked building blocks Nested building blocks Overlapping building blocks (Illegal in structured programs) Fig. 5.24 Stacked, nested and overlapped building blocks.

Fig. 5.25 An unstructured flowchart.