Wyo VB Lecture Notes
Objective #1: Use While loops.
- A While loop can be used to execute a block of code many times over and over. The loop below causes a message box to appear 3 times.
intNum = 0
While (intNum <= 3)
MessageBox.Show("Hello World")
intNum = intNum + 1
End While
In the While loop above (intNum <= 3) is a control
expression. The statements
MessageBox.Show("Hello World")
intNum = intNum + 1
are considered the body of this loop since they are indented and placed between the While and the End While.
Visual Basic evaluates the control expression to see if it is True or False. If the control
expression is True then VB executes the indented statements in the body of the loop. If the control expression is False, VB skips the while loop and does not execute the statements inside the body of the loop. The body of the loop executes over and over again until the control expression becomes false. Each time a looIt is usually considered a logic error if the control expression never becomes false since the loop would execute forever. A loop that executes forever is called an infinite loop. Notice that the example above would be an infinite loop if the statement intNum = intNum + 1 was removed.
- For good style, you should indent the statements inside the body of the loop and you should place parentheses around the control expression for readability. You should always surround your loops with blank lines in your code. This makes your program easier to read and debug. That is, above all While and For loops
you should include a blank line and below all End While and Next statements, you should include a blank line. Blank lines used to make code easier to read are called white
space. Tabs and blank spaces that are also used for the same purpose of making white space.
- A While loop should be used instead of a For loop
when you do not know the exact number of iterations that the loop must
make. If there is a situation in which some process must occur
over and over again within a loop but you do not know when exactly it will
end then you should use a While loop.
For example, if you ask the user to input his test scores but your program
does not necessarily know how many tests he has taken you could use a While loop and simply tell the user to enter
the number -1 to end his list of input values. Of course, we know that
-1 will not be confused with his actual exam scores since it is impossible
to earn a -1 on an exam. In this case, -1 is called the sentinel value that
warns the loop that the inputs have ended just like Paul Revere was a sentinel
that warned the Americans that the British were coming.
Objective #2: Use flag variables to control While loops.
- A flag variable is
often used to store the value 1 (meaning True) or 0 (meaning False). The
programmer uses the flag variable as a way of controlling some process
in an algorithm. Flag variables are often used in While loops to send a signal
to the control expression. They are not used in For loops since a For loop executes a predetemined, definite number of times.
For example, in the code segment below intGameIsOver is
used as a flag variable. It sends a signal to the control expression of
the loop if the user has scored enough points to end the game and exit
the loop. intGameIsOver is
initially set to the value 0 which indicates False (the game is NOT over).
You should name flag variables appropriately. The variable should be named
in a way that it's name indicates a True condition. (For example, in the
segment below, you should not use a flag variable with the name, intGameIsNotOver,
since it would probably make the logic of the algorithm harder for most
fellow programmers to follow.)
intGameIsOver = 0
While (intGameIsOver = 0)
' a bunch of statements would be located here
intUserPointsScored = intUserPointsScored + intLastRoundScore
If (UserPointsScored > 1000) Then
intGameIsOver = 1
End If
End While
Often programmers use variables with the Boolean data
type as flag variables. A Boolean variable
can be set to either True or False.
Here is an example:
Dim
blnGameIsOver As Boolean = False
While (blnGameIsOver = False)
' a bunch of statements
would be located here
intUserPointsScored
= intUserPointsScored + intLastRoundScore
If (intUserPointsScored > 1000)
Then
blnGameIsOver
= True
End If
End While
By using a Boolean variable
you can simplify the first line of the loop like
this
While
(Not blnGameIsOver)
Here's another example of a While loop that uses a flag variable. In this case, the flag variable has the Boolean data
type.
Dim blnOutOfBounds As Boolean = False
While (blnOutOfBounds = False)
picPlayer.Left = picPlayer.Left + 10
If (picPlayer.Left > 300) Then
blnOutOfBounds = True
End If
End While