Visual Basic Flow Control

Revision as of 18:39, 7 August 2007 by Neil (Talk | contribs) (Using If ... Then to Make Decisions)

Revision as of 18:39, 7 August 2007 by Neil (Talk | contribs) (Using If ... Then to Make Decisions)

In Visual Basic Comparison and Logic we looked at how to use Visual Basic logical expressions to decide is something is True or False. Much of the art of programming involves writing code that makes decisions based on one or more criteria. Such decisions define which code gets executed and which code gets by-passed when the application is running. This is often referred to as flow control since it controls the flow of execution.

In previous chapters the If statement has been widely used in examples. In this chapter of Visual Basic Essentials we are going to cover the various forms of If statements such as If .. Then .. Else, together with other decision making code structures such as Select Case and GoTo

Using If ... Then to Make Decisions

The If ... Then construct is probably the most common decision making tool used by Visual Basic programmers. teh basic syntax of this decision structure is as follows:

If expression Then

.. Code to be Executed ..

Endif

Essentially if the expression evaluates to True (see Visual Basic Comparison and Logic for more details of this type of logic) then the code in the body of the statement is executed. If, on the other hand, the expression evaluates to False the code is skipped.

For example, if a decision needs to be made depending on whether one string matches another the following code might be used: