Sunday, December 18, 2011

Visual Basic 2008 Tutorial

Lesson 9: Controlling Program Flow 
 
In the previous lessons, we have learned how to program code that accept input from the users and display the output without controlling the program flow. In this chapter, you will learn how to write VB2008 code that can make decision when it process input from the users, and control the program flow in the process. Decision making process is an important part of programming because it will help solve practical problems intelligently so that it can provide useful output or feedback to the user. For example, we can write a VB2008 program that can ask the computer to perform certain task until a certain condition is met, or a program that will reject non-numeric data. In order to control the program flow and to make decisions, we need to use the conditional operators and the logical operators together with the If control structure.

9.1  Conditional Operators
 The conditional operators are powerful tools that resemble mathematical  operators . These operators allow a VB2008 program to compare data values and then decide what actions to take, whether to execute a program or terminate the program and more. They are also known as numerical comparison operators. Normally they are used to compare two values to see whether they are equal or one value is  greater or less than the other value. The comparison will return a true or false result. These operators are shown in Table 9.1. 
able 9.1: Conditional Operators


Operator
Meaning

=
Equal to

>
More than

<
Less Than

>=
More than and equal

<=
Less than and equal

<>
Not Equal to


9.2  Logical Operators

 Sometimes we might need to make more than one comparisons before a decision can be made and an action taken. In this case, using numerical comparison operators alone is not sufficient, we need to use additional operators, and they are the logical operators. These logical operators  are shown in Table 9.2. 
Table 9.2
Operator
Meaning
And
Both sides must be true
or
One side or other must be true
Xor
One side or other must be true but not both
Not
Negates truth
* Normally the above operators are use to compare numerical data. However, you can also compare strings with the above operators. In making strings comparison,  there are certain rules to follows: Upper case letters are less than lowercase letters, "A"<"B"<"C"<"D".......<"Z" and number are less than letters.

9.3  Using  the If control structure  with the Comparison Operators

To effectively control the VB2008 program flow, we shall use the If control structure together with the conditional operators and logical operators. There are basically three types of If control structures, namely If....Then statement, If....Then... Else statement and If....Then....ElseIf statement.
9.3(a) If....Then Statement
This is the simplest control structure which  ask the computer to perform a certain action specified by the VB expression if the condition is true. However, when the condition is false, no action will be performed. The general format for the if...then.. statement is


If  condition Then

VB expression

End If
 
Example 9.1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myNumber As Integer
myNumber = TextBox1.Text
If myNumber > 100 Then
Label2.Text = " You win a lucky prize"
End If
End Sub

* When you run the program and enter a number that is greater than 100, you will see the "You win a lucky prize" statement. On the other hand, if the number entered is less than or equal to 100, you don't see any display.

http://www.vbtutor.net/vb2008/vb2008tutor.html

No comments:

Post a Comment