Appendix E : Control Statements


Control statements tell an application which other statements to execute under a particular set of circumstances. They control the path that execution takes through the code. They include statements that tell the program to execute some statements but not others and to execute certain statements repeatedly.

The two main categories of control statements are decision statements and looping statements. The following sections describe the decision and looping statements provided by Visual Basic .NET.

Decision Statements

A decision statement represents a branch in the program. It marks a place where the program can execute one set of statements or another or possibly no statements at all. These include If, Choose, and Select Case statements.

Single-Line If Then

A single-line If Then statement tests a condition and, if the condition is true, executes a piece of code. The code may include more than one simple statement separated by a colon.

Optional Else If clauses let the program evaluate other conditions and execute corresponding pieces of code. A final optional Else clause lets the program execute a piece of code if none of the previous conditions is true.

The syntax is as follows:

  If condition Then statement If condition Then statement1 Else statement2 If condition1 Then statement1 Else If condition2 Then statement2 Else statement3 If condition Then statement1 : statement2 If condition Then statement1 : statement2 Else statement3 : statement4 

Complicated single-line If Then statements can be confusing and difficult to read, so I recommend using the multiline versions if the statement includes an Else clause or executes more than one statement.

Multiline If Then

A multiline If Then statement is similar to the single-line version, except the pieces of code executed by each part of the statement can include multiple lines. Each piece of code ends before the following ElseIf, Else, or End If keywords. In complex code, this format is often easier to read than a complicated single-line If Then statement.

The syntax is:

  If condition1 Then     statements1... ElseIf condition2     statements2... Else     statements3... End If 

The statement can contain any number of ElseIf sections.

Select Case

A Select Case statement lets a program execute one of several pieces of code based on a test value. Select Case is equivalent to a long If Then Else statement.

The syntax is as follows:

  Select Case test_value     Case comparison_expression1         statements1     Case comparison_expression2         statements2     Case comparison_expression3         statements3     ...     Case Else         else_statements End Select 

A comparison expression can contain multiple expressions separated by commas, can use the To keyword to specify a range of values, and can use the Is keyword to evaluate a logical expression using the test value. The following example’s first case looks for a string in the range “A” to “Z” or “a” to “z.” Its second and third cases look for values less than “A” and greater than “Z,” respectively.

  Select Case key_pressed     Case "A" To "Z", "a" To "z"         ...     Case Is < "A"         ...     Case Is > "Z"         ... End Select 

Many developers always include a Case Else section to catch unexpected situations. If every possible situation should be covered by other cases, some developers throw an exception inside the Case Else section to make it easier to find errors.

IIf

IIf takes a Boolean value as its first parameter. It returns its second parameter if the value is true, and it returns its third parameter if the value is false.

The syntax is as follows:

  variable = IIf(condition, value_if_false, value_if_true) 

IIf is often confusing and is slower than an If Then Else statement, so you should usually use If Then Else instead.

Choose

Choose takes an index value as its first parameter and returns the corresponding one of its other parameters.

The syntax is as follows:

  variable = Choose(index, value1, value2, value3, value4, ...) 

Choose is rarely used by many programmers, so it can be confusing. To avoid unnecessary confusion, you may want to use a Select Case statement instead.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net