Space web hosting - Chapter 5 Control Structures: Part 2 205 Note

Chapter 5 Control Structures: Part 2 205 Note that, besides small circles and arrows, the flowchart contains only rectangle symbols and a diamond symbol. The programmer fills the rectangles and diamonds with actions and decisions appropriate to the algorithm. 5.4 Examples Using the forStructure The examples given next show methods of varying the control variable in a forstructure. In each case, we write the appropriate for structure header. Note the change in the relational operator for loops that decrement the control variable. a) Vary the control variable from 1to 100in increments of 1. for ( int i = 1; i <= 100; i++ ) b) Vary the control variable from 100 to 1in increments of -1(i.e., decrements of 1). for ( int i = 100; i >= 1; i– ) c) Vary the control variable from 7to 77in steps of 7. for ( int i = 7; i <= 77; i += 7 ) d) Vary the control variable from 20to 2in steps of -2. for ( int i = 20; i >= 2; i -= 2 ) e) Vary the control variable over the following sequence of values: 2, 5, 8, 11, 14, 17, 20. for ( int j = 2; j <= 20; j += 3 ) f) Vary the control variable over the following sequence of values: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0. for ( int j = 99; j >= 0; j -= 11 ) counter <= 10 g.drawLine( 10, 10, 250, counter * 10 ); true false int counter = 1 counter++ Establish initial value of control variable. Determine if final value of control variable has been reached. Body of loop (this may be many statements) Increment the control variable. Fig. 5.4Flowcharting a typical forrepetition structure. Fig. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply