Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Chapter 2: Visual Basic Programming Concepts
Last change: February 13 2016 18:48:07.
<<PreviousNext>>

Statements That Direct Flow of Execution Control

From time to time, you will encounter situations in which you need to change the sequence of execution in your code. For example, you may need to execute a block of code only if a certain condition is true; you may need to repeat the execution of a block of code for a certain number of times; or you may want to leave (jump away from) a block of code when your program reaches a certain point of execution.

Conditional Execution

If you need to execute a block of code that depends on a certain condition, you may need to use the If block. Its syntax appears as follows:

If Condition Then Statements to be executed when Condition is True Else Statements to be executed when Condition is False End If

where Condition is an expression that can be evaluated to either True or False.
For example, suppose you want to set the BackColor property of the label lblWelcome to either blue or green, depending on the value of the random number given by the Rnd function. You can code the following:

If Rnd() <.5 Then lblWelcome.BackColor = Color.Blue Else lblWelcome.BackColor = Color.Green End If

Rnd is a random number generator that returns a fractional number in the range of 0 and 1. Color.Blue and Color.Green are named constants that VB recognizes as values for the blue and green colors. The preceding code block will set the background color of the label to blue if the random number has a value less than .5; otherwise, it will set it to green. There are other code structures used to handle execution of different statements based on certain conditions. Chapter 5, “Decision,” discusses these in more detail.

Repetition

In many other situations, you may need to execute a block of code repetitively. One way to handle this is to use the For . . . Next loop. The code syntax for this structure is as follows:

For Counter = StartingValue To EndingValue Statements to be repeated Next Counter

Suppose you need to write 10 numbers, 1 through 10, in the immediate window. You can use this structure in the following manner:

For Counter = 1 To 10 Console.WriteLine(Counter) Next Counter

When this block of code is executed, Counter will start with a value of 1. The statement inside the For block (that is, Console.WriteLine(Counter)) is then executed, resulting in the number 1 being displayed in the immediate window. The next statement, Next Counter, will send the execution control back to the For statement. Counter is then increased by 1 to 2, which is in turn displayed when the WriteLine statement is executed. The loop will continue until Counter is greater than 10. The execution control is then transferred to the statement immediately below the block. Other variations and ways of coding repetitions are discussed in detail in Chapter 7, “Repetition.”

Code That Jumps

There are statements that can jump (skip) the remainder of code in a block or a procedure. These statements include GoTo, (which results in execution flow that is hard to trace and should be avoided by all means,) Exit Do, Exit For, Exit Sub, and Exit Function. When there is a need to use each of these statements, their uses will be explained. Figure 2-15 shows an event procedure that contains various code structures discussed previously.

Figure 2-15
A sample procedure with various code structure

Last change: February 13 2016 18:48:06.
<<PreviousNext>>