Chapter 5 Control Structures: Part (Freelance web design) 2 245 5.21

Chapter 5 Control Structures: Part 2 245 5.21 Modify the program you wrote in Exercise 5.20 to read an odd number in the range 1 to 19 to specify the number of rows in the diamond. Your program should then display a diamond of the appropriate size. 5.22 A criticism of the break statement and the continue statement is that each is unstructured. Actually, break statements and continuestatements can always be replaced by structured statements, although doing so can be awkward. Describe in general how you would remove any break statement from a loop in a program and replace that statement with some structured equivalent. [Hint: The break statement leaves a loop from within the body of the loop. The other way to leave is by failing the loop-continuation test. Consider using in the loop-continuation test a second test that indicates early exit because of a break condition. ] Use the technique you developed here to remove the breakstatement from the program in Fig. 5.11. 5.23 What does the following program segment do? for ( i = 1; i <= 5; i++ ) { for( j = 1; j <= 3; j++ ) { for ( k = 1; k <= 4; k++ ) System.out.print( ‘*’ ); System.out.println(); } System.out.println(); } 5.24 Describe in general how you would remove any continue statement from a loop in a program and replace that statement with some structured equivalent. Use the technique you developed here to remove the continue statement from the program in Fig. 5.12. 5.25 ( The Twelve Days of Christmas Song) Write an application that uses repetition and switchstructures to print the song The Twelve Days of Christmas. One switch structure should be used to print the day (i.e., First, Second, etc.). A separate switchstructure should be used to print the remainder of each verse. Visit the Web site www.12days.com/library/carols/ 12daysofxmas.htm for the complete lyrics to the song. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply