Visual Basic .NET/Branch statements

A branch statement, or conditional statement, is code that allows you to test whether statements are true or false and then execute some code based on these comparisons.

The Branch Statements

 * 1) If/Endif
 * 2) If/Else/Endif
 * 3) If/ElseIf/Else/Endif
 * 4) Select Case/End Select

Statement
below an example of simple if statement:

Statement
Refer to these lines for the examples below:

The actual format of the IF statement is:

The IF statement ALWAYS tests the left condition or statement for a TRUE result: ie: is iJunk1 = 4 ........ TRUE ie: is iJunk1 = 5 ........ FALSE ie: is iJunk1 > 7 ........ TRUE ie: is iJunk1 < iJunk2 ... TRUE

If the condition or statement is true, the IF statement continues to process the top or next portion of the statement using the THEN operator.

If the THEN operation is a single statement, it can be written on the same line as the test condition:

If the THEN operation requires two or more statements you will find that the entire IF statement will be MUCH easier to read when written with each statement on its own line: As shown above, the IF statement does not require and can be used without the ELSE clause.

When your IF statement needs to process both a True and a False condition the use of the ELSE clause is required:

It is sometimes easier to read the condition or statement when you place it within paren's:

Statement
If/ElseIf statement is used to conditionally execute code based on more than one test condition or statement. If the condition provided in the If statement evaluates to true, the code in the block is executed. Otherwise, execution would proceed to the ElseIf condition or statement, or on to the Else or EndIf statement.

The ElseIf and Else clauses are not required parts of an If statement.

An example of the If/ElseIf/Else branch statement is:

Statement
Either strings or numbers can be used for a Select Case statement.

Select Case statements are usually used to avoid long chains of If/ElseIf/.../ElseIf/Else statements.

An example of a Select

Boolean Operators
Boolean operators in Visual Basic .NET now accommodate for short circuit boolean evaluation, most other languages always apply short circuit boolean evaluation by default (usually, this can be turned off with a compiler option), consider the following boolean statement:

functionA and functionB

With this statement, when short-circuit boolean evaluation is used, the second function will only be called if the first function returns true, this is because if functionA returns false it becomes irrelevant to the outcome of the statement whether functionB returns true or not.

However, when no short-circuit boolean evaluation is used, both of the functions will be called irrespective of whether the first part of the statement returns true or false.

Something to note with short-circuit boolean evaluation is that the order of the parameters can become important when it is used.

Due to previous versions of Visual Basic not having short circuit boolean evaluation, Microsoft has decided to preserve backward compatibility and add two new boolean logic identifiers which support short-circuit boolean evaluation, so in addition to the standard boolean operators:

Not And Or Xor

There are also two new operators which function using short-circuit boolean evaluation, and they are:

AndAlso OrElse

Visual Basic .NET/Conditions