Wednesday, January 4, 2012

Conditional and Loop Statements

Visual Basic .NET supports all the conditional and loop statements supported by its predecessors—that is, the If and Select conditional blocks and the For, Do, and While loop statements. Nevertheless, Visual Basic .NET offers some new features in this area as well, and Visual Basic .NET 2003 adds a new way to declare the controlling variable in For and For Each loops.



Short-Circuit Evaluation with AndAlso and OrElse Operators

Short-circuit evaluation allows you to avoid the unnecessary evaluation of Boolean subexpressions if they wouldn’t affect the value of the main expression. Let’s see a simple example:
CODES:

If the n1 variable is 0 or negative, the entire expression can only be False, whether the subexpression following the And operator evaluates to True or False. Previous Visual Basic versions always evaluate the entire If expression, so they incur an unnecessary performance hit. Visual Basic .NET lets you produce smarter code using the new AndAlso and OrElse operators, which enforce short-circuit evaluation:
CODES:
This expression is equivalent to the following, more verbose, code:
CODES:
You can have short-circuit evaluation in situations in which you use the Or operator:
CODES:
In this case, if the n1 variable is greater than 0, the entire expression is surely True, so evaluating the second subexpression Log(n2) can be sidestepped. You can enforce this smarter behavior with the new OrElse operator:
CODES:
These new operators also work inside complex Boolean expressions: CODES:

Short-circuit evaluation helps you avoid many run-time errors without writing much code. For example, you can use the following approach to read an array element only if the index is in the valid range: 
CODES:

Here’s another example:
CODES:

The AndAlso operator lets you avoid errors when you check the property of an object variable that might be Nothing: CODES:


Short-circuit evaluation can speed up your applications, but you must account for subtle bugs that might slip into your code. This is especially true when the subexpression contains user-defined functions that can alter the program’s behavior. Consider this code: CODES:

Unless you’re familiar with short-circuit evaluation—which might be the case if you’re a C/C++ or Java developer—you might not immediately realize that the n2 variable is incremented only if the n1 variable is 0. You can make your code more readable by using nested If statements—in other words, by writing what you might call manual short-circuiting code:
 CODES:

The And and Or operators work as they do in Visual Basic 6 and perform bitwise operations rather than truly Boolean operations. You can use them for bit-manipulation code as you’ve always done: CODES:

For Next and For Each Loops
Visual Basic .NET supports For, For Each, and Do loops, and they support exactly the same syntax as under previous versions of the language. The most frequently used looping structure in Visual Basic is undoubtedly the For Next loop: CODES:
You need to specify the Step clause only if an increment is different from 1. You can exit the loop using an Exit For statement, but unfortunately Visual Basic doesn’t provide a sort of “Continue” command that lets you skip the remaining part of the current iteration. (This command is available in C#.) The best you can do is use (nested) If statements or use a plain Goto keyword that points to the end of the loop. In fact, this

occasion is one of the few when a single Goto statement can make your code more readable and aintainable. Visual Basic .NET 2003 introduces a new syntax that allows you to declare the controlling variable inside the loop:
CODES:
This new syntax doesn’t add any groundbreaking possibility, but it is preferred because it prevents you from reusing the value in the variable when the loop exits. You can be glad that you can’t reuse the value because most of the time you can’t make any correct assumptions about the value in the variable at the end of the loop (which might have been exited because of an Exit For or a Goto statement). For this reason, I will use this syntax in the remainder of this book, even though you can easily fix the code so that it runs under the first edition of Visual Basic .NET simply by declaring the controlling variable in a separate Dim statement.

Always use integer variables as the controlling variable of a For…Next loop. They’re always faster than Single, Double, or Decimal controlling variables by a factor of 10 times or more (or even 100, in the case of Decimal control variables). In addition, because of rounding errors, you cannot be completely sure that a floating-point variable is incremented correctly when the increment is a fractional quantity, and you might end up with fewer or more iterations than expected:
CODES:

If you need to increment a floating-point quantity, the safest and most efficient technique is shown in the following code snippet: CODES:

The For Each loop lets you visit all the elements of an array or a collection. The controlling variable can be of any type:
CODES:

If you are working under Visual Basic .NET 2003, you can also declare the controlling variable inside the loop itself:
CODES:


When you’re working with arrays, regular For…Next loops are usually faster and preferable to For Each…Next loops, whereas when you are working with collections, the latter type of loop is usually faster. When coupled with the ability to create an array on the fly, the For Each loop allows you to execute a block of statements with different values for a controlling variable, which don’t need to be in sequence: CODES:

The Do…Loop structure is more flexible than the For…Next loop in that you can place the termination test either at the beginning or at the end of the loop. (In the latter case, the loop is always executed at least once.) You can use either the While clause (repeat while the test condition is true) or the Until clause (repeat while the test condition is false). You can exit a Do loop at any moment by executing an Exit Do statement, but— as with For…Next loops—Visual Basic doesn’t offer a Continue keyword that skips over the remaining statements in the loop and immediately restarts the loop:
CODES:

No comments:

Post a Comment