Section 5.12. Nested Repetition Statements


5.12. Nested Repetition Statements

Let us present another complete example. We use nested repetition statements to solve the problem. Consider the following problem statement:

Write a program that draws in the command prompt a filled square consisting solely of one type of character, such as the asterisk (*). The side of the square and the character to be used to fill the square should be entered by the user. The length of the side should not exceed 20 characters. If the user enters a value over 20, the message "Side is too large" should be printed.

Your program should draw the square as follows:

  1. Input the side of the square.

  2. Validate that the side is less than or equal to 20.

  3. Use repetition to draw the square by printing only one fill character at a time.

After reading the problem statement, we make the following observations:

  1. The program must draw side rows, each containing side fill characters, where side is the value entered by the user. Counter-controlled repetition should be used.

  2. A test must be employed to ensure that the value of side is less than or equal to 20. If it is not, the message"Side is too large" should be printed.

  3. Four variables should be usedone that represents the length of the side of the square, one that represents (as a String) the fill character to be used, one that represents the row in which the next symbol should appear and one that represents the column in which the next symbol should appear.

The Visual Basic class and test program that solve this problem are shown in Fig. 5.17 and 5.18.

Figure 5.17. Nested repetition statements used to print a square of symbols.

  1  ' Fig. 5.17: Box.vb  2  ' Class can be used to draw a square of a specified length, using  3  ' a specified fill character.  4  Public Class Box  5     Private sideValue As Integer ' length of side of square  6     Private fillCharacterValue As String ' character used to draw square  7  8     ' property provides access to side length of box  9     Public Property Side() As Integer 10        Get 11           Return sideValue ' return side length 12        End Get 13 14        Set(ByVal value As Integer) 15           sideValue = value ' modify side length 16        End Set 17     End Property ' Side 18 19     ' property provides access to fill character for drawing box 20     Public Property FillCharacter() As String 21        Get 22           Return fillCharacterValue ' return fill character 23        End Get 24 25        Set(ByVal value As String) 26           fillCharacterValue = value ' modify fill character 27        End Set 28     End Property ' FillCharacter 29 30     ' display box 31     Public Sub Display() 32        Dim row As Integer ' current row 33        Dim column As Integer ' current column 34 35        If Side <= 20 Then ' if true, then print the box 36           row = 1 37 38           ' this While is nested inside the If in lines 35-55         39           While row <= Side ' controls row being printed              40              column = 1 ' prepare to print first character in the row 41            42              ' this loop prints one row of the square                 43              ' and is nested inside the While in lines 39-52          44              While column <= Side                                     45                 ' print fill character and a space                    46                 Console.Write(FillCharacter & " " )                   47                 column += 1 ' increment column                        48              End While                                                49            50              Console.WriteLine() ' position cursor to next line       51              row += 1 ' increment row                                 52           End While                                                   53        Else ' condition (Side <= 20) is false 54           Console.WriteLine("Side too large") 55        End If 56     End Sub ' Display 57  End Class ' Box 

Figure 5.18. Using class Box to draw a square.

  1  ' Fig. 5.18: BoxTest.vb  2  ' Program draws square by creating an object of class Box.  3  Module BoxTest  4     ' Main begins program execution  5     Sub Main()  6        Dim box As New Box()  7  8        ' obtain fill character and side length from user   9        Console.Write("Enter fill character: ") 10        box.FillCharacter = Console.ReadLine() 11        Console.Write("Enter side length (must be 20 or less): ") 12        box.Side = Console.ReadLine() 13 14        box.Display() ' display box 15     End Sub ' Main 16  End Module ' BoxTest 

 Enter fill character: # Enter side length (must be 20 or less): 8 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 



 Enter fill character: * Enter side length (must be 20 or less): 5 * * * * * * * * * * * * * * * * * * * * * * * * * 



 Enter fill character: $ Enter side length (must be 20 or less): 37 Side too large 





Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net