CyberiaPC.com Main Page




Learning VB

Setting Conditional Statements (If, Select)

This section covers the main uses of the If Statement in Visual Basic.  Additionally, it also talks about the Select Case structure.  This lesson is probably the most important for anyone who wants to write programs that can actually "make decisions".  The If Statement is a statement that decides what to do next when the user invokes an event by, say, clicking on a button.  

For example, if a user enters the number '5' in a text box and then clicks on a button, the program might check the value of the text box.  If the value is less than 4, the program should display a message box that says: "You entered a number that is less than 4".  If the value is 5, the program should display a message box that says: "You entered 5".  Else (if none of the above is true), the program should display a message box that says: "You entered something else".

Now, let's write the above example in Visual Basic.  Here is how If Statements generally look like:

     If condition Then
          (what you want to do if condition is true)
     End If

And here is how we would write a functioning If Statement for our example:

     If txt1.Text < 4 Then
          MsgBox "You entered a number that is less than 4"
     ElseIf txt1.Text = 5 Then
          MsgBox "You entered 5"
     Else
          MsgBox "You entered something else"
     End If

The logic is simple to follow.  The If and ElseIf statements all define conditions.  If the condition is true then the line inside the statement is executed.  The Else statement does not define a specific condition.  Its presence is important because if the user were to enter a number that is larger than 4 and not 5, they should also get a message box.  The final line tells the compiler that the current If block has ended.  Oh, and if you're wondering what txt1.text is, it's just the text in the textbox control called txt1.  This will be covered in greater detail in the next lesson.

As for the the MsgBox command, you will see a lot of it later on.  To display a message box to the user, simply enter MsgBox and then the prompt between double quotes.

The Select Case structure is similar to the If statement, however, sometimes it is much more efficient to use Select Case structures because of the way these kinds of statements function.  In other words, expressions are evaluated only once and then compared to multiple values.  Here is the basic format of a Select Case structure:

     Select Case Expression

     Case Condition 1
          do this if expression is true
     Case Condition 2
          do this if expression is true
     Case Else
          do this if expression is true

     End Select

The expression might be a variable declared as Integer called intAge.  Condition 1 could be, say, 20 and condition 2 could be 60.  Then, if the value in intAge is 20 a message box is displayed, and if it's 60 a different message box is displayed.  If it's neither, a different message box pops up.  Here is how this example would look using Select Case:

     Select Case intAge

     Case 20
          MsgBox "You are young"
     Case 60
          MsgBox "You are not young"
     Case Else
          MsgBox "I don't know what you are"

     End Select

If this were written using If statements, it would look something like this:

     If intAge = 20 Then
          MsgBox "You are young"
     ElseIf intAge = 60 Then
          MsgBox "You are not young"
     Else
          MsgBox "I don't know what you are"
     End If

As you can see, the Select Case is much easier to read and follow than the If Statement.

A few more tips, if you want a Case line that executes if one value or the other are true, write this:

     Case 20, 30
          MsgBox "You are young"

And if you want a Case line that executes if a range is true, write this:

     Case 20 To 30
          MsgBox "You are young"

{Go Back To VB Lessons Page}