Review Questions


What is the purpose of program loops ?


Program loops are used to repeat a series of program statements multiple times.


What are ill-behaved and well-behaved loops?


A well-behaved loop repeats the series of program instructions in a predictable manner and has a condition that will eventually terminate the loop. An ill-behaved manner does not process the loops statements in a predictable manner and often results in an infinite, never-ending loop.


What are the conditions that result in a well-behaved loop?


There's a minimum of three conditions. First, one or more variables must be initialized to a starting loop value. Second, there must be a test expression that determines whether another pass should be made through the loop body statements. The outcome of the test expression must have the possibility of terminating the loop and is normally determined by a loop control variable. Third, the variable that controls the loop must have the chance to change its state during program execution.


Write a program that displays a number, its square, and its cube for the values from 0 to 25 in the debug output window.


The program could be written as

 Dim i As Integer  For i = 0 To 25  System.Console.Write(CStr(i) & " " & CStr(i * i) & _             " " & CStr(i ^ 3) & vbCrLf) Next i 

The program uses a For loop to generate the data requested . Notice that it possesses the three necessary conditions for a well-behaved loop. Always remember that For loops run inclusively for the terminating value of the loop. In our example, that means the values for 25 are displayed.


What is the difference between a pre-test and a post-test loop?


A pre-test loop has the test expression that determines whether the loop should be executed at the start of the loop structure. A post-test loop has the test expression at the bottom of the loop structure. Because the test expression in a pre-test loop is before the statements in the loop body, it's possible that the statements in the loop body will never be executed. However, because the test expression in a post-test loop is at the end of the loop structure, the statements of the loop body are always executed at least one time.


What kind of testing (pre-test or post-test) is done with a For loop and a Do - While loop?


The For loop is a pre-test loop. A Do - While loop can be written as either a pre-test or post-test loop.


Assume that you're writing an automated fire alarm system. There are several hundred fire and smoke sensors scattered throughout a building and each sensor is polled every few seconds by your program. The first alarm in the list is assigned the value StartingAlarmNumber and the last one is named EndingAlarmNumber . The alarm currently being polled is determined by a variable named CurrentAlarmToTest . The alarms are numbered sequentially and are integer values. The function responsible for polling the sensors is named PollAlarms() and uses CurrentAlarmToTest as its argument. If everything is normal, the function returns logic False after each sensor is polled and it proceeds to the next alarm. If there's a fire, the function returns the number of the sensor and the program must calls the fire depart using a function named CallFireDepartment() with CurrentAlarmToTest as its argument. What would the code look like? (You can assume that the code for PollAlarms() and CallFireDepartment() already exists in the namespace.)


Although there are many variations, one solution might be

 CurrentAlarmToTest = StartingAlarmNumber   ' Initialize to first alarm  Do  If PollAlarms(CurrentAlarmToTest) Then   ' Start checking them   Exit Do                  ' If there's a fire, get out  End If  CurrentAlarmToTest += 1           ' No fire, look at next alarm  If CurrentAlarmToTest > EndingAlarmNumber Then ' Start over?   CurrentAlarmToTest = StartingAlarmNumber ' Yep  End If Loop While True CallFireDepartment(CurrentAlarmToTest)     ' Call for help 

A Do-While loop with a post-test is used. This means we'll always enter the loop at least once. The If test calls the PollAlarms() function and returns False if there is no fire. If there is a fire, the return value from PollAlarms() is non-zero and the Exit Do statement is executed, which sends control to CallFireDepartment() . If there is no fire, CurrentAlarmToTest is incremented and tested to see whether we're at the end of the alarm list. If we are at the end, CurrentAlarmToTest is reset to its starting value. Because the loop test is set to logic True , we've purposely written an infinite loop, so another pass is made through the loop. The program should stay in this loop until there is a fire.


Suppose that you have a U.S. stamp collection and you want to track the stamps you have using the Scott number of the stamp (this is a standard classification numbering system used by collector and should be a string), the date you bought the stamp, how much you paid for it, and a brief description. Right now, you're just starting to collect, so you have only 20 stamps. How would you organize the data? (You don't have to set up the text boxes or the like ”just show how you would organize the data.)


You might use

 Structure MyStamps   Dim Scott As String  Dim PurchaseDate As Date  Dim Cost As Double  Dim Description As String End Structure 

to organize the data into a Structure type. This declaration must appear outside of any procedure call. Because you have 20 stamps, you'll need 20 variables of this structure type. Therefore, elsewhere in the code you would write

 Dim MyCollection(20) As MyStamps 

You would now have 20 such structures that you could fill in with the data about each stamp. In Chapter 23, "Disk Data Files," and Chapter 25, "Database Programming with Visual Basic .NET," you'll learn how you can write such data to a data file or a database.


How can you exit from a nested For loop?


The problem is that the Exit For statement transfers control out of only the current For loop. This still leaves you in the outer For loop. For example:

 Dim i, j, foundit, Goal, ThisOne As Integer  ' Some code that sets our Goal For i = 1 To 100  For j = 1 To 100   ' code that sets ThisOne   If ThisOne = Goal Then    Exit For   End If  Next j  ' The Exit For puts us here Next i 

In the code, if we find what we're looking for, the Exit For statement transfers us to the point of the last comment in the code fragment. However, we want to end both loops once we found what we are looking for. In other words, we want to be at the statement following Next i , not Next j .

To do this, modify the code as follows :

 foundit = 0  For i = 1 To 100  For j = 1 To 100   ' code that sets ThisOne   If ThisOne = Goal Then    foundit = 1       ' Set our success flag    Exit For   End If  Next j  ' The Exit For puts us here  If foundit = 1 Then     ' Did we have success?   Exit For          ' Yep, so get outta Dodge  End If Next i 

The code uses what is called a flag variable to set a flag when some desired state is reached. In the preceding code, foundit is a flag variable that's initialized to . If we find what we're looking for, foundit is set to 1 and the If test after the Next j statement causes the second Exit For to be executed and we transfer control out of the nested loops. You should be able to convince yourself that program control will stay in the loops through all passes or until foundit is set to 1 .

Visual Basic .NET. Primer Plus
Visual Basic .NET Primer Plus
ISBN: 0672324857
EAN: 2147483647
Year: 2003
Pages: 238
Authors: Jack Purdum

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: