Branch Control Structure


© Swapna Kamat

Branching Control Statements

Visual Basic like other programming languages executes lines of code from the top of the program downwards. However, often a programmer does not wish all lines to be executed every time the program is run. Instead of writing several different programs for this job, branching and repeating commands can be used.

Branching commands are used to determine when to execute a small section of code and when not to run it. One example is the 'If' command. If something is true then the code will be run, if not it will not be executed and VB will continue further down the program.

Repeating commands are useful for running small sections of code several times. If a programmer needs to read 100 lines of text from a file he could use 100 lines of code, one for every read statement, or alternatively a 'For Next' loop could do a similar job using just three or four lines!

Branching Structures

This category includes decision statements like If-Then-Else and Case Select statements.

In Visual Basic, you can write an If...Then statement for handling True conditions in two ways: the single-line If...Then statement and the multiline If...Then statement. Each uses the If...Then keywords to check a condition. If the condition is True, the program runs the commands associated with the If...Then statement. If the condition is False, the commands are skipped.

Writing a Single-Line If Statement

The single-line If statement is used to perform a single task when the condition in the statement is True. The following is the syntax of the single-line If statement:

If condition Then command

The condition represents any type of statement or function that evaluates to True.

The condition can be any of the following:

  • A comparison of a variable to a literal, another variable, or a function
  • A variable that contains a True or False value
  • Any function that returns a True or False value

Nested if-then-else are used when a group of statements are to be executed when more than one conditions need to be satisfied. Multi-line if-then-else are used when many conditions are to be checked. When more than say 3 conditions are present, it is advisable to use Case Select instead of if-then-else.

The Select Case structure is similar to a series of if-then-else-if statements. The following lines of code show the syntax of the Select Case block:

Select Case stringOne
Case Val1
Statement_Group_1
Case Val2
Statement_Group_2
End Select
Case Select statement is used to eliminated Multiline if-then-else statements. They cannot replace nested structures.

We will study the looping structures in the next article

Go To Page: 1 2


The copyright of the article Branch Control Structure in Learning Visual Basic is owned by . Permission to republish Branch Control Structure in print or online must be granted by the author in writing.

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo